Linux Commands

Linux at Command: A Convenient Tool for Scheduled Jobs

As a Linux system administrator, we always look for ways to automate repetitive tasks and save time. Linux provides you with the at command for scheduling one-time jobs to run at a specified time.

In this article, we’ll explore the at command and how to use it to streamline your system administration tasks.

The content of this article includes:

Linux at Command

The at command in Linux is a powerful tool for scheduling tasks to execute them at a certain defined time. It allows users to specify the exact date and time when a command should be run. The at command allows users to manage different tasks automatically.

The at command also includes the

  • atq Command
  • atrm Command

The “atq” is used to display pending tasks for a specific user, while root users can view all tasks. The output format shows the task ID, date, hour, queue, and username.

On the other hand, “atrm” is used to delete tasks using their job number.

These commands provide additional functionality and control for managing scheduled tasks with the at command in Linux.

Installing at Command in Linux

The Linux at command might not be by default installed in your system. To install at on Linux, use the below-given command:

sudo apt install at

Understanding the at command

The at command is used to schedule jobs to run at a specific time in the future, without the need for a continuous process running in the background. The command works by adding jobs to a queue, which are then executed at the designated time.

The syntax of the at command is as follows:

at [-f filename] [-m] [-q queue] [-v] [time]

Where:

  • f filename: Specifies a file containing the commands to run
  • m: Sends email notification when the job is completed
  • q queue: Specifies the job queue to use
  • v: Verbose output
  • time: The time at which to run the job

You can schedule jobs using a variety of time formats, including absolute times, relative times, and times specified as minutes past the hour.

Absolute time expressions include:

  • YYMMDDhhmm[s]: This format specifies the abbreviated year, month day, hour, minutes and also seconds option.
  • CCYYMMDDhhmm[s]: This format includes the complete year, month day, hour, minutes and seconds.
  • now: This specifies the current time or immediate execution of at command.
  • midnight: This equals to 00:00 AM.
  • noon: This equals to 12:00 PM.
  • teatime: This time is taken as 4 PM.
  • AM: Time before noon or 12:00 PM.
  • PM: Time after noon or 12:00 PM.
  • today: This specifies the current day.
  • tomorrow: This specifies the day after today.

For example, the given below command will print “Hello” at 4PM:

echo "hello" | at 4PM

To specify a relative time expression using at command, we have to specify the command by adding the “+” plus sign in any one of these:

  • minutes
  • hours
  • days
  • weeks
  • months
  • years

For example, to schedule a task at 5 minutes after current time, use:

echo "hello" | at now +5 minutes

Schedule a Bash Script Using at Command

Using the at command is straightforward. Simply enter the command followed by the time at which you want the job to run and the command you want to execute.

For example, to run a script, follow the below-given syntax:

echo "sh file.sh" | at time

Note: I have created a bash script that will append the text “Hello World” inside a file “myfile.txt” whenever it’s executed.

echo "sh demo.sh" | at 4:24 PM

We can verify the content inside the file using the cat command to ensure the script is run successfully at the given time.

Interactively Scheduling a Job Using at Command

Using the at command in Linux, users can interactively schedule tasks by specifying the desired execution time and entering the corresponding commands at the prompt. This process is like creating events in a calendaring app.

For example, we can create a memo text and schedule it like a remainder on a specific time. Run the following command to schedule a memo that adds “Linuxhint” in a text file at specified time:

at 11:21 AM

After executing the above command, add the following command to add a text “Linuxhint” inside a file.

at> echo "Linuxhint" > ~/at-test.txt

To exit the at prompt, users can press CTRL+D on their keyboard.

Note: The above command creates a text file and appends the text inside the file.

After the task has run, one can verify its execution by using the cat command on the corresponding file.

cat ~/at-test.txt

Scheduling a Job with at

The at command in Linux allows users to schedule tasks by piping commands to it using echo without using the interactive prompt.

For example, users can specify the delay time in minutes and the “now” notation to schedule a task, as shown in the following example:

echo "echo 'Welcome to Linuxhint' >> ~/at-simple.txt" | at now +1 minute

This command schedules a task to run 1 minute from the current time and appends the text “Welcome to Linuxhint” inside the file when executed.

Using this method, we can schedule tasks without the need for interactive input.

After one minute, run the cat command to verify the schedule execution:

cat ~/at-simple.txt

List all Running Jobs

To list all running jobs, you can run:

at -l

Or:

atq

All running scheduled jobs will be listed with their Job ID.

Removing a Schedule Job Using at Command

To remove a scheduled job, we must know the Job ID of a running process. Following syntax will be followed to remove a scheduled job.

atrm [Job ID]

For example, to remove the current schedule jobs having job ID 4, use:

atrm 4

Remove the at Command

To remove the at command from Linux system, use:

sudo apt autoremove at

Conclusion

The at command is a useful utility for scheduling jobs on Linux systems. It allows users to automate tasks, which can save time and increase productivity. The at command allows users to specify the exact time and date for a job to be executed, making it a powerful tool for managing complex tasks. For a detailed description of at command read the above-mentioned guidelines.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.