Log In | Register | October 19, 2019

Share
 

Linux - October 22, 2011

Linux `at` and `batch` one-time schedulers

Although CronTabs are useful, sometimes you just want to run a command at a given time once and not have it reoccurring. The commands `at` and `batch` can do just that.

The `at` command can be used to run a command based on a specific time. Lets say you wanted to run a command 1 day in advance or even 1 hour from your current time, `at` would be the tool to use.

`batch` on the other hand runs commands based on the systems load average. By default it will run when the load average is less then 0.8.

Installation

Before using the command we need to make sure the `at` package is installed. We can do this as follows:

rpm –qa | grep ^at

This will display a list of packages that start with the word at. If you do not have the at package installed you can install it by using the rpm command or yum.

yum install at

Installing the at package also installed the batch binary.

`at` Usage

Lets say you wanted to schedule at to run a specific command at a future time. You could do this by specifying the at command followed by a future time: at

at 12:30

Then press enter, and you will be prompted to enter a command. For this example, I’m going to output the current date and time to a file in the root directory.

date >> /root/datelog.txt

Then you could press CTRL-D to issue the one time cron, or you could press enter to issue another command for the same scheduled task.

You could also specify a number of days to wait until running the given command by doing the following:

at now + 1 day

Then you will be prompted for your command. After you type the command you want to issue, press CTRL-D to set your schedule.

To view a list of items in the at queue you can type the following:

atq

This would list all of the tasks you have scheduled via at. You will also see a list of numbers in front of each task. Each number specifies the job number for that task. You can use that job number in order to remove the scheduled task from running.

atrm 5

For my example, the job number we scheduled was set to 5. In order to remove the scheduled task you type the command atrm followed by the job number.

`batch` Usage

The batch utility does not allow any command line options. In order to use batch, you simply just type the word batch.

batch

Press enter and then type the command you wish to issue.

uptime >> /root/uptime.txt

After issuing your command press CTRL-D to apply it. It will then be run the next time your server load is less then 0.8.

Let me know if you have any questions below.

Post By: | FavoriteLoadingAdd to favorites

0 Comments

Leave a Comment



Need Help? Ask a Question

Ask anything you want from how to questions to debug. We're here to help.

You Must Be Logged In To Post A Question.

Log In or Register