Task Scheduling In Linux Command Line
In this tutorial we can learn how to schedule tasks via the Linux Command line
Scheduling one-time tasks with at
One solution available to users of a Linux system for scheduling future tasks is at. This not a standalone tool, but rather a system daemon (atd), with a set of command-line tools to interact with the daemon (at, atq and more). In Linux installation, the atd daemon will be installed and enabled automatically. The atd daemon can be found in the at package. Users (including root) can queue up jobs for the atd daemon using the command-line tool at. The atd daemon provides 26 queues, a to z, with jobs in alphabetically later queues getting less system priority.
A new job can be scheduled by using the command at <TIMESPEC>.at then read this commands to execute from stdin. For larger commands, and typo-sensitive commands. It is often easier to use input redirection from a script file, eg., at now +5min < myscript, than typing all the commands by hand in a terminal window. When entering commands by hand, you can finish your input by pressing ctrl+D. The <TIMESPEC> allows for many powerful combinations, giving users a free-form way of describing exactly when a job should run. Typically, they start with a time, eg.,02:00pm or 15:59 followed by an optional date or number of days in the future.
Some examples of combinations that can be used are listed in the following text.
1) now +5min
2) teatime tomorrow (teatime is 16:00)
3) noon +4 days
4) 5pm august 3 2016
Inspecting and managing jobs
To get an overview of the pending jobs for your user, use the command atq or, alternatively. the alias at -l.
Running this command gives the following output:
28 Mon Feb 2 05:13:00 2015 a job1
29 Mon Feb 3 16:00:00 2014 h job2
27 Tue Feb 4 12:00:00 2014 a job3
This shows columns for every job scheduled to run in the future:
1) The job number 28 in the first line.
2) The date and time scheduled for that job, Mon feb 2 05:13:00 2015 in the first line.
3) The queue for the job, a in the first line, but h in the second
4) The owner of the job (and user as which the job will run), Student in all our lines.
To inspect the actual commands that will run when a job is executed, use the command at -c <JOBNUMBER>. This output will first show the environment for the job being set up to reflect the environment of the user who created the job at the time it was created, followed by the actual commands to be run.
The atrm <JOBNUNUMBER> will remove a scheduled job. This is useful when a job is no longer needed; for example, when a remote firewall configuration succeeded, and does not need to be reset.
Scheduling Recurring Task with Cron
Schedule a recurring job by having the job resubmit a new job at the end of its execution. Linux systems ship with the crond daemon enabled and started by default specifically for recurring jobs. crond controlled by multiple configuration files, one per user and systemd wide files. These configuration files give users and administrators fine-grained control over exactly when their recurring jobs should be executed. The crond daemon is installed as part of the cron package.
When editing jobs with crontab -e, an editor will be started, the file being edited will have one job per line. Empty lines are allowed, and comments start their line with a hash symbol. Environment variables can also be declared, using the format NAME=Value, and will reflect all lines below the line where they are declared. Common environment variables in a crontab include SHELL and MAILTO. Setting the shell variable will change which shell is used to execute the commands on the lines below it, while setting the MAILTO variable will change will email address output will be to.
Individual jobs consist of six fields detailing when and what should be executed. When all five of the first fields match the current date and time, the command in the last field will be executed. These fields are below:
Example cron Jobs
1) 0 9 2 2 * /usr/local/bin/yearly_backup
Execute the command /usr/local/bin/yearly_backup at exactly 9 am on February 2nd. every year.
2) */7 9-16 * jul 5 echo “chime”
Send an email containing the word chime to the owner of this job, every seven minutes between 9 am and 5 pm on every Friday in July.
3) 58 23 * * 1-5 /usr/local/bin/daily_report
Run the command /usr/local/bin/daily_report every weekday at two minutes before midnight.
If you need any further assistance please contact our support department.