Task Scheduling in Linux with cron and at command

For task scheduling cron command and at command is used in Linux. This tutorial explains both commands in detail including supported options, parameters and arguments. Learn how to schedule and manage a task or job in Linux through cron and at command step by step with practical examples.

at daemon allows you to run the command of your choice, once, at a specified time in the future.

You can set an at job to be run once. The at daemon works to the print process; jobs are spooled in the /var/ spool/at directory and run at the specified time.

You can use the at daemon to run the command or script of your choice. For the purpose of this article we are creating a simple script that will list the contain of current directory and send this output to terminal 2.

From the command line, you can run the at time command to start a job to be run at a specified time. That time can be now; in a specified number of minutes, hours, or days; or at the time of your choice. We will take several example to illustrate it more deeply. The CTRL-D command exits the at command shell and returns to your original command line interface.

Login from root and create a simple script file test.sh and grant it execute permission

create simple scripts

Check date before scheduling job from at commands

date command

To run this script on 21 Feb 2010 after seven days you need to schedule at command as shown here

at command with days

To run this script after ten minutes you need to schedule at command as shown here

at commands with minutes

To run this script now you need to schedule at command as shown here

at commands with now

To run this script on 10:15 AM you need to schedule at command as shown here

at command with time

To check the status of your jobs, so you can see if it will work, run the following job queue command:

atq

If there's a problem with the job, you can remove it with the atrm command. In this example you would remove job number 4 with the following command:

atrm

Securing At daemon

You may not want everyone to be able to run a job in the middle of the night. If your system have important security data, someone may download important data or worse, and it could be done before you discover the security violations.

Two files are used to control the behavior of at daemons

  • /etc/at.allow If present then only users those name are in this file can use at daemons
  • /etc/at.deny If present then only user those name are in this file will not be able to use at daemons apart from these user all other can use at daemons
  • If both files are not present then only root can access at daemons

For example create two user Vinita and nikita

useradd

These files are formatted as one line per user; add user vinita to at.allow

at allow

To test login on other terminal from user vinita and schedule job from at commands

at permit vinita

Now login on other terminal from nikita and schedule job form at commands

deny nikita

Task scheduling with cron command

The cron system is basically a smart alarm clock. When the alarm sounds, Linux runs the commands of your choice automatically. You can set the alarm clock to run at all sorts of regular time intervals.

Linux installs the cron daemon (crond) by default. It's configured to check the /var/spool/cron directory for jobs by user. It also checks for scheduled jobs for the computer under /etc/ crontab and in the /etc/cron.d directory.

Login form root and check system date, and run crontab command to schedule job

date crontab e

In open file you can schedule job. There are 6 field in this file 5 for time and one for commands.

FieldValue
minute0–59
hourBased on a 24-hour clock; for example, 23 = 11 P.M.
day of month1–31
month1–12, or jan, feb, mar, etc.
day of week0–7; where 0 and 7 are both Sunday; or sun, mon, tue, etc.
commandThe command you want to run

If you see an asterisk in any column, cron runs that command for all possible values of that column. For example, an * in the minute field means that the command is run every minute during the specified hour(s). Consider another example, as shown here:

 11 5 3 5 * ls 

This line runs the ls command every May 3 at 5:11 A.M. The asterisk in the day of week column simply means that it does not matter what day of the week it is; crontab still runs the ls command at the specified time.

For example time in my system is 21:46 and date is 14 Feb Sunday. ( See image above). Now I will set cron to display the output of ls commands on tty2 at 21:50

#crontab -e
set corn
save file and quit

In real life you do not have to restart cron every time you make a change because cron always checks for changes, But so far exams concern we suggest you to restart cron whenever you made change.

service cornd restart

Wait for four minute and on 21:50 you will get the output of ls command on tty2

output of cron

Setting Up cron for Users

Each user can use the crontab command to create and manage cron jobs for their own accounts. There are four switches associated with the crontab command:

  • -u user Allows the root user to edit the crontab of another specific user.
  • -l Lists the current entries in the crontab file.
  • -r Removes cron entries.
  • -e Edits an existing crontab entry. By default, crontab uses vi.

If you want to set up cron entries on your own account, start with the crontab -e command.

Securing cron daemon

You may not want everyone to be able to run a job in the middle of the night. If your system have important security data, someone may download important data or worse, and it could be done before you discover the security violations.

Two files are used to control the behavior of crond daemons

  • /etc/cron.allow If present then only users those name are in this file can use crond daemons
  • /etc/cron.deny If present then only user those name are in this file will not be able to use crond daemons apart from these user all other can use cron daemons
  • If both files are not present then only root can access cron daemons

For example create two user Vinita and nikita

useradd

These files are formatted as one line per user; add user nikita to cron.allow

cron allow

To test login on other terminal from user nikita and schedule job from cron commands

crontab
crontab
crontab

Now login on other terminal from vinita and schedule job form cron commands

cron deny

ComputerNetworkingNotes RHCE 7 Study Guide Task Scheduling in Linux with cron and at command