Scheduling commands in IT environments are important. There are tools like
batch . We will look all of them in this tutorial.
Schedule Job At
While scheduling jobs we will use
at command. At command have very causal time specification. We will look varying time specification in the next example in detail. Now we simply schedule job.
$ at 20:58
We will run
mkdir test command at
20:58 . In order to accomplish this we run following steps.
at 20:58will run following commands at
mkdir testis the command we want to run specified time
- We can give more than one command for each line of prompt but we can end the commands with
CTRL+dwhich will give an output like
at command supports a lot of different type of time specification. Here some of them.
noonwill run 12:00 PM
midnightwill run 12:00 AM
teatimewill run 4:00 PM
tomorrowwill run next day same time
next weekwill run next week
friwill run Friday
next mondaywill run next monday
now + 1 hourwill run after 1 hour
now + 3 weekswill run after 3 weeks
Schedule Job With Relative Time
As we have seen a lot of different time expressions in previous example we can use them in our examples. In this example we will set time relatively which means we will set time according to current time. To specify relative time we will use
now time specifiers with
count . In the example we will set command to run after 15 minutes.
$ at now + 15 minutes
We can see from screenshot that the command is set to run at
21:31 which is after 15 minutes from now. We can see that this job is numbered
View All The Schedules Jobs
In a busy system there may be a lot of scheduled jobs. Listing them can be done with
We can see that there is only one job in the list. This job is numbered as
2 . This job will e run at
Thu Mar 9 21:31:00 . The job is created by user
Remove/Delete A Scheduled Jobs
We can also remove jobs that not run. We need the job ID which can be listed with atq . We will provide job ID to the
atrm command like below. We will remove job ID
$ atrm 2
Execute Jobs According to Load Average
We can set for different times for our jobs. But some times just setting time is not enough or not suitable for our situation. There is an alternative way to schedule command We can use
batch command to run commands if load average is less than
1.5 . In the example we will run
echo "Run reports"
Reload Jobs From A File
Another useful feature is reading commands from a file. As we know commands are read from an interactive shell. We can provide commands to be run at scheduled time. We will use
-f option and the command file name to use this feature. In the example we will read commands file named
myjobs.txt and run them after 2 days.
$ at -f myjobs.txt now + 2 days
Allow and Deny User To Schedule Job
By default all users in Linux system can create scheduled jobs. This may create some problems or security issues or simply it is unwanted situation. We can set rules to allow or deny some user from scheduling commands.
There is two configuration file to configure this.
at.allowis used to set allowed users
at.denyis used to set denied users