restart apache service automatically using cron 12AM daily

Vinayagam picture Vinayagam · Feb 13, 2015 · Viewed 82.3k times · Source

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.

Answer

Alain Kramar picture Alain Kramar · May 22, 2015

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:

  1. SSH into server.

  2. Get list of current user's jobs with $ crontab -l

  3. Edit jobs list with $ crontab -e (default editor will open)

  4. Add 0 4 * * * service mysql restart for mysql at 4:00am

  5. Add 0 5 * * * service apache2 restart for apache2 at 5:00am

  6. Add 0 0 * * * service apache2 restart for apache2 at 12:00 am

  7. Save and close (Ctrl+O and Ctrl+X in nano)

  8. Recheck with $ crontab -l