tags:

views:

387

answers:

8

What is the easiest way in Linux to have a script run on startup and another on shutdown?

+3  A: 

Create the script in /etc/init.d and run chkconfig myscript on. See the other scripts in /etc/init.d for examples.

As others have pointed out, this is on a Red Hat based distribution.

Brian L
-1: chkconfig does not exist in my Linux. You might want to mention what distribution you’re using.
Bombe
This is distribution dependant. You can't assume chkconfig is there.
Adam Lassek
Just to add to this, chkconfig relies on a very specific set of comments near the top of the script to tell it which runlevels to start and stop at. Also, it's mostly found in RedHat-based distros.
Paul Tomblin
Command is update-rc.d in debian based distros. Scripts still in /etc/init.d (standard?)
skinp
Not all distros use sysv-style init. My distro of choice (Slackware) uses BSD-style with some borrowed sysv-isms, and I think there are a couple of more obscure set-ups too (ISTR hearing that Gentoo does its own thing, but I'm not familiar with it).
Adam Jaskiewicz
+6  A: 

SysV style init.

See the introductory article http://www.linux.com/feature/114107

Most distros have their own front-end to manage runlevels. Unfortunately there is no standard tool.

For example I use OpenSUSE, where it can by managed by YaST.

vartec
Not all distros use SysV, either.
Adam Jaskiewicz
Most do. InitNG and Upstart are still experimental.
vartec
I wasn't talking about InitNG and Upstart, I was talking about the somewhat modified BSD-style init in Slackware and some related distros.
Adam Jaskiewicz
On Slackware for example, you would add a /etc/rc.d/rc.whatever script to stop/start your service, then edit the runlevels' startup script to run your script. You'd want to tell runlevel 0 (halt) to stop the service, and runlevel 3 (multiuser) to start the service.
Adam Jaskiewicz
I see there is bit of confusion. SysV init consist in runlevels defined in /etc/inittab. Slackware of course uses that. Now, what you're talking about is rc.d init scripts, which indeed are called SysV or BSD style, so it might be confusing. In fact I think most distros use hybrid of both.
vartec
A: 

This is very distribution-dependent, and also varies with stuff like runlevels, and exactly when during start-up you want your script to run.

But poking around in /etc is probably a good way to figure out how to do it for your particular machine.

unwind
A: 

The easiest way to make a script run on startup is to add a @reboot entry in root's crontab. (I don't know of any @shutdown rule):

@reboot /path/to/script <arguments>

For debian systems (ubuntu included), the "correct" way is to make an init script in /etc/init.d based on the skeleton file in that directory. Use update-rc.d to make it active (read the man for usage).

skinp
A: 

If your application does not support daemonizing, pid file handling, etc, you can use the command start-stop-daemon to provide these features for you in your init script.

Ali A
+3  A: 

In Debian or Ubuntu, copy /etc/init.d/skeleton, an example script for starting/stopping daemons, and edit it to your liking, and make it executable:

cp /etc/init.d/skeleton /etc/init.d/mythingy
pico /etc/init.d/mythingy
chmod 755 /etc/init.d/mythingy

That should provide a way for you to start and stop a given program/script. You may want to test your script to make sure that it works as expected:

/etc/init.d/mythingy start
/etc/init.d/mythingy stop

After doing this, use the update-rcd command to make it run at startup/shutdown:

update-rcd mythingy defaults

Unless you know what you are doing, using the defaults is perfectly fine. That should be all you need to do!

You might find these resources helpful:

  1. Debian SysV Init
  2. Ubuntu Init
vezult
+1  A: 

Ubuntu has /etc/rc.local, a script which you can edit to call anything you need to do on startup.

Gentoo has /etc/conf.d/local.start and /etc/conf.d/local.stop

Must be something similar under Redhat/fedora.

Jim T
A: 

Since you haven't specified which distro, all you can rely on is the information from LSB about init scripts.

Ronny Vindenes