Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 40 down vote accepted

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.

at is not installed by default and can be installed via:

sudo apt-get install at

For more information, options, examples, and others see the Ubuntu Manpage Repository

Example:

at now +8 hours -f ~/myscript.sh

You can also use convenient shorthands, like tomorrow or noon, as in

echo "tweet fore" | 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
share|improve this answer
9  
It might be helpful to add a simple usage example that addresses the original poster's question more specifically e.g. at now +8 hours -f ~/myscript.sh –  steeldriver Aug 30 '13 at 12:12
    
@steeldriver Thanks +1 for the example –  Mitch Aug 30 '13 at 12:48
    
Perfect, thank you! –  Steve Bennett Aug 30 '13 at 12:59
3  
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
    
In case anyone was wondering, teamtime 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
show 2 more comments

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:

share|improve this answer
    
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
1  
@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
1  
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
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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