NSLU2-Linux
view · edit · print · history

HowTo.EnableLoggingInCron History

Hide minor edits - Show changes to markup

March 17, 2009, at 02:05 PM by SpaltN --
Changed lines 45-46 from:

Edit by SpaltN?: I'm using the following version to get a copy by mail. Only works, if execution of $runthis doesn't take to long. I'm working on a solution.

to:

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

March 17, 2009, at 02:03 PM by SpaltN --
Changed lines 66-69 from:
  "$runthis" "$*" | tee "/var/log/$name.log" | smtpclient -v --from=$sender --subject="Result of $name started at $starttime" $recipient & 
to:
  "$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
March 12, 2009, at 01:43 PM by SpaltN --
Changed lines 45-46 from:

Edit by SpaltN?: I'm using the following version to get a copy by mail

to:

Edit by SpaltN?: I'm using the following version to get a copy by mail. Only works, if execution of $runthis doesn't take to long. I'm working on a solution.

March 09, 2009, at 12:31 PM by SpaltN --
Changed lines 48-65 from:
  1. !/bin/sh
  2. email addresses

sender="<sender@domain>" recipient="<recipient@domain>"

  1. Get start time

starttime=`date`

  1. Work out names

name=`basename $1` runthis="$1"

  1. Update the log

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

  1. Run the command

"$runthis" "$*" | tee "/var/log/$name.log" | smtpclient -v --from=$sender --subject="Result of $name started at $starttime" $recipient &

to:
  #!/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" "$*" | tee "/var/log/$name.log" | smtpclient -v --from=$sender --subject="Result of $name started at $starttime" $recipient & 
March 09, 2009, at 12:29 PM by SpaltN -- Mail enabled cronlogger
Added lines 44-68:

Edit by SpaltN?: I'm using the following version to get a copy by mail (:table border=0 width=100% bgcolor=#eeffee:) (:cell:)

  1. !/bin/sh
  2. email addresses

sender="<sender@domain>" recipient="<recipient@domain>"

  1. Get start time

starttime=`date`

  1. Work out names

name=`basename $1` runthis="$1"

  1. Update the log

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

  1. Run the command

"$runthis" "$*" | tee "/var/log/$name.log" | smtpclient -v --from=$sender --subject="Result of $name started at $starttime" $recipient & (:tableend:)

April 03, 2005, at 09:16 AM by Farmer Ed --
Changed line 31 from:

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

to:

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

April 03, 2005, at 09:15 AM by Farmer Ed --
Changed line 32 from:
  0 0 * * root /usr/bin/cronlogger /usr/sbin/WatchDog? &>/dev/null
to:
  0 0 * * root /opt/bin/cronlogger /usr/sbin/WatchDog? &>/dev/null
January 25, 2005, at 02:07 PM by uSURPER --
Changed line 18 from:
  echo `date` CRON: $* >>/var/log/cronlog.log
to:
  echo "`date` CRON: $*" >>/var/log/cronlog.log
January 25, 2005, at 02:06 PM by uSURPER --
Changed line 22 from:
  $runthis >>/var/log/$name.lof 2>&1 $* &
to:
  "$runthis" >>"/var/log/$name.log" 2>&1 "$*" &
Changed line 43 from:
  runthis >>/var/log/$name.log 2>>/var/log/$name.stderr $* &
to:
  "$runthis" >>"/var/log/$name.log" 2>>"/var/log/$name.stderr" "$*" &
January 25, 2005, at 02:01 PM by uSURPER --
Changed lines 1-43 from:

Describe EnableLoggingInCron here.

to:

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:

table border=0 width=100% bgcolor=#eeffee? cell?

  #!/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.lof 2>&1 $* &

tableend?

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 /usr/bin/cronlogger. e.g.

  0 0 * * root /usr/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 $* &
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