Over a million developers have joined DZone.

A Simple Cron Wrapper Script With Logging

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

When working with crontab service, one thing I often need is to capture the output of the job. Having the job script aware of this output and logging is tedious, and often make the script harder to read. So I wrote a shell wrapper that will redirect all job script's STDOUT into a log file. This way I can inspect it when a job has run and the job script can just focus on the task itself. 

# file: runcmd.sh
# Helper/wrapper script to run any command in the crontab env. This script will ensure
# user profile script is loaded and to log any command output into log files. It also
# ensure not to print anything to STDOUT to avoid crontab system mail alert.
# NOTE: be sure to pass in absolute path of the command to be run so it can be found.
# Usage:
#  ./runcmd.sh find $HOME/crontab/test.sh  # Simple use case
#  LOG_NAME=mytest ./runcmd.sh $HOME/crontab/test.sh # Change the log name to something specific

# Options
DIR=`dirname $0`
CMD_NAME=`basename $1`
LOG="$DIR/logs/$LOG_NAME.log`date +%s`"

# Ensure logs dir exists
if [[ ! -e $DIR/logs ]]; then
  mkdir -p $DIR/logs

# Run cron command
source $HOME/.bash_profile
echo "`date` Started cron cmd=$CMD, logname=$LOG_NAME" >> $LOG 2>&1
$CMD >> $LOG 2>&1
echo "`date` Cron cmd is done." >> $LOG 2>&1

With this wrapper, you can run any shell script and their output will be recorded. For example this job script below will clean up the logs accumulated in our logs folder.

Note that the wrapper will also auto source the ".bash_profile". Often this this is needed if your job script expect all the env variables you already have setup in your login shell scripts.

# file: remove-crontab-logs.sh 
DIR=`dirname $0`/logs
echo "Checking and removing logs in $DIR"
find $DIR -type f -mtime +31 -print -delete
echo "Done"

Now in the crontab file, you may run the job script like this:

# Clean up crontab logs
@montly $HOME/crontab/runcmd.sh $HOME/crontab/remove-crontab-logs.sh

The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today


Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}