NSLU2-Linux
view · edit · print · history

The cron daemon built into the NSLU2 doesn't perform any logging of jobs whatsoever, and by squashing the stdout and stderr streams means that some applications will not work as a cron job. An example of this is rsnapshot - the rsync portion will always fail with error 13 unless it is first directed through a script using this method.

A simple solution to this is create a simple batch file to capture the output of your applications and store them in the log directory.

1. telnet / ssh to your NSLU2.

2. Create a /opt/bin/cronlogger using your favourite editor add the following:

  #!/bin/sh

  # Work out names
  name=`basename $1`
  runthis="$1"

  # Update the log
  echo "`date` CRON: $*" >>/var/log/cronlog.log
  shift 1

  # Run the command
  "$runthis" >>"/var/log/$name.log" 2>&1 "$*" &

3. Change the permissions of the script to 755 like so:

  # chmod 755 /opt/bin/cronlogger

4. Edit /etc/crontab. The default lines look like this:

  0 0 * * root /usr/sbin/WatchDog? &>/dev/null

5. Change each line you want logging by adding a /opt/bin/cronlogger. e.g.

  0 0 * * root /opt/bin/cronlogger /usr/sbin/WatchDog? &>/dev/null

6. Save your changes and re-start cron:

  # /etc/rc.d/rc.crond

7. When a cron job you have modified starts new files will be automatically created in /var/log/. The <app name> is the "basename" of the job - so for our example of /usr/sbin/WatchDog? it will be WatchDog?

  cronlog.log         [contains what jobs have ran, when they ran]
  <app name>.log      [the output and error log from the application]

The behaviour above is easily modified by changing the script - for example you could break stdout and stderr into two files like so:

  "$runthis" >>"/var/log/$name.log" 2>>"/var/log/$name.stderr" "$*" &

Following version sends a copy by mail. It stores only output of the last run in the logfile.

  #!/bin/sh
  # email addresses
  sender="<sender@domain>"
  recipient="<recipient@domain>"

  # Get start time
  starttime=`date`

  # Work out names
  name=`basename $1`
  runthis="$1"

  # Update the log
  echo "`date` CRON: $*" >>/var/log/cronlog.log
  shift 1

  # Run the command
  "$runthis" >"/var/log/$name.log" 2>&1 "$*"

  # Send mail
  cat "/var/log/$name.log" | smtpclient -v --from=$sender --subject="Result of $name started at $starttime" $recipient
view · edit · print · history · Last edited by SpaltN.
Based on work by SpaltN, Farmer Ed, and uSURPER.
Originally by uSURPER.
Page last modified on March 17, 2009, at 02:05 PM