Run a command *later*
How can I run a command later? I'm looking for a CLI way to schedule a command to be run later.
I know that I could create a systemd timer, install it, activate it... That is too much work. I want something like runlater 10min some_cmd
and some_cmd
runs 10 min later.
I don't want to do sleep 600 && some_cmd
because that will block. After I schedule the program to run later, I want to go back to my shell, do some other stuff, close the terminal, and the command should still execute when it's time.
If I schedule a program for 10 mins later and shut down the computer 8 mins later, I don't really care what happens. The program can end up being not run. A notification about it the next time I boot up would be nice, but otherwise I'm thinking of situation where the uptime will not be interrupted.
2 answers
The traditional way on a *nix system to run a one-time command at some specified future time is at
.
$ at 'now + 10 minutes'
at Wed Aug 23 19:00:00 2023
at> date >> ~/current-time
at> ^D
job NNN at Wed Aug 23 19:00:00 2023
$
You can also specify a particular time, which will be interpreted as "next time the real time is X":
$ at '16:00'
at Wed Aug 24 16:00:00 2023
...
or a date and time, although the syntax is a little peculiar:
$ at '22:00 Aug 29'
at Tue Aug 29 22:00:00 2023
...
$ at '22:00 Jul 29'
at Mon Jul 29 22:00:00 2024
...
$ at '22:00 Jul 29 2025'
at Tue Jul 29 22:00:00 2025
...
Normally, after the job finishes executing, any output (stdout or stderr) will be delivered in an email to the account which scheduled the job.
I do believe that most at
implementations will start executing the job as soon as the specified time has occurred. So if the system is powered off, or atd
otherwise isn't running, at the specified time, the job will execute the next time atd
is started.
At least on a typical Debian system, it's also possible to pipe a command into at
to schedule it; for example:
$ echo date | at 'now + 10 minutes'
will run date
after approximately ten minutes.
To see which jobs are queued for execution, use atq
. To see what a job will do when it is executed, use at -c $JOBID
. To delete a job, use atrm $JOBID
.
0 comment threads
Using systemd timers is too much work? Not with systemd-run
!
systemd-run --user --on-active=10min some_cmd
1 comment thread