Slackware 12 - Break out crond of rc.M to allow custom PATH:s

On a machine running Slackware 12 I encountered a problem where the cron daemon had a incomplete PATH variable.

Commands executed in the terminal did not work in cron, for example tar cJf (xz compression).

I found examples online that suggested putting PATH=-declarations at the beginning of the /etc/crontab file. But this did not work as Slackware 12 uses Dillons cron, which wants to have PATH:s defined before it starts.

I grep:ed through the /etc/rc.d startup scripts and found crond in /etc/rc.d/rc.M:

# Start crond (Dillon's crond):
# If you want cron to actually log activity to /var/log/cron, then change
# -l10 to -l8 to increase the logging level.
if [ -x /usr/sbin/crond ]; then
  /usr/sbin/crond -l10 >>/var/log/cron 2>&1
fi

I could simply put my PATH=declaration in the IF-clause above. But as I did not want to pollute this important file with custom PATH declarations I decided to break out the start of cron to a separate file. This also allows restarting the cron daemon.

This is what I did:

Made a backup of rc.M:

cp /etc/rc.d/rc.M /etc/rc.d/rc.M.backup && chmod -x /etc/rc.d/rc.M.backup

Opened /etc/rc.d/rc.M in a text editor, and replaced this IF-clause:

if [ -x /usr/sbin/crond ]; then
  /usr/sbin/crond -l10 >>/var/log/cron 2>&1
fi

with this (comment the above out with # before the lines, so you can revert if you like):

if [ -x /etc/rc.d/rc.crond ]; then
  /etc/rc.d/rc.crond start
fi

Save the file.

Checked the PATH i wanted - the one the current user is running:

echo $PATH

Opened a new file at /etc/rc.d/rc.crond, filled it with the following:

#!/bin/sh
#
# crontab rc script
#

# set this path for cron - replace with what you got from echo $PATH
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

WHAT="crond";
BINFILE=crond;
BINFILE_FULLPATH="/usr/sbin/$BINFILE"

start() {

  if /usr/bin/ps ax | /usr/bin/grep -v grep | /usr/bin/grep -v rc.$BINFILE | /usr/bin/grep $BINFILE > /dev/null
  then
    echo "$WHAT is already running";
    exit;
  else
    echo "Starting $WHAT";
  fi

  # Start crond (Dillon's crond):
  # If you want cron to actually log activity to /var/log/cron, then change
  # -l10 to -l8 to increase the logging level.
  if [ -x /usr/sbin/crond ]; then
     /usr/sbin/crond -l10 >>/var/log/cron 2>&1
  fi

}

stop() {
  echo "Stopping $WHAT..."
  killall $BINFILE
}

# rc.crond start
if [[ "$1" = "start" ]];then
   # do start();
   start
fi

# rc.crond stop
if [[ "$1" = "stop" ]];then
   # do stop();
   stop
fi

# rc.crond restart
if [[ "$1" = "restart" ]];then
 # do stop()
 stop
 # do start()
 start
fi

# ---

Make the new file executable:

chmod +x /etc/rc.d/rc.crond

Test the file before rebooting:

/etc/rc.d/rc.crond stop

You should now have no crond running, test with ps ax|grep crond

/etc/rc.d/rc.crond start

Crond should now start, test again with ps ax|grep crond

Try to reboot, use Shift + PageUp/PageDown to scroll in the log.

If something fails completely use live-CD / live-USB pendrive and to revert rc.M from the backup.

If everything goes as planned you may now have the custom PATH you defined in /etc/rc.d/rc.crond available to your cron scripts.

Check it out by running a cron script with contents like this in it:

#!/bin/bash
echo $PATH >> /tmp/test.log

Remember to change /etc/rc.d/rc.crond when editing PATH:s.

This is a personal note. Last updated: 2015-06-12 00:07:18.



GitHub

My

GitLab

My

LinkedIn

My

Klebe.se

Don't forget to pay my friend a visit too. Joakim