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:
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