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?

  • 1
    Is there something wrong with nohup + sleep? Why is it "improper"? – cprn Nov 24 '15 at 13:32

You 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
| improve this answer | |
  • 7
    In addition, there is at 8:00 to run the command at an absolute time, and batch for "when it looks like the computer is idle" – Simon Richter Aug 30 '13 at 18:09
  • 4
    In case anyone was wondering, 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. – Dan Sep 6 '13 at 7:53
  • Wow, batch looks like a really useful command (although its mode of operation is weird - no arguments.) – Steve Bennett Sep 9 '13 at 12:34
  • 3
    Maybe we can mention that "at" utility is not installed by default and if anyone else want to get it simply install the "at" package? Edit: Anyway, i simply edited the answer (also fixed the manpage link). – heartsmagic Feb 12 '14 at 10:11
  • 2
    about the whole piping-into-at running now instead of later issue, echo "/command/to/run" | at teatime produces the desired outcome – rmanna Jan 24 '18 at 10:40

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:

enter image description here

| improve this answer | |

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:

| improve this answer | |
  • 3
    Ok, yes, I should have mentioned I know about Cron jobs. This is even messier because then it sits around in the crontab indefinitely, right? – Steve Bennett Aug 30 '13 at 12:55
  • 4
    @RaduRădeanu: with cron, careful that it could fire again in a few years (when there is another 30th of August occuring on a friday) ... – Olivier Dulac Aug 30 '13 at 13:12
  • 2
    This is a clever hack. However 'at' is a much better way. What would happen if the computer was turned off at 14:39? – emory Aug 30 '13 at 14:47
  • 1
    Err that's still not my point. My point is that the text you entered will (I think) literally still be sitting in your crontab after the event, creating another cleanup task for you. – Steve Bennett Sep 4 '13 at 1:20
  • 2
    @SteveBennett Yes, you are correct. The command will remain in your 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. – Paddy Landau Sep 4 '13 at 16:05

I figured out a quick and dirty way to do this before I found out at was a thing.

Say you want your script to run at noon:

until [[ "$(date)" =~ "12:00:" ]]; do
    sleep 10

Say you want it to run tomorrow at noon (today is Nov 24 for me):

until [[ "$(date)" =~ "25 12:00:" ]]; do 
    sleep 30

Update: Slightly better method: only test the numbers you care about, using date +FORMAT:

until [[ $(date +%H:%M) == 21:06 ]]; do
| improve this answer | |
  • 12
    To the two people who've upvoted this answer, please use at, cron, or any other real scheduler application. Task scheduling is hard, and not a wheel you should reinvent. – dimo414 Jan 20 '17 at 8:39
  • 1
    @dimo414 What's wrong with my method? – wjandrea Jan 20 '17 at 17:20
  • 10
    At a minimum, it's busy-waiting, which is wasteful. Worse, it's not guaranteed to be correct; it's perfectly possible (if unlikely) for the conditional to be missed. Basing your scheduler on regular expressions is also highly error-prone. It might work well-enough, but has no advantages over at. – dimo414 Jan 20 '17 at 17:36
  • 7
    Another reason to use at is because its jobs will survive a reboot. – manatwork Feb 28 '17 at 10:25
  • 1
    @Eli your server certainly does have cron or another task scheduling tool. – dimo414 Dec 9 '18 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.