- What is AI networking? How it automates your infrastructure (but faces challenges)
- I traveled with a solar panel that's lighter than a MacBook, and it's my new backpack essential (and now get 23% off for Black Friday)
- Windows 11 24H2 hit by a brand new bug, but there's a workaround
- This Samsung OLED spoiled every other TV for me, and it's $1,400 off for Black Friday
- How to Protect Your Social Media Passwords with Multi-factor Verification | McAfee Blog
Using the at command to schedule tasks on Linux
To schedule a command or script to run at some particular time, the at command is perfect and provides many options for specifying the time you want it to run. It will set the task up to be run whenever you specify, and you can view the scheduled tasks or even change your mind and cancel one of them as you see fit.
The at command differs from cron in that it sets up a command or script to run only once, while cron allows you to set up commands or scripts to be run on a specified schedule – whether every day, once a week, a couple times a month or even just once a year.
at command syntax
Using the at command is relatively easy, though it has a lot of options, particularly on how you specify the time a task should be run. If you specify a time like shown below, the task will be set up to be run the next time you reach 15:27 (3:27 PM), whether that’s today or tomorrow.
$ at 15:27 warning: commands will be executed using /bin/sh at Wed Mar 15 15:27:00 2023 at> echo "It works!" > /tmp/test at> <EOT> job 5 at Wed Mar 15 15:27:00 2023
End the at command by pressing ^D.
In the next example, the command is set to run the following date at the time specified.
$ at 12:34 tomorrow warning: commands will be executed using /bin/sh at Wed Mar 15 12:34:00 2023 at> echo 1234 >> /tmp/wow at> <EOT> job 6 at Wed Mar 15 12:34:00 2023
You can specify the time of day in either of two formats. If you ask to have a command run at 11:11, it will be run at 11:11 AM. To specify it to run at 11:11 PM, use 23:11 or 11:11pm as the time.
Dates can be also specified in any of these formats:
MMDD[CC]YY MM/DD/[CC]YY DD.MM.[CC]YY [CC]YY-MM-DD
You can also specify a task on some particular date by specifying a weekday (e.g., Sunday).
$ at noon Wednesday warning: commands will be executed using /bin/sh at Wed Mar 15 12:00:00 2023 at> date >> /tmp/test at> <EOT> job 7 at Wed Mar 15 12:00:00 2023
Listing at jobs
To list the tasks scheduled with the at command, use the atq (at queue) command.
$ atq 5 Wed Mar 15 15:27:00 2023 a shs 6 Wed Mar 15 12:34:00 2023 a shs 7 Wed Mar 15 12:00:00 2023 a shs
The command shows the jobs waiting to be run, but not the tasks they will be running. The details only include the job numbers, the times they will run and the user who added them. Only root is able to view all of the scheduled tasks. Other users will see only their own.
Deleting at jobs
You can cancel a scheduled job with the atrm command.
$ atrm 5 $ atq 6 Wed Mar 15 12:34:00 2023 a shs 7 Wed Mar 15 12:00:00 2023 a shs
The command below provides a list of commands associated with the at command.
$ sudo dnf repoquery -l at | grep bin [sudo] password for shs: Last metadata expiration check: 1:16:38 ago on Fri 10 Mar 2023 01:40:19 PM EST. /usr/bin/at /usr/bin/atq /usr/bin/atrm /usr/bin/batch /usr/sbin/atd /usr/sbin/atrun /usr/bin/a
Wrap-up
Regularly scheduled tasks should be set up using the cron command, but to run some particular task at some particular time (no repeats), the at command is flexible and easy..
Copyright © 2023 IDG Communications, Inc.