What's a simple way to run a command, say, 8 hours from now? I can think of this way:
nohup bash -c "sleep 28800 ; ./mycommand.sh" &
Is there a more "proper" way?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityYou can use the at
command. The at
execute commands at a later time. The at
utility shall read commands from standard input and group them together as an at-job, to be executed at a later time.
Usually, at
is installed by default in Ubuntu, but if your release doesn't include it, install via:
sudo apt-get install at
For more information, options, examples, and others, see the manpage in man 1 at
.
Example of a relative time specification (note the space between +
and the duration):
at now + 8 hours -f ~/myscript.sh
You can also use convenient shorthands, like tomorrow
or noon
, as in
echo "tweet fore" | at teatime
Note: This will run the command to the left of the pipe immediately - and its output (which is piped to at
) will be run by at
at the scheduled time. So, the above command schedules tweet fore
to be run at teatime.
The example also demonstrates how you can pipe actions into at
. at -c
is the way you can examine scheduled actions, which you can conveniently list with their number, as with:
at -c 3
at 8:00
to run the command at an absolute time, and batch
for "when it looks like the computer is idle"
Aug 30, 2013 at 18:09
teatime
is at 4pm. For some reasons it's not mentioned in manpages.ubuntu.com/manpages/raring/man1/at.1posix.html but it is in man at
and here manpages.ubuntu.com/manpages/raring/en/man1/at.1.html.
at
is its lowest resolution is 1 minute. You can schedule now
or +1 minute
but nothing between. Can be a bit annoying for trying to just fork a job.
Aug 7, 2014 at 14:07
echo "/command/to/run" | at teatime
produces the desired outcome
Yes, you can set a cron job.
For example if now the time is 14:39:00 and today is friday, 30 august, you can add the following cron job (to be executed after 8 hours) in your crontab file using crontab -e
command:
39 22 30 8 5 /path/to/mycommand.sh
More about:
crontab
until you remove it. A year later, when you have forgotten who put it there and why, you will be left scratching your head wondering what to do with the command.
Sep 4, 2013 at 16:05
Use the Gnome-based GUI for cron
, at
, and the like:
The introduction of the CronHowto suggests using the gnome-schedule
gui, which is much nicer than typing all the garbage into the terminal (esp. for "average" Ubuntu users who are not "power" *nix/bsd users.)
Run it by using the Unity Dash (or other applications menu) to look for Scheduled Tasks or running gnome-schedule
.
On Gnome-based Ubuntu systems Gnome Scheduled tasks tool (from the gnome-schedule package) in Applications --> System Tools provides a graphical interface with prompting for using Cron. The project website is at http://gnome-schedule.sourceforge.net/; the software is installable from the Software Center or by typing
sudo apt-get install gnome-schedule
in a terminal.
Using gnome-schedule
, for a script in your home directory, a new "at" command would be set up using this type of window:
Check the output of date
in a loop. This is a quick and dirty way to do this, like if you can't use at
, cron
, or other tools.
Say you want your script to run at noon:
until [[ $(date +%H:%M) == 12:00 ]]; do
sleep 30
done
./mycommand.sh
Say you want it to run tomorrow at noon (today is Nov 24 for me):
until [[ $(date +%d_%H:%M) == "25_12:00" ]]; do
sleep 30
done
./mycommand.sh
Previously, this answer recommended regex, which has some pitfalls, like a lot of special characters. As well it recommended matching against the whole date string, which is more error-prone since it's locale-dependent. For example, I use some French locale formats since I live in Quebec, so if I forget about that and write until [[ $(date) == "25 12:00" ]]
, it will never match because the French ordering is "day month year, time" instead of the English "month day time year".
at
, cron
, or any other real scheduler application. Task scheduling is hard, and not a wheel you should reinvent.
at
.
at
is because its jobs will survive a reboot.
Feb 28, 2017 at 10:25
at
installed and I'm a user without sudo. I think this alternative is better. @dimo414
Dec 8, 2018 at 6:05
If you are running bash on a mac you will run into some difficulties using the at
program. In that case you can improve on your original proposed solution of,
nohup bash -c "sleep 28800 ; ./mycommand.sh" &
with this,
nohup bash -c "sleep $(echo '8 * 60 * 60' | bc) ; ./mycommand.sh" &
which is a more readable form of how long you want to wait. In this example 8
is the number of hours you want to wait before running your script mycommand.sh
.
In order to install gnome-schedule
on Ubuntu 21.04 you can do
$ sudo snap install gnome-schedule --edge --jailmode
nohup
+sleep
? Why is it "improper"?