DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • XAI: Making ML Models Transparent for Smarter Hiring Decisions
  • Build Your Tech Startup: 4 Key Traps and Ways to Tackle Them
  • Why and How to Participate in Open-Source Projects in 2025
  • Top 5 GRC Certifications for Cybersecurity Professionals

Trending

  • Your Ultimate Website QA Checklist
  • Is the Model Context Protocol a Replacement for HTTP?
  • How to Format Articles for DZone
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. A Simple Cron Wrapper Script With Logging

A Simple Cron Wrapper Script With Logging

By 
Zemian Deng user avatar
Zemian Deng
·
Jun. 09, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
7.8K Views

Join the DZone community and get the full member experience.

Join For Free

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="$@"
CMD_NAME=`basename $1`
LOG_NAME=${LOG_NAME:=$CMD_NAME}
LOG="$DIR/logs/$LOG_NAME.log`date +%s`"

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

# 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
career

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

Opinions expressed by DZone contributors are their own.

Related

  • XAI: Making ML Models Transparent for Smarter Hiring Decisions
  • Build Your Tech Startup: 4 Key Traps and Ways to Tackle Them
  • Why and How to Participate in Open-Source Projects in 2025
  • Top 5 GRC Certifications for Cybersecurity Professionals

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!