Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a job which runs forever the moment it starts. So i want to start it only once after entering it into "crontab -e" file and saving it (or) whenever reboot happens.

How to achieve this?

share|improve this question
add comment

4 Answers 4

up vote 13 down vote accepted

If you want a command to run once at a later date, use the at command.

If you want a command to be run once at system boot, the correct solution is to use either:

  • system RC scripts (/etc/rc.local)
  • crontab with the @reboot special prefix (see manpage)

The latter is the only option for a non-root user.

share|improve this answer
2  
+1 for mentioning @reboot. –  Alex Holst Feb 10 '10 at 19:02
    
I would always prefer using /etc/rc.local if possible... I suspect that @reboot actually runs commands on crond startup, not system boot. Subtle but possible difference. –  MikeyB Feb 11 '10 at 2:14
    
suppose i entered @reboot "cmd ............." into the crontab file and saved it. Will this command start now? (or) Does it start only after reboot? –  Rama Vadakattu Feb 11 '10 at 8:11
    
Try it and see :) What you'll probably need to do is put it into crontab, then schedule for immediate execution using at now –  MikeyB Feb 11 '10 at 14:44
    
Good answer. I always forget about @reboot. –  Bill Weiss Feb 11 '10 at 17:29
show 1 more comment

You could use at(1) to run a job at some point in the future. However, if you want to run something on boot and it'll stay running by itself, maybe you want an entry in /etc/init.d/ for it? That would let you start it on boot.

share|improve this answer
1  
You could also use rc.local. The commands in /etc/rc.local "will be executed after all the other init scripts. You can put your own initialization stuff in rc.local if you don't want to do the full Sys V style init stuff." –  Joe Feb 10 '10 at 17:31
    
Joe: good call. –  Bill Weiss Feb 10 '10 at 17:34
    
How to stop the process which started by at command? (i know kill process id after seeing list of process ids by using "ps command" does this work for "at command" also).Is creating custom scripts easy? –  Rama Vadakattu Feb 10 '10 at 17:43
    
Yes to both. All processes can be stopped using kill (assuming you've got the permissions to do so, which you do for your own processes). Making an init script is really easy. Look in /etc/init.d/ for a script that isn't very long and model yours after it, or google for making init scripts (your distribution). –  Bill Weiss Feb 10 '10 at 18:47
1  
A lot of distros include a skeleton script (/etc/init.d/skeleton on openSuSE) that you can adapt for your use. –  MikeyB Feb 11 '10 at 2:16
show 1 more comment

A job that runs only once and runs forever till you tell it to die is called a "daemon". They are normally started via init scripts in /etc/init.d/.

If your job ends at some point and can be considered a one-off kind of job, then you may want to look at the at command. For instance if I wanted to run the find command at 10PM tonight and only this once I would do:

$ at 10PM
at> find /root > /root/find_results.list
at> <EOT>
job 3 at 2010-02-10 22:00
share|improve this answer
add comment

Have a look at the at command

share|improve this answer
    
what will happen when the systems gets rebooted? Do i again need to enter "at command" ? –  Rama Vadakattu Feb 10 '10 at 17:32
    
The at command entries won't be lost across reboots. ibm.com/developerworks/linux/library/l-job-scheduling.html is an interesting introduction to job scheduling –  Dominik Feb 10 '10 at 17:59
    
@Rama Vadakattu: "The at command entries won't be lost across reboots." ...that haven't been executed yet. See MikeyB's answer. –  Dennis Williamson Feb 10 '10 at 19:47
    
ok i will do more research on this. –  Rama Vadakattu Feb 11 '10 at 8:12
add comment

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.