- At Command in Linux for One-Time Jobs Scheduling
- Understanding the ‘at’ Command
- Installing the ‘at’ Command
- Syntax and Options
- Scheduling a One-Time Job
- Listing and Managing Scheduled Jobs
- Best Practices
- At Command Examples
- Conclusion
- Related Posts
- How to Ignore SSL Certificate Check with Wget
- How to Ignore SSL Certificate Check with Curl
- The `ls` Command in Linux with Practical Examples
- 29 Comments
- How to schedule tasks using the Linux ‘at’ command
- Install the ‘at’ utility
- Training & certification
- When to use the ‘at’ utility
- Schedule tasks with the ‘at’ command
- Use flags with the ‘at’ command
At Command in Linux for One-Time Jobs Scheduling
Scheduling tasks in a Linux environment is a common requirement for system administrators and developers. While the cron command is often used for recurring tasks, the “at” command is a powerful tool for scheduling one-time jobs in Linux. This article will provide an in-depth look at the “at” command, its syntax, usage examples, and best practices for managing one-time jobs.
Understanding the ‘at’ Command
The “at” command allows users to schedule a command or script to be executed at a specified time in the future. It is particularly useful for running one-time jobs, such as maintenance tasks, backups, or system updates, without requiring manual intervention. The “at” command reads the commands to be executed from standard input or from a file and schedules them accordingly.
Installing the ‘at’ Command
Most Linux distributions come with the “at” command pre-installed. However, if it is not present on your system, you can install it using the package manager for your distribution.
- For Debian-based distributions, use the following command:
Syntax and Options
The basic syntax of the “at” command is as follows:
- -f : Specifies a file containing the commands to be executed.
- -t : Specifies the time at which to run the commands using a Unix timestamp.
- -m : Sends an email to the user when the job has completed.
- -q : Specifies a queue in which to place the job.
Scheduling a One-Time Job
To schedule a one-time job, simply provide the desired time for execution. The “at” command supports various time formats, such as:
- Relative time: “now + 1 hour” or “now + 30 minutes”
- Absolute time: “2:30 PM” or “15:30”
- Date and time: “10:00 AM tomorrow” or “2023-04-01 18:00”
echo "echo 'Hello, World!' > /tmp/hello_world.txt" | at now + 1 hour
This example schedules a one-time job to create a file containing “Hello, World!” in the /tmp directory after one hour.
You can also schedule the command as below:
at now + 1 hour
echo 'Hello, World!' > /tmp/hello_world.txt
Press CTRL + d to exit from at command terminal.
Listing and Managing Scheduled Jobs
To list all scheduled jobs for the current user, use the “atq” command:
To remove a scheduled job, use the “atrm” command followed by the job ID:
Best Practices
- Always verify that the “at” command is installed and enabled on your system.
- Use descriptive comments in your “at” jobs to make it easier to understand their purpose.
- Test your commands or scripts before scheduling them with the “at” command.
- Remember that the “at” command is designed for one-time jobs. Use the cron command for recurring tasks.
At Command Examples
at 10:00 AM 6/22/2015
at 10:00 AM 6.22.2015
at now + 1 week
at now + 2 weeks
at now + 1 year
at now + 2 years
Conclusion
The “at” command is an essential tool for Linux users who need to schedule one-time jobs. By understanding its syntax and usage, you can effectively automate tasks and improve the efficiency of your workflow. Remember to use best practices when scheduling jobs to ensure that your system runs smoothly and your tasks are completed on time.
Related Posts
How to Ignore SSL Certificate Check with Wget
How to Ignore SSL Certificate Check with Curl
The `ls` Command in Linux with Practical Examples
29 Comments
EXACTLY WHAT I NEEDED.
BTW the recaptcha input is underneath the Submit button in the comments form. Hard to click.
How can we schedule a gtk “graphical” job, for example a simple yad message?
yad –title “Warning” –text “Alarm now, attention” –on-top –borders=25 In my tests, no display is shown at a specified time.
How is it better to schedule installation of upgrades?
For some reason tasks don’t execute. I tried something like: sudo apt-get upgrade -y | at 21:00
please tell me that i want to run a corntab command in every last day of the month.
i am thinking but in some its 30 days and 31 in some and 28 and 29 like that.
59 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && /root/script.sh
here [ “$(date +%d -d tomorrow)” = “01” ] will retrun true if tomorrow is the first day of next month.
I’m using at to run mplayer and stream some radio station, when i want to stop mplayer and use atrm command it doesn’t work. i got only ” Warning: deleting running job” . job vanishes from the atq list, but it still streams music.
HI, I am experiencing an issue. When i am using the format:
command | at time It is not executing the command that I am providing.
Please tell how to get past it.
Great article Rahul!
I just wanted to extend it a bit with some useful additions.
1) You can use -f option to point “at” to the script you need to run:
at -f /path/to/the/script time_spec
2) One can use “at” to start a process in background without nohup, etc. As easy as
at -f /a/command now
or
echo “/a/command” | at now
3) You can use “at” to run a command repeatedly, but unlike cron you can use “at” to run commands with some period between runs, for example after 3 minutes after previous run was completed. This allow you to avoid various checks preventing next run to start before previous is finished.
Moreover you can define this period as random value. Examples:
The script (lets name it /home/user1/at_run.sh):
————————————-
#!/bin/bash
/the/command/you/need
# fixed period between runs
period=3
# or random period. RANDOM is a bash’s random number from 0 to 32767
period=$[ ($RANDOM % 20) + 15 ]
at -f /home/user1/at_run.sh now + $period minutes
————————————-
run /home/user1/at_run.sh and all next runs will be scheduled automatically, so your /the/command/you/need will run repeatedly forever. Sure, you can break the next run with atq/atrm.
Thx Rahul and Sergey. @Sergey, in your last point you are basically using a wrapper with a random pause and re-scheduling of the at job. I think this is also achievable with cron, without the “various checks preventing next run to start before previous is finished”.
You can just replace the last line of your script with this: sleep $(($period * 60)) && exec /home/user1/at_run.sh Then set a crontab for /home/user1/at_run.sh .
Note: the ‘exec’ bash builtin will prevent calling bash recursively (nested bash’s) and spare memory.
You will need to kill the process to end it. If the job is not supposed to stay permanently (boot safe), I would prefer to use an interactive bash inside a ‘screen’ command and just do a loop: while : ; do
/the/command/you/need
sleep 60
done So you can follow in “live” the output of the script.
hello!
i am running centos. when i submit an at nothing happens. i can call it up using “atq #”, however, it doesn’t execute?
thanks for the help.
How to schedule tasks using the Linux ‘at’ command
Time is precious, making time management an appreciated virtue in every aspect of life, whether you’re talking about financials, technology, or any other daily activity.
To manage time, a skilled sysadmin must know when and how to control tasks so that they can be programmatically executed at certain times, whether recurring or a set number of times. You can apply this concept in numerous scenarios, from scheduled backup tasks to collecting system logs periodically.
[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]
You can accomplish task scheduling in numerous ways. In this article, I focus on a straightforward tool available on Linux operating systems to help achieve this goal: the at command. My colleague Seth previously wrote a great article about at , so I recommend you check it out, as well as my article about the cron command, another Linux scheduling tool.
This article aims to be as brief, straightforward, and practical as possible, meaning I won’t be able to explore all available options for the at utility. Let’s get started!
Install the ‘at’ utility
Depending on your Linux distribution, the at utility may or may not be installed by default. You can install it using your distribution’s package manager if it’s not installed. For Red Hat Enterprise Linux (RHEL)-based distributions:
The at package installs other binaries that are used together with the main command:
$ sudo dnf repoquery -l at | grep bin [. ] /usr/bin/at /usr/bin/atq /usr/bin/atrm /usr/bin/batch /usr/sbin/atd /usr/sbin/atrun
The package provides the atd daemon, which you’ll interact with through the use of the at and atq commands:
$ sudo systemctl status atd ● atd.service - Job spooling tools Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2022-11-09 17:47:48 -03; 14min ago Main PID: 1378 (atd) Tasks: 1 (limit: 23643) Memory: 428.0K CGroup: /system.slice/atd.service └─1378 /usr/sbin/atd -f nov 09 17:47:48 demo.example.local systemd[1]: Started Job spooling tools.
Training & certification
When to use the ‘at’ utility
The at and batch ( at -b ) commands read from standard input or a specified file. The at tool allows you to specify that a command will run at a particular time. The batch command will execute commands when the system load levels drop to a specific point. Both commands use the user’s shell.
Install the at package if you need a utility for time-oriented job control. If you have a recurring job that repeats at the same time every day, week, and so forth, use crontab instead.
For more detailed information on all the possible parameters and examples of how to use at , read the man page:
Schedule tasks with the ‘at’ command
I’ll show you how at works. First, I’ll establish the time frame:
$ date fri nov 11 15:21:58 -03 2022
Now, there are some different ways to interact with the at utility. One of them is using its interactive command prompt. Do this by typing at and pressing Enter:
$ date fri nov 11 15:26:21 -03 2022 $ at 15:27 warning: commands will be executed using /bin/sh at> echo "It works!" > /tmp/test.txt at> job 2 at Fri Nov 11 15:27:00 2022
I defined a command to run at 15:27 (3:27 pm GMT-3) after defining my runtime and entering the at command prompt. In this case, the command creates a text file. To exit the command prompt, type Ctrl+D. The at utility then queues the command to execute later, according to the runtime definition. For an overview of the pending jobs for the current user, use the atq or at -l commands:
$ atq 2 Fri Nov 11 15:27:00 2022 a localuser
From the previous output, you can see the following:
- 2 is the unique job number.
- Fri Nov 11 15:27:00 2022 is the execution date and time for the scheduled job.
- a indicates that the job is scheduled with the default queue a.
- localuser is the job owner (and the user the job runs as).
[ Want to test your sysadmin skills? Take a skills assessment today. ]
After waiting for 39 seconds, here’s what happens when the job gets out of the queue and executes:
$ date fri nov 11 15:27:01 -03 2022 $ atq $ cat /tmp/test.txt It works!
Of course, this is not an ideal way of running scheduled jobs; another way is by pipelining the desired command as an input for the at utility. Learn more about that in my article about manipulating files with shell redirection and pipelines. Here is a quick example:
$ rm /tmp/test.txt $ echo "It works while pipelining!" > /tmp/test.txt | at 15:41 warning: commands will be executed using /bin/sh job 3 at Fri Nov 11 15:41:00 2022 $ atq 3 Fri Nov 11 15:41:00 2022 a localuser $ date fri nov 11 15:40:40 -03 2022 $ atq $ date fri nov 11 15:41:01 -03 2022 $ cat /tmp/test.txt It works while pipelining!
Use flags with the ‘at’ command
The most used form of the at utility is specifying an existing script with the -f parameter (or using shell redirection like at so that at can read the inputs from a file instead of a standard input. Check it out:
$ rm /tmp/test.txt $ cat myscript.sh > echo "It works while scripting!" > /tmp/test.txt > EOF $ ls myscript.sh $ sudo chmod +x myscript.sh $ date fri nov 11 15:50:58 -03 2022 $ at 15:52 -f ./myscript.sh warning: commands will be executed using /bin/sh job 4 at Fri Nov 11 15:52:00 2022 $ atq 4 Fri Nov 11 15:52:00 2022 a localuser $ date fri nov 11 15:52:00 -03 2022 $ atq $ cat /tmp/test.txt It works while scripting!