117

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?

Braiam
  • 67,791
  • 32
  • 179
  • 269

6 Answers6

150

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
muru
  • 197,895
  • 55
  • 485
  • 740
Mitch
  • 107,631
  • 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
  • 7
    In case anyone was wondering, teatime is at 4pm. For some reasons it's not mentioned in http://manpages.ubuntu.com/manpages/raring/man1/at.1posix.html but it is in man at and here http://manpages.ubuntu.com/manpages/raring/en/man1/at.1.html. – Dan Sep 06 '13 at 07:53
  • Wow, batch looks like a really useful command (although its mode of operation is weird - no arguments.) – Steve Bennett Sep 09 '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
  • 1
    One nuisance I've discovered with 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. – Steve Bennett Aug 07 '14 at 14:07
  • Can I set the email address to which emails get sent? – starbeamrainbowlabs Nov 01 '14 at 13:09
  • Yes you can. Take a look at this. – Mitch Nov 01 '14 at 13:18
  • 1
    I believe this solution only works if the "command" is a shell script. – AndreKR Apr 11 '15 at 09:33
  • at is capable of describing time in a bunch of different ways ' x| at 4am tomorrow' can be useful. – Andrew Hill Oct 15 '15 at 09:15
  • How to control the pending commands? See the list? Cancel a command? The documentation is so lacking :( – AlikElzin-kilaka Jun 24 '16 at 06:08
  • http://www.brunolinux.com/02-The_Terminal/The_at_Command.html – AlikElzin-kilaka Jun 24 '16 at 06:13
  • 3
    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
  • at is very limited because it runs the command only in sh so none of your bash aliases or bash functions will work. – deanresin Jan 19 '21 at 02:14
  • how can i specify the command directly instead of the file? – Eular Jun 12 '21 at 14:02
  • Just an addition, on the Mac (perhaps on BSD as well), the syntax is at -f <file> <time>. File first, then time. – Vihung Jan 10 '24 at 10:12
17

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:

Radu Rădeanu
  • 169,590
  • 4
    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
  • @SteveBennett As in my example, no: 5 means friday, 8 means august, 30, means today. – Radu Rădeanu Aug 30 '13 at 12:58
  • 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
  • @OlivierDulac The current version of Ubuntu will be probably EOF at that time (30 august 2019) :) – Radu Rădeanu Aug 30 '13 at 13:13
  • 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
  • @RaduRădeanu yes, but the crontab itself doesn't get cleared does it? It's just messy. – Steve Bennett Sep 02 '13 at 12:54
  • @SteveBennett I agree. cron is an overkill for a one-off run. – Paddy Landau Sep 03 '13 at 09:53
  • 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 04 '13 at 01:20
  • 3
    @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 04 '13 at 16:05
  • Entering a comment into crontab explaining the purpose of the command would reduce the amount of head scratching. I put a blank line at the end of my crontab, then add "#Description of my command" followed by the crontab entry for the command. I do think the correct answer to the original poster is to use "at" instead of a crontab entry. – user215175 Jan 03 '18 at 16:52
  • In most commercial environments, setting up cron jobs require admin privileges. Most programmers would skip this step (interacting with people). They would rather write their own custom schedulers, – Thyag Oct 17 '18 at 19:12
14

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

user29020
  • 533
12

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".

wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • 13
    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 08:39
  • 1
    @dimo414 What's wrong with my method? – wjandrea Jan 20 '17 at 17:20
  • 11
    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
  • 8
    Another reason to use at is because its jobs will survive a reboot. – manatwork Feb 28 '17 at 10:25
  • 1
    My server doesn't have at installed and I'm a user without sudo. I think this alternative is better. @dimo414 – yihui.dev Dec 08 '18 at 06:05
  • 1
    @Eli your server certainly does have cron or another task scheduling tool. – dimo414 Dec 09 '18 at 08:18
  • 1
    This'll do the trick. I have no sudo and can't access cron or at either. I'm not staying up any later to schedule an experiment that I want run by tomorrow. I'll bet there are better ways to do this, but they're all eating into my sleep schedule at this point. – Aydin Gerek Nov 19 '20 at 20:01
  • I upvote this solution because I agree with @yihui.dev and this solution is not fully busy-waiting as sleep 30 shows(it is actually polling timer with semi-waiting). I know cron(8) & at(1) should be better if I can use it, but this solution could be better in some situations (e.g. need to merge the output to /proc/1/fd/1, or some auditing system wrongly detect crond as security issue -- in my case;-( ) – Fumisky Wells Aug 06 '23 at 20:25
3

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.

Max
  • 131
  • Why you have chosen to use bc instead of shell arithmetic expansion? i.e. nohup bash -c "sleep $((86060)) ; ./mycommand.sh" & – diabolusss May 19 '23 at 20:05
0

In order to install gnome-schedule on Ubuntu 21.04 you can do

$ sudo snap install gnome-schedule --edge --jailmode
paradox
  • 163