Linux Job Scheduling Commands

There are two main tools used to perform scheduled tasks, at and cron. You may also like to try anacron if your computer does not run continuously, as cron will only work if your computer is left on (anacron can catch up with the scheduled tasks the next time the computer is on…). Fcron is also another best tool for job scheduling.

 

AT command

 

At is a Job spooling tools for linux/unix which can executes a command once on a particular day, at a particular time. at will add a particular command or script to be executed. For example

 

  • # at 21:00

 

You then type the commands you want executed then press CTRL−D to save and exit the jobs. Another example

 

  • # at now + time

 

This will run at the current time + the hours/mins/seconds you specify (use at now + 1 hour to have command(s) run in 1 hour from now…). You can also use the −f option to have at execute a particular file (a shell script).

 

  • # at −f /pathto/shell_script now + 1 hour

 

This would run the shell script 1 hour from now.

 

Atq command

 

Will list jobs currently in queue for the user who executed it, if root executes at it will list all jobs in queue for the at daemon. Doesn’t need or take any options.

 

  • # atq

 

To remove a job from the ‘at’ queue. Use atrm

 

  • # atrm job_no

 

The job no can be identified using the ‘ atq ‘ command.

 

 

vixie-cron (Crond)

 

Cron can be used to schedule a particular function to occur every minute, hour, day, week, or month. It’s normal to use the crontab to perform the editing functions as this automates the process for the cron daemon and makes it easier for normal users to use cron. Crontab is used to edit, read and remove the files which the cron daemon reads

 

 

Crontab

 

When using crontab −e you have a number of fields  what they mean is listed below:

 

 

Crontab Syntax

 

There are also a number of shortcut methods for common tasks

 

@reboot −−− run command at reboot
@yearly −−− same as 0 0 1 1 *
@annually −−− same as @yearly
@monthly −−− same as 0 0 1 * *
@weekly −−− same as 0 0 * * 0
@daily −−− same as 0 0 * * *
@midnight −−− same as @daily
@hourly −−− same as 0 * * * *

 

Note that * (asterisk) is used to mean anything (similar to the wildcard). For example if you leave the day part (the 5th place) with an asterisk it would mean everyday.

 

When writing a crontab entry you simply type in six fields separated by spaces, the first five are those listed in the table (using numbers or letters and numbers as appropriate), the 6th field is the command to be executed and any options, cron will read everything up until the newline.

 

  • 5 4 * * sun echo “run at 5 after 4 every sunday”

 

Anacron

 

Anacron is another tool designed for systems which are not always on, such as home computers. While cron will not run if the computer is off, anacron will simply run the command when the computer is next on (it catches up with things). Anacron syntax’s below :

 

-f Force execution of the jobs.
-u Only update the timestamps of the jobs.
-s Serialize execution of jobs.
-n Run jobs now
-d Don’t fork to the background.
-q Suppress messages to standard error
-h Print short usage message, and exit.

 

Fcron

 

Fcron is a periodical command scheduler which aims at replacing Vixie Cron, so it implements most of its functionalities. But fcron does not assume that your system is running neither all the time nor regularly : you can, for instance, tell fcron to execute tasks every x hours y minutes of system up time or to do a job only once in a specified interval of time.

 

Useful Links :

 

Fcron Help Docs : http://fcron.free.fr/doc/en/index.html

Anacron Man page : http://linux.die.net/man/8/anacron