How to setup a crontab job in Linux / Unix

Gerwin JansenSr. IT Engineer
CERTIFIED EXPERT
Apple, Debian, EE, Hardware, MBP, Oracle, Raspberry PI, Shell Scripting, SQL, Testing, Tibco, Windows
Published:
Updated:
Automating work can be done on Linux / Unix by creating what is called a cronjob that is periodically running a program or script (job) on a schedule you specify. Jobs are specified in a crontab (file) and are run by the cron daemon process.
 

The crontab

The crontab itself is a time table that has entries for jobs and the schedule on which these jobs need to execute. Each user on a Linux/Unix system can have a crontab. If you cannot access your crontab, an administrator (root) can grant you access.

A sample crontab entry could look like this:
00 11 * * * rm -f /var/tmp/*

Open in new window

The first part "00 11 * * *" is a specification of the time (or schedule) and the second part "rm -f /var/tmp/*" is the job that will be executed at that time.
 

Basic commands

crontab -l
  (show your crontab)

crontab -r
  (remove your crontab - be careful)

crontab -e
  (edit your crontab)
 

How to specify the schedule

Let's take a sample entry:

00 11 2 * *

Meaning:
00 - minute (number 0-59)
11 - hour (number 0-23)
  2 - day of month (number 1-31)
  * - month (number 1-12 / names allowed as well)
  * - day of week (number 0-7 / 0 or 7 for sunday, names allowed as well)

The example would mean that a job would be scheduled at 11:00 (AM) on the day 2 of every month. You can specify multiple schedules either by adding lines or by combining a comma as a separator:
00,15 11 2 * *

Open in new window

For more details on scheduling use "man 5 crontab" on your system. For more information on tables for driving cron jobs, see this very clear description.
 

How to specify the job

You can specify the job (program, script or commands) by adding it directly behind the schedule:

00 11 * * * rm -f /var/tmp/*
  (one command)

00 11 * * * rm -f /var/tmp/* && touch /var/tmp/cleanup_done
  (more commands)

00 11 * * * /home/username/cronscript.sh
  (a script on your homedirectory)

Note: cron does not know of any environment settings you may have setup in your profile. So if you would have ${ORACLE_HOME} in your PATH and you type sqlplus to start the sqlplus application, this will not work in a script that is started by cron. You either have to specify a full path to each command in your script, set the path first or call your environment script.
 

A real example

In this example, I want to know the status of the power supply of a banana pi device that I have. The details of the power supply can be shown by looking at a special device (uevent).

This is the (root) crontab entry:
*/5 * * * * /root/pwr

Open in new window

(note the special /5 at the minute field which means every 5 minutes, so 0,5,10,15 etc.)

The pwr script that is being called here looks like this:

echo $(date) >> /root/pwr.txt
                      cat /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply/ac/uevent >> /root/pwr.txt

Open in new window

The script is echoing the current date/time and adding the output of the cat ... uevent to the pwr.txt file.

The last lines of the pwr.txt file:
Sun Feb 15 23:25:02 CET 2015
                      POWER_SUPPLY_NAME=ac
                      POWER_SUPPLY_MODEL_NAME=ac
                      POWER_SUPPLY_PRESENT=1
                      POWER_SUPPLY_ONLINE=1
                      POWER_SUPPLY_VOLTAGE_NOW=4846000
                      POWER_SUPPLY_CURRENT_NOW=313000

Open in new window

Where I can see that the pwr script ran at 23:25 and that the supply voltage is 4846000mV (4.8V) and the device is using 313000uA (313mA) so about 1.5W.
 

Some best practices

Redirecting output of your job
If you want to know if your crontab is generating output, redirect std error and std output by adding this at the end of your crontab line:
2>&1 >> /home/username/cronscript.log

Open in new window


Howto check if you job did run (or not)
When you want to know whether a crontab job ran, you can add a touch command after the command like this:
&& touch /home/username/cronscript.lastrun

Open in new window

When you look at the file cronscript.lastrun, look at its timestamp to know the last time it ran. If you get no file, it did not run at all

Temporary disabling
To disable one crontab job, edit your crontab using crontab -e and put a # (hash) in front of the line you want to disable.

To disable the whole crontab, first save your crontab entries using crontab -l > saved_crontab and then remove it using crontab -r If you want to enable it again use crontab saved_crontab
 
3
4,256 Views
Gerwin JansenSr. IT Engineer
CERTIFIED EXPERT
Apple, Debian, EE, Hardware, MBP, Oracle, Raspberry PI, Shell Scripting, SQL, Testing, Tibco, Windows

Comments (1)

CERTIFIED EXPERT

Commented:
Good One
Very useful for Linux sysadmins and Oracle DBAs.
Voted Good article :)

Thanks for sharing it

Regards,
Yashwant Vishwakarma

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.