Skip to content

Instantly share code, notes, and snippets.

@alesk
Created February 15, 2012 12:30
Show Gist options
  • Save alesk/1835384 to your computer and use it in GitHub Desktop.
Save alesk/1835384 to your computer and use it in GitHub Desktop.
Crontab execution test

Cron jobs

Let's assume we are working under user ales.

To install script to run periodically with ales's privileges, use

> crontab -e

5 10 * * * /home/ales/myscript.sh

This will run myscript.sh at 10:5 each day. To test the script, first prepare execution environment of crontab with:

sudo su
env -i /bin/bash --noprofile --norc

and then run the script.

For ~/.bashrc is not automatically loaded when cron job starts, it is recomended to put

source ~/.bashrc

somwhere early in your script.

It is good practice to log the execution result into syslog by:

logger -p user.notice -t MYSCRIPT "Operation myscript successful"

And to send na email to inform administrator the myscript.sh has been run:

Logging

After every execution of cron job, the standard output is sent to the coresponding user's mail.

sendmail -oi -t << EOF
From: proces@myhost.com
To: admin@myhost.com
Subject: script myscript execution results

...
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment