How can I set the CentOS to restart a service on a time interval?

  • 1
    Which version of Centos? Is the service started from an init script or via systemd or other method? Does the init script/systemd unit have a "restart" option? – Stephen Harris Jul 5 '16 at 1:44
  • If it's a regular time interval you can use cron if it's a time interval that needs to be determined each time, then you probably want atd. systemd has timers but it's probably best to be as systemd-neutral as possible for the time being. There's still a whole lot of RHEL6 and even RHEL 5 out there. – Bratchley Jul 5 '16 at 1:46
  • @StephenHarris i'm using CentOS 7.2 – Vinicius Silva Jul 5 '16 at 1:58
  • @Bratchley how can i active the cron? – Vinicius Silva Jul 5 '16 at 1:59
  • 1
    You want to do man cron and man crontab... – MAP Jul 5 '16 at 5:08

As said before you need a cronjob to make a time-specific event.

It's a bit confusing at the beginning, but here're examples:

Edit cronjob

crontab -e

Setting time interval

min hour day month day of the week /var/www/app/test.sh


Note - a day of the week are defined in USA week start in Sunday /var/www/app/test.sh - Path to the script or command you'll execute in a time manner.

Here's an example of running by backup script every day at 00:05 only on Saturday AND Sunday:

5 0 * * 0-1 /root/scripts/backup/dayly-backup.sh

With the same command you can restart some service (let's say Apache) everyday at midnight (on Ubuntu):

5 0 * * * /etc/init.d/apache2 restart

Remove crontab (will remove all crontabs defined)

crontab -r

Final notes

What service you need to restart? Keep in mind that most of the services have special modes for restarting them (without killing the service, otherwise this will cause a data loss).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.