
|
View Full Version : Restart Apache via Cron
hostseeker 12-30-2001, 09:29 AM I want to restart Apache daily via cron, (running Redhat 7.1) so I edited crontab and inserted the following line:
30 3 * * * /etc/rc.d/init.d/httpd restart
Is this an acceptable way to restart Apache every morning at 3:30 am? If not what is a better way and the reasons?
How do I know if it restarts since it does not send an email since it is the root cron and not one of the accounts?
Many thanks for this forum and your help!
Hostseeker
ffeingol 12-30-2001, 10:41 AM That should work fine, but I have a couple of suggestions :D
You might want to use "graceful" instead of "restart". They both do the same thing, but graceful lets current connections end, where restart just stops/starts the server.
If you want a mail everyday from this, you could just make the entry like this (or create a small script):
30 3 * * * /etc/rc.d/init.d/httpd restart 2>&1 | /path/to/mail -s "restart Apache" user@your.domain
The 2>&1 sends standard error to the same place that standard out is going. The rest will just pipe the output to mail and send you a message.
Frank
hostseeker 12-30-2001, 11:07 AM Frank,
Thanks for your helpful reply! I had heard of a "graceful" restart but did not know how to do it.
Before editing Crontab I tested your suggestion at the command line, but got an error from Exim. I used:
/etc/rc.d/init.d/httpd graceful 2>&1 | /usr/sbin/sendmail -s "restart Apache" me@mycorrectemail.com
Got this error: exim abandoned: unknown, malformed, or incomplete option -s
So I tried it without the -s and didn't get an error, but I didn't get the email either.
Any ideas?
ffeingol 12-30-2001, 11:19 AM Sorry, not sendmail, just "mail". On most linux systems it's /bin/mail.
Frank
Mike the newbie 12-30-2001, 11:45 AM Originally posted by ffeingol
The 2>&1 sends standard error to the same place that standard out is going.
Thanks, I always wondered about that, but I never took the time to track it down. :D
hostseeker 12-30-2001, 12:56 PM Originally posted by ffeingol
Sorry, not sendmail, just "mail". On most linux systems it's /bin/mail.
Frank
Frank,
That did it, thanks so much for your help!
ffeingol 12-30-2001, 05:26 PM No problem :D
Frank
ADEhost 12-30-2001, 05:46 PM Preface : I'm not that good with unix
Why would you want to restart every day? the risk to the end users services would high. If I was a hosting client, would hate knowing that you have to reboot.
If it's because there a to many processes running that have not died then you should look to find those before you go out and reset the entire system.
again I don't know why, but maybe a tune up in needed on the system.
Side note.
does the restart affect the drive speed in anyway? in otherwords, when you restart windows the drive slows down then get's back to normal speed. this is a ware and tear issue.
mike
driverdave 12-30-2001, 08:15 PM **********, I'm curious, why do you need to re-start apache daily?
As far as I know, there should be no need to re-start anything daily on a properly configured system.
hostseeker 12-30-2001, 09:51 PM I have tnis database script that sometimes hangs. Restarting Apache clears it. I know, you're going to say I should fix the script, but I have tried and can't find the problem. The script is the basis of one of my very successful web sites and I have done much customization to the site and the script and finding another script is not an option.
So I do the only thing that I can find that works! In fact, I may restart it every hour.
Anyway, restarting Apache doesn't harm anything. Everytime I add a new account to my server it restarts.
hostseeker 01-03-2002, 10:33 AM Re:
30 3 * * * /etc/rc.d/init.d/httpd graceful 2>&1 | /bin/mail -s "restart Apache" me@mymail.com
While the above works at the command line it will not work with cron. I edited crontab using crontab -e and added the line to it and crontab would not accept it, giving the error of "bad hour - can't install" even though there is no problem with the hour and it will take this command line minus the email portion that begins with 2>
Any other suggestions? To refresh, my goal is to get email with the output from the above cron job, which will restart apache once a day.
Thanks!
ffeingol 01-03-2002, 12:28 PM Put the command into a script (i.e.):
#! /bin/bash
/etc/rc.d/init.d/httpd graceful 2>&1 | /bin/mail -s "restart Apache"
Make sure to chmod the script to be executable. Then run the script from cron.
Frank
hostseeker 01-03-2002, 01:16 PM Thanks Frank, I'm still learning all this command line stuff, so bear with me.
Would I save the text you wrote as say myfile.sh and upload it to any directory that only has root access then chmod it so that the user has executable permissions?
I did this and put it in the /usr directory and when I try to execute it I get the error "file or directory not found". I am sure I entered the correct path and spelling.
Thanks for your help.
ffeingol 01-03-2002, 01:55 PM If you're trying to test the script as root, you should put in the full path to the file. i.e.
/usr/my-script.sh
even if you are in the /usr directory. root usually has a very limited path.
See if that solves the problem.
Frank
hostseeker 01-03-2002, 02:18 PM I tried using the full path and it doesn't work, I get the error "no such file or directory" even though I can see the file in the /usr directory. The file does have an * after it, I don't know what that means, does this have something to do with it?
ffeingol 01-03-2002, 02:51 PM hmm,
Sounds like there is no bash on your system. Can you try:
which bash
If that comes back, check the path to bash agains the path on the 1st line of the script.
If you don't have bash, you can try /bin/sh on the first line of the script (i.e. #! /bin/sh)
Frank
hostseeker 01-03-2002, 03:08 PM which bash returned:
/bin/bash
|