Reputation: 1002
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.
Upvotes: 39
Views: 133013
Reputation: 3170
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
Upvotes: 0
Reputation: 317
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
Upvotes: 2
Reputation: 697
which service
. This should return something like /usr/sbin/service
crontab -e
and enter the following:@daily /usr/sbin/service httpd restart
@daily /usr/sbin/service httpd restart > /dev/null 2>&1
grep run-parts /etc/crontab
PS: It is important to get the full path to service.
Upvotes: 7
Reputation: 2406
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.
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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
Upvotes: -3
Reputation: 1002
I got it and give you step by step adding cron jobs into your system:
crontab -l
to display list of cron jobs,crontab -e
to edit your crontab,0 4 * * * /etc/init.d/mysqld restart
to restart Mysql everyday at 4 AM,0 5 * * * /etc/init.d/httpd restart
to restart Apache everyday at 5 AM and0 24 * * * /etc/init.d/httpd restart
to restart Apache everyday at 12 AMcrontab -l
Upvotes: 47
Reputation: 531
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:00am
Add 0 5 * * * service apache2 restart
for apache2 at 5:00am
Add 0 0 * * * service apache2 restart
for apache2 at 12:00 am
Save and close (Ctrl+O and Ctrl+X in nano)
Recheck with $ crontab -l
Upvotes: 53