- 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 do I print contents of at jobs?
- 6 Answers 6
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 do I print contents of at jobs?
I have a Debian box with some jobs scheduled using at . I know I can list the jobs with their times using atq , but is there any way to print out their contents, apart from peeking into /var/spool/cron/atjobs ?
6 Answers 6
at -c jobnumber will list a single job. If you want to see all of them, you might create a script like
#!/bin/bash MAXJOB=$(atq | head -n1 | awk '< print $1; >') for each in $(seq 1 $MAXJOB); do echo "JOB $each"; at -c $each; done
Probably there’s a shorter way to do that, I just popped that out of my head 🙂
at -c $(atq | cut -f 1) or for each in $(atq | cut -f 1) will avoid «Cannot find jobid» errors. (Also, Bash has for ((each=1; each
Building upon previous responses, this lists each job’s line from atq showing job number and scheduled time and then just the command to be run, sorted chronologically (rather than job number):
for j in $(atq | sort -k6,6 -k3,3M -k4,4 -k5,5 |cut -f 1); do atq |grep -P "^$j\t" ;at -c "$j" | tail -n 2; done
48 Fri Mar 10 15:13:00 2017 a root /usr/local/bin/a-command 47 Fri Mar 10 15:14:00 2017 a root /usr/local/bin/another-command
for j in $(atq | cut -f 1); do at -c "$j"; done
You could also look at each one in less in turn, which might be clearer:
for j in $(atq | cut -f 1); do at -c "$j" | less; done
I’ve created command atqc for this («atq with command»). A bash function. Run this on the bash command line (terminal command). Or put it in the ~/.bashrc file to make it available for later:
That works for RHEL7 with at -V version 3.1.13.
Ubuntu 16.04 with at -V version 3.1.18 has a slightly different output format in at -c N , so on my Ubuntu server this works:
Here is an alternative approach, similar to the others in this thread but using awk more:
To do a «dry run» and see what would be executed, simply remove the «| bash» part from the end.
I wanted to have my at jobs listed by execution date plus their corresponding command line. Swap the ## at line get_at_line if you prefer the output separated by blanks instead of columns.
me@PC3540:~$ which atl /home/me/bin/atl me@PC3540:~$ cat /home/me/bin/atl #!/bin/bash PFIX=A00000 get_at_line () < for i in $(at -l | cut -f1 ) do ATLINE=$(at -l | grep ^$i) ATSTATUS=$(echo $ATLINE | tr -s " " | cut -f7 -d" ") ATTIME=$(echo $ATLINE | cut -f2- | cut -f1-6 -d" ") ATISO=$(date -d "$ATTIME" +%Y-%m-%d' '%R) ATCMD=$(at -c $i | sed -n -e '/>/,$p' | grep -v -e '>' ) HEX=$(printf '%x\n' $i) VAL=$(printf "%X\n" $((0x$PFIX+0x$HEX))) VAL=$ echo Job: $(printf "%02d\n" $i) $VAL At: $ATISO $ATSTATUS: $ATCMD done > if $(which at > /dev/null) then if [ ! -z "$(at -l)" ] then get_at_line | sort -k5 -k6 | column -t -s" " -n ##get_at_line | sort -k5 -k6 else echo -e "\n.. no at jobs found for user $USER\n" fi else echo -e "\n.. service at not installed \n" fi