问题
I have a CentOs setup in test server.
I wanna to run a cron job (the cron needs to run apache server at 12AM) daily.
My cron.daily fodler is located in /etc/cron.daily
Please let me know the steps how to implement this.
Usually I use to restart the apache service using the below command:
service httpd restart
I wanna to do restart apache service automatically using cron 12AM daily.
Thanks in advance.
回答1:
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
回答2:
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
回答3:
I am not allowed to comment yet on the last one here, but actually you can just use 0 0 * * * then it will go through a-ok.
回答4:
following this advice adding:
0 12 * * * /etc/init.d/httpd restart
0 24 * * * /etc/init.d/httpd restart
I get "/tmp/crontab.D6cOzs/crontab":3: bad hour
errors in crontab file, can't install.
i had to do 12 only then it worked, so I'm assuming 24 is unacceptable
来源:https://stackoverflow.com/questions/28498400/restart-apache-service-automatically-using-cron-12am-daily