Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the best way to stop/start a particular service on a schedule using cron?

I tried running service servicename stop in a cron job but that didn't appear to work so switched to /etc/init.d/servicename

0 19 * * * /etc/init.d/servicename stop >/dev/null 2>&1
0 7 * * * /etc/init.d/servicename start >/dev/null 2>&1

That appears to work, not sure how consistently yet.
But the strange thing is that I get an e-mail from cron with the following error:

/bin/sh: line 1: 20178 Terminated  /etc/init.d/servicename start > /dev/null 2>&1

I only seem to get this error on the starting of the service. The stopping appears to be fine.

Also I thought that the >/dev/null 2>&1 should suppress the e-mailing of notifications. In this case though I'm glad it didn't.

Anyone know of a better way to stop/start services on a schedule, hopefully using cron?
Anyone got any idea what this error is about?


share|improve this question
up vote 1 down vote accepted

Yes, a better way to handle this is by using a job control utility like Monit. It's available for CentOS and is a clean way to ensure that your daemons are running when they need to, and can be used to start/stop individual or groups of services on a schedule (via cron)... The example where I see cron fall through the cracks is when something happens within the service window (application crash, etc.)

Here's an example with the CUPS printing service...

In a Monit config file somewhere, I'd have a stanza defining the CUPS service, including its PID and start and stop commands.

check process cups
        with pidfile "/var/run/"
        start program = "/sbin/service cups start"
        stop program = "/sbin/service cups stop"

Running monit status

Process 'cups'
  status                            Running
  monitoring status                 Monitored
  pid                               2357
  parent pid                        1
  uptime                            8d 6h 43m 
  data collected                    Mon, 13 Aug 2012 09:07:03

Running monit stop cups stops the service...

Process 'cups'
  status                            Not monitored
  monitoring status                 Not monitored
  data collected                    Mon, 13 Aug 2012 09:08:03

monit start cups starts it. I usually add the monit start and stop commands in the crontab to handle the application startup and shutdown. Monit will ensure that the service is also running during those times (e.g. after a mid-day reboot)...

share|improve this answer
That's very cool. Thanks for the great answer too! – batfastad Aug 13 '12 at 13:58

You should be able to use /sbin/service...and in fact, that's a good habit to get into, since the next release of RHEL/CentOS/etc will probably use systemd instead of the legacy /etc/init.d scripts. If calling /sbin/service isn't working, you may want to take a look at any error output the command is generating.

The Terminated error you're getting may be a buggy service control script...if it does something like killall servicename it may inadvertently kill the control script, which can result in this error. You're still seeing the error output because this error message is coming from the shell that cron starts to run your command, rather than from the command itself. If that's what's happening here, you may want to take a look at the init script and see if you can fix the problem.

When debugging errors, it's sometimes a good idea to redirect stdout and stderr to a file rather than /dev/null (although that wouldn't help in this case).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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