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?

Rama Vadakattu
  • 195
  • 1
  • 1
  • 6

4 Answers4

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.

MikeyB
  • 39,291
  • 10
  • 105
  • 189
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.

Bill Weiss
  • 10,979
  • 3
  • 38
  • 66
  • 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 02: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
CarpeNoctem
  • 2,437
  • 4
  • 23
  • 32
1

Have a look at the at command

Dominik
  • 2,218
  • 14
  • 9