cron Notes

There's information about cron all over the place. This note is only a quickie example of lifecycle.

The script to run periodically

I've chosen to do something here whose effects are easily seen. I'm touching a file in the filesystem. If I do this every minute (see below), then I'll be able to use ll to see that happen without having to wait a long time or go looking around through stuff.

do_mycronjob.sh:
	#!/bin/sh
	touch /home/russ/do_mycronjob.hey

The crontab file

This is a separate file I give to crontab with the schedule of when I want cron to run my script. This does every minute of every hour of every day.

do_mycronjob.sample:
	# Every minute, run this script:
	* * * * * /home/russ/do_mycronjob.sh

I'm not going to waste my time attempting to explain what can go into the five, separate places (mostly numbers corresponding to minutes [0-59], hours [0-23], day of month [1-31], month of year [1-12], or day of week [0-6]). You can look that stuff up all over the web.

Submitting the cron job

One submits a cron job, which cron will run as the submitting user (so no tasks requiring root access, access as another user, etc.) as show in the first command line. The second command line allows you to see what your cron job is.

	$ crontab do_mycronjob.sample
	$ crontab -l
	# Every minute, run this script:
	* * * * * /home/russ/do_mycronjob.sh

Unscheduling a cron job

You can edit the current job or collection of jobs using the first command below. Whatever /usr/bin/editor is set to will be used for that. Using the second command completely removes the job(s) your user has.

	$ crontab -e
	$ crontab -r
	$ crontab -l
	no crontab for russ

Out on the edge...

At least on Linux, you can also employ environment varibles right inside the crontab file!

do_mycronjob.sample:
	# Every minute, run this script:
	FROM_HERE=/home/russ
	* * * * * ${FROM_HERE}/do_mycronjob.sh