How to schedule a task with the at command on Linux – LLODO


Automation is great, but not everyone knows how to automate tasks. However, being able to schedule a task for the computer at a specific time is equally as good. So in this article, I will show you how to schedule tasks using the at command in Linux.

How to schedule a task with the at command on Linux

What is the at command?

at is a Linux command that allows you to schedule jobs for a specific date and time. It’s automation made easy from Terminal.

How to install

On Linux, the at command may already be installed by default. You can verify it is installed with the command at -V:

$ at -V
at version x.y.z

If you can’t find the at command, most modern Linux distributions provide an at command installation package for you.

You may also need to start the at daemon, called atd. On most Linux systems, you use the systemctl command to enable services and allow them to automatically start from now on:

$ sudo systemctl enable --now atd

How to schedule an interactive task with the at . command

You first need to create an event at some point and then you specify what you want to happen.

For example, try scheduling by creating a task for the next few minutes. Just do a simple task. To exit the terminal, press Ctrl + D on your keyboard.

$ at 11:20 AM
warning: commands will be executed using /bin/sh
at> echo "hello world" > ~/at-test.txt
at> <EOT>
job 3 at Mon Jul 26 11:20:00 2021

As you can see, at uses intuitive and natural time definitions. You don’t need to know the 24 hours or convert to UTC. In general, you can use any time notation such as noon, 1:30 PM, 13:37,… to describe the moment you want.

Wait a few minutes and then see if the word “hello world” has been written to the file at-text.txt using the cat command.

$ cat ~/at-test.txt
hello world

How to schedule a task with the at . command

You don’t need to write dance on terminal to schedule jobs with at. You can use the echo or printf command instead. In this example, I will use now to delay that task for 1 minute.

$ echo "echo 'hello again' >> ~/at-test.txt" | at now +1 minute

After a minute, you need to confirm the task has been completed with the cat command.

$ cat ~/at-test.txt
hello world
hello again

Time options

The at command is pretty easy to manipulate with time options. You can choose between several formats, depending on which is most convenient for you:

  • YYMMDDhhmm[.ss] (short for year, month, day, hour, minute and second)
  • CCYYMMDDhhmm[.ss] (full year, month, day, hour, minute and second)
  • now
  • midnight
  • noon
  • teatime (4 PM)
  • AM
  • PM

Times and dates can be absolute, or add a plus sign (+) to make them relative to now.

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

Date and time syntax

The at command is less comfortable when dealing with dates and times. The time must be entered first, then the date, although that date defaults to the current date and only scheduled tasks for some future date.

Eg:

$ echo "rsync -av /home/tux [email protected]:/home/tux/" | at 3:30 AM tomorrow
$ echo "/opt/batch.sh ~/Pictures" | at 3:30 AM 08/01/2022
$ echo "echo hello" | at now + 3 days

View task queue

You can view your scheduled jobs on Linux with the atq command:

$ atq
10 Thu Jul 29 12:19:00 2021 a tux
9  Tue Jul 27 03:30:00 2021 a tux
7  Tue Jul 27 00:00:00 2021 a tux

To remove a job from the queue, use the atrm command. To remove job #7:

$ atrm 7
$ atq
10 Thu Jul 29 12:19:00 2021 a tux
9  Tue Jul 27 03:30:00 2021 a tux

In addition, you can also see 10 more dangerous Linux commands you absolutely should not try here.



Link Hoc va de thi 2021

Chuyển đến thanh công cụ