
|
View Full Version : cron job question
mpkapadia 10-03-2001, 05:51 PM Hi,
I have to do this bcos i have a small problem with tomcat on my setup , If i restart tomcat once in a day it runs fine, If i dont then it sometimes stops for no reason and gives internal server error on all pages,
My question,
Can someone tell me how do i setup a cron job to run
/etc/rc.d/init.d/tomcat.sh stop
/etc/rc.d/init.d/tomcat.sh start
Every 12 hours, daily
Is this possible,
Please help
With regards
Manish Kapadia
Originally posted by mpkapadia
Can someone tell me how do i setup a cron job to run
/etc/rc.d/init.d/tomcat.sh stop
/etc/rc.d/init.d/tomcat.sh start
Every 12 hours, daily
Make a script with the following contents:
#!/usr/bin/sh
/etc/rc.d/init.d/tomcat.sh stop
/etc/rc.d/init.d/tomcat.sh start
And save it somewhere on the server.
Then login as root and run this command:
crontab -e
Add a line like the following to your crontab:
* */12 * * * /path/to/script/name.sh
Check out man crontab for more information.
There's no real need for a shell script. Easiest way IMO is to edit /etc/crontab and add:
0 */12 * * * root /etc/rc.d/init.d/tomcat.sh start; /etc/rc.d/init.d/tomcat.sh stop
For simplicity, you might want to edit tomcat.sh and add a restart option that simply calls 'start' and 'stop'...
Also note, your * isn't what you want, you need a zero (or some number) to keep it from running every minute for two hours out of the day :)
Originally posted by Jm4n
There's no real need for a shell script. Easiest way IMO is to edit /etc/crontab and add:
For maintainability, a shell script is often preferred. It also gives an easier way of doing logging.
IMHO using /etc/crontab is an ugly hack. I prefer using the standard crontabs...
Also note, your * isn't what you want, you need a zero (or some number) to keep it from running every minute for two hours out of the day :)
Ofcourse...
mpkapadia 10-06-2001, 02:55 AM Will this work,
My etc/crontab has following lines,
I suppose the 4 entries are there by default,
------------------------------------------------------------
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
------------------------------------------------------------
Now what i did is in /etc/cron.daily folder i created a file tcat.cron
with following contents
-----------------------------------------
#!/usr/bin/sh
/etc/rc.d/init.d/tomcat.sh stop
/etc/rc.d/init.d/tomcat.sh start
-----------------------------------------
Then i have chmod it to 755 ,
Will this setup work, How do i verify that it is working,
Please help me and thanks in advance,
With regards
Manish Kapadia
Yes, that would work as well. The hourly/daily/weekly/monthy crons are generally for system services such as rebuilding the 'slocate' databse, flushing unused kernel modules, and things like that, but there's no reason you can't use it for this purpose as well.
IMHO using /etc/crontab is an ugly hack.
Yes, but certainly you would agree that periodically restarting a service because it keeps failing is also an ugly hack, no? If this were Windows I'd not give it a second thought, but all of the services I run can go months without crashing or leaking memory...
I personally prefer /etc/crontab only because, for me, it's much more managable. I have one location where my crons are run from, and I do my stats processing, backups, and other things from there.
mpkapadia 10-06-2001, 08:01 AM How can i verify that this is working correctly,
I am on Rh 7.1 with plesk machine
Regards
Manish Kapadia
Originally posted by Jm4n
Yes, but certainly you would agree that periodically restarting a service because it keeps failing is also an ugly hack, no?
Surely, but in that case you would be aware that it's a hack.
Most people don't feel the same about /etc/crontab
I personally prefer /etc/crontab only because, for me, it's much more managable. I have one location where my crons are run from, and I do my stats processing, backups, and other things from there.
Why is that more "manageable" or more "everything in one place" than the standard crontabs?
I.e. if you'd jus use 'crontab -e' as the root user - that would also give you just one crontab where everything is run from.
Originally posted by mpkapadia
How can i verify that this is working correctly,
I am on Rh 7.1 with plesk machine
You should receive an email, as the tomcat start/stop commands give some output.
Please note that the email is normally sent to the admin user on the box.
Please note that the program starts only once a day (normally late at night, at approx. 04:00).
|