Notes about cron, specifically Vixie cron, which is the version I use.
How to create cron jobs
- Create a file called crontab.txt (or whatever you want).
- Add your cron jobs to it.
- Run
crontab crontab.txt
to create the jobs. - Run
crontab -l
to check that they’ve been created properly. Its output should be the same as the contents of crontab.txt.
Example crontab
This sets up a cron job that prints ‘Hello from cron’ on steph’s terminal every 1 minute (it assumes that the write
command is installed, which on Gentoo you’ll find in the util-linux package).
*/1 * * * * echo 'Hello from cron' | write steph
Sending email
Vixiecron will attempt to send email to the user with the result of the cron jobs. This of course means you need email configured correctly on the machine.
To specify which email address receives the email, add the MAILTO variable to the list of jobs, as follows.
MAILTO=steph@localhost
*/1 * * * * echo 'Hello from cron' | write steph
Full example
Here’s a full example, using the method described above.
steph@localhost ~ $ cat crontab.txt
MAILTO=steph@localhost
*/1 * * * * echo 'Hello from cron' | write steph
steph@localhost ~ $ crontab crontab.txt
steph@localhost ~ $ crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (crontab.txt installed on Fri Feb 23 13:12:28 2007)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
MAILTO=steph@localhost
*/1 * * * * echo 'Hello from cron' | write steph
steph@localhost ~ $
Message from steph@localhost on <no tty> at 13:14 ...
Message from cron
EOF
References
Specifying cron user
Vixie cron can’t specify the username in the crontab itself. Instead, specify it when you create the crontab.
crontab -u[user] crontab.txt
crontab -l -u[user]