Bash – cron.hourly script executing multiple times

bashcron

The setup

I have a script /etc/cron.hourly/msm-hourly that contains this:

#!/bin/sh
LOGPATH='/opt/msm/servers/cronscripts'
LOGFILE="$LOGPATH/msm-start.log"

# Make restart dead servers
echo "" >> $LOGFILE && /bin/date >> $LOGFILE && /etc/init.d/msm start >> $LOGFILE

Every hour:

  1. Create a new line in the log file
  2. Add the date
  3. Then the output of the msm start command

It works running from the command line as well as within /etc/cron.daily/

The issue

The output, when run from the command line looks like this:

[ msm-hourly.log ]

Thu Sep 12 20:56:51 UTC 2013
[ACTIVE] Server already started.

However, after leaving it run for a while the output on each iteration looks like this:

[ msm-hourly.log ]

Thu Sep 12 20:56:51 UTC 2013
[ACTIVE] Server already started.


Thu Sep 12 21:01:02 UTC 2013
Thu Sep 12 21:01:02 UTC 2013

Thu Sep 12 21:01:02 UTC 2013

Thu Sep 12 21:01:02 UTC 2013

Thu Sep 12 21:01:02 UTC 2013

Thu Sep 12 21:01:02 UTC 2013
[ACTIVE] Server already started.
[ACTIVE] Server already started.
[ACTIVE] Server already started.
[ACTIVE] Server already started.
[ACTIVE] Server already started.
[ACTIVE] Server already started.

Here's the output from /var/logs/cron :

Sep 12 21:01:02 server run-parts(/etc/cron.hourly)[19497]: starting msm-hourly
Sep 12 21:01:02 server run-parts(/etc/cron.hourly)[19498]: starting msm-hourly
Sep 12 21:01:02 server run-parts(/etc/cron.hourly)[19522]: starting msm-hourly
Sep 12 21:01:02 server run-parts(/etc/cron.hourly)[19541]: starting msm-hourly
Sep 12 21:01:02 server run-parts(/etc/cron.hourly)[19549]: starting msm-hourly
Sep 12 21:01:02 server run-parts(/etc/cron.hourly)[19565]: starting msm-hourly
Sep 12 21:01:03 server run-parts(/etc/cron.hourly)[20166]: finished msm-hourly
Sep 12 21:01:03 server run-parts(/etc/cron.hourly)[20182]: finished msm-hourly
Sep 12 21:01:03 server run-parts(/etc/cron.hourly)[20243]: finished msm-hourly
Sep 12 21:01:03 server run-parts(/etc/cron.hourly)[20288]: finished msm-hourly
Sep 12 21:01:04 server run-parts(/etc/cron.hourly)[20310]: finished msm-hourly
Sep 12 21:01:04 server run-parts(/etc/cron.hourly)[20315]: finished msm-hourly

Question

I've attempted to replace and rework the script followed by /etc/init.d/crond reload and/or /etc/init.d/crond restart.

Things restart without errors and the script fires every hour as it should.. just with all the extra lines.

How do I track this down to better understand what's going on and fixing it so that the msm-hourly only runs once per hour?

Best Answer

Check /etc/crontab to make sure the line for cron.hourly is similar to the one below (paying particular attention that only the minutes field has a number and the rest are asterisks) and appears only once.

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly

Also, make sure your script (or /etc/cron.hourly) is not entered in any other crontabs, including a user's (or root's) and that multiple copies (backups) don't appear in /etc/cron.hourly.

Note that the last line in your script can be simplified to:

{ echo && /bin/date && /etc/init.d/msm start; } >> $LOGFILE

Please also see Process Management.

Related Topic