35

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.

Vinayagam
  • 944
  • 1
  • 9
  • 29
  • possible duplicate of [Crontab - Restart apache every 3 hours](http://stackoverflow.com/questions/12862576/crontab-restart-apache-every-3-hours) – runDOSrun Feb 13 '15 at 11:32
  • (use 24 instead of 3 and execute at 12am) – runDOSrun Feb 13 '15 at 11:33
  • Thanks for the reply. I need to open bash file and write like this: 0 */24 * * */ root/restart_apache > /dev/null 2>&1 /etc/init.d/httpd restart Is this rite? – Vinayagam Feb 13 '15 at 12:08

8 Answers8

50

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

Alain Kramar
  • 501
  • 5
  • 4
  • 12
    On my (debian) server I had to use `/usr/sbin/service` rather than `service`. – user3673 Mar 16 '17 at 14:45
  • What about ubuntu? – Sibidharan Jul 26 '17 at 16:06
  • 3
    Cronjobs may not run in the same environment as your shell, to be safe, you can specify the full path, to find it, you can use something like `whereis service` – Oylex Jan 17 '18 at 16:24
  • 9
    You must use `systemctl restart [service]` instead of the deprecated service command. – JuliSmz Sep 27 '18 at 12:32
  • 1
    You should edit the **root** crontab `sudo crontab -e` (assuming your logged in as a non-root user), as a user's crontab won't have permissions to restart a service. – Mint Mar 03 '22 at 22:45
46

I got it and give you step by step adding cron jobs into your system:

  1. Login to your server with SSH
  2. Type crontab -l to display list of cron jobs,
  3. Type crontab -e to edit your crontab,
  4. Add 0 4 * * * /etc/init.d/mysqld restart to restart Mysql everyday at 4 AM,
  5. Add 0 5 * * * /etc/init.d/httpd restart to restart Apache everyday at 5 AM and
  6. Add 0 24 * * * /etc/init.d/httpd restart to restart Apache everyday at 12 AM
  7. Save your file,
  8. Recheck with crontab -l
NealeU
  • 1,264
  • 11
  • 23
Vinayagam
  • 944
  • 1
  • 9
  • 29
7
  1. Get the path for service by running: which service. This should return something like /usr/sbin/service
  2. Add entry to contrab via crontab -e and enter the following:@daily /usr/sbin/service httpd restart
  3. 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
  4. To find what time cron daily runs, run: grep run-parts /etc/crontab

PS: It is important to get the full path to service.

Nepaluz
  • 669
  • 1
  • 12
  • 27
1

It wasn't spelled out in the other answers so I'll say it here. There is a different list of cron jobs for the current user and the root user. On my Raspberry Pi 4, doing it the way above does not work because the current user doesnt have permission to restart the service.

This works however:

sudo crontab -l (List current jobs) sudo crontab -e (Edit cron job list) 0 0 * * * systemctl restart openvpn.service (Add this line to the bottom) Save and close (Ctrl+O, ENTER, Ctrl+X in nano) sudo crontab -l (Validate job was added)

In other words, "crontab -l" will give a different list than "sudo crontab -l". Adding "sudo" to the above commands makes the job run as root.

Kevin
  • 2,296
  • 21
  • 22
1

You can use following command:

crontab -e

Add following line to cron:

0 12 * * * service httpd restart

or use following command.

echo "0 12 * * * service httpd restart" | crontab -

This site is a good one for cron time https://crontab.guru

0

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.

0

Tried on ubuntu 20.04.3 LTS

sudo crontab -e
0 8 * * * /home/<user>/restart_service.sh
# Runs above crontab 8AM everyday.

Inside restart_service.sh

#!/bin/bash
systemctl restart my_service.service

Later provide appropriate permissions for execute

chmod u+x /home/<user>/restart_service.sh

Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
-3

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

  • Hi, welcome to StackOverflow. As this is not an answer to this question, you really need to post it as a new question instead - as it stands it is very unlikely to get any attention. Thanks. – MandyShaw Oct 18 '18 at 19:52