Scheduling a cronjob is great, but sometimes you only want it to run once. You could add, then remove the job, or write a script that executes once, but there is a better way.
Install at
sudo apt install at
Schedule a script with at
The below examples show two different ways to run a script with at.
echo /script/to/run | at 20:30
at 20:30 -f ./script/to/run
For this, the script needs to exist, and needs executable permissions for your user, sudo chmod +x /script/to/run
, if you need a lil' hint.
The time/date passed 20:30
in this example, can then be passed in a few assorted ways.
at now +5 minutes
at 11:00 today
at 11:00 tomorrow
at 20:30
at 20:30 093024
at 08:30PM Sep 30
at 08:30PM Sep 30 2024
at 08:30PM Sep 30 24
Batch a script
Similar to the regular means of scheduling, except the batch will wait for the system load drops to a set point.
echo /script/to/run | at -b 20:30
You can also run batch
, as follows.
echo /script/to/run | batch 20:30
Check Queue
atq
Running the command above, will show you something akin to...
10 Sun Sep 29 14:14:00 2024 a user
Which broken down shows the ID 10
, the date/time the item will run Sun Sep 29 14:14:00 2024
,
the queue the item is in a
, and the user executing the item user
Remove from Queue
You can remove an item from the queue, with the atrm
command, passing just the ID.
atrm 3
Mail the user
Like cron, at also allows the ability to mail the user about the task once executed. By default, if the command produces an output, at will mail the user, you can override this with:
at -M
This will prevent mail being sent to the user. You can also force a mail sent even if there is no output with:
at -m