restart apache service automatically using cron 12AM daily
While @einterview's answer is almost correct, it's important to note that a *
in the minute column will run the job every minute of that hour. If intending to run once every hour, steps would be:
SSH into server.
Get list of current user's jobs with
$ crontab -l
Edit jobs list with
$ crontab -e
(default editor will open)Add
0 4 * * * service mysql restart
for mysql at 4:00amAdd
0 5 * * * service apache2 restart
for apache2 at 5:00amAdd
0 0 * * * service apache2 restart
for apache2 at 12:00 amSave and close (Ctrl+O and Ctrl+X in nano)
Recheck with
$ crontab -l
I got it and give you step by step adding cron jobs into your system:
- Login to your server with SSH
- Type
crontab -l
to display list of cron jobs, - Type
crontab -e
to edit your crontab, - Add
0 4 * * * /etc/init.d/mysqld restart
to restart Mysql everyday at 4 AM, - Add
0 5 * * * /etc/init.d/httpd restart
to restart Apache everyday at 5 AM and - Add
0 24 * * * /etc/init.d/httpd restart
to restart Apache everyday at 12 AM - Save your file,
- Recheck with
crontab -l
- Get the path for service by running:
which service
. This should return something like/usr/sbin/service
- Add entry to contrab via
crontab -e
and enter the following:@daily /usr/sbin/service httpd restart
- If you do not want an email sent to you whenever it is run, you should instead add the following:
@daily /usr/sbin/service httpd restart > /dev/null 2>&1
- To find what time cron daily runs, run:
grep run-parts /etc/crontab
PS: It is important to get the full path to service.