19

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?

4 Answers 4

28

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.

6
  • 2
    +1 for mentioning @reboot.
    – Alex Holst
    Feb 10, 2010 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, 2010 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? Feb 11, 2010 at 8:11
  • 1
    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, 2010 at 14:44
  • Good answer. I always forget about @reboot.
    – Bill Weiss
    Feb 11, 2010 at 17:29
9

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.

5
  • 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, 2010 at 17:31
  • Joe: good call.
    – Bill Weiss
    Feb 10, 2010 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? Feb 10, 2010 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, 2010 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, 2010 at 2:16
4

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
1

Have a look at the at command

4
  • what will happen when the systems gets rebooted? Do i again need to enter "at command" ? Feb 10, 2010 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, 2010 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. Feb 10, 2010 at 19:47
  • ok i will do more research on this. Feb 11, 2010 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .