views:

2712

answers:

8

If I want to make something run on startup on Linux, where is the preferred place to put things? I would like a description of each script or file that is executed at boot, and that script or file's purpose as to how you edit it, or what sorts of things are normal to put in it to make run on startup.

A: 

Every script in /etc/profile.d/ is executed at startup for every user. If you have something you want done, the easiest way is to create a script (in bash or perl or whatever), make it executable and place it in that directory. I've used this technique in the past to export certain environment variables (a proxy server, for example), map shared drives, among other things.

Hope that helps you out.

Justin Bennett
+3  A: 

Put your scripts n /etc/rc.d/init.d. There's a standard set of parameters the scripts should take, e.g. "myscript start", "myscript stop".

Now, you need to make sure they're going to run. There are some predefined directories in /etc/rc.d called rc.0, rc.1, etc. "man init" for a full description. You will probably want to add your script to rc.local. So, do a

ln -s /etc/rc.d/init.d/myscript /etc/rc.d/rc.local/99myscript

Now, the next time you reboot, your script will be automatically called with "myscript start", and when you shut down, it will be called with "myscript stop".

The scripts will be executed as root. you can su to another user when you execute a program inside the script.

There's a lot more details, but this should be enough to get you started and write a useful script.

Mark Harrison
A: 

Under which user will the scripts be executed as?

That would be dependent on who the owner of the file is. I would chown them to a regular-level user (read: not root) and then make them executable by everyone (chmod a+x).

Edit: Mark's answer pretty much covers it

Justin Bennett
+3  A: 

I think the /etc/profile.d/ answer is wrong. That directory is used when users log in, not at system startup. Also, it's shell dependent, I believe only for bash. Definitely not for my preferred kornshell.org flavor of ksh.

File execution at boot is specific to your linux distribution, I don't think there's a single answer. For the most part though init runs sysinit which runs all the S* scripts in the runlevel the system just entered. In other words, look in /etc/rc.d, /etc/init.d, /etc/rc3.d, etc. Those scripts are the full on control scripts that allow process management, starting, stopping, etc. At least in the older redhats there was also support for /etc/rc.local, so if you just had a single command to run at boot you could throw it in there without having to build all the infrastructure.

For a more detailed response you'll need to post what distribution you are wanting specific details for.

jj33
A: 

jj33's answer is correct. I was thinking along the lines of scripts running at login, not at startup... my mistake.

Thanks jj33 for the clarification.

Justin Bennett
+6  A: 

If you're adding something that needs to be started up and shut down gracefully, like a server daemon that keeps files open, creating an /etc/init.d-style script and then symlinking it into the /etc/rc?.d directory is the right thing to do.

The name of the file in /etc/rc?.d is actually important; the S* scripts are run on startup in the the order you see when doing a ls -l; if your startup process depends on other things (like networking), be sure it runs after the networking startup script. The numbers after the S are to set the sequence. Things that start last need to be shut down first (before the things they depend upon are shut down), so if your startup entry is named S99mydaemon, symlink it as K01mydaemon to shut it down.

Make note of the different runlevels on the system, 3 (/etc/rc3.d) is multi-user with a text login and 5 is multi-user with with a graphical login.

/etc/rc.local is just an individual script on Red Hat-like systems; add commands that do not require a cleanup process to it. rc.local entries run last.

The startup process goes like this (on System V-like systems):

  • The bootloader finishes it's thing
  • /etc/rc.sysinit is run - don't change this as patches may overwrite it.
  • /etc/inittab is consulted to see what runlevel the system should be put into, and any commands for that runlevel are run. see man inittab if you're curious about this file.
  • /etc/rc?.d (where ? = runlevel) S* scripts are run
  • /etc/rc.local is run
jaredg
A: 

In addition to /etc/rc.d, you can also add a @reboot entry to a crontab. You can even do this as a normal user in your own crontab, which might be handy if you don't have root access to the machine.

Thomas Vander Stichele
+1  A: 

You may also want to be aware that some of the major linux distributions are moving to a replacement init system called upstart. Ubuntu and Fedora have it as their default init system, though upstart currently uses the file structures described by others for almost all the start up scripts.

However it also checks the new location - /etc/event.d/ - for scripts and uses them. So you can just stick scripts there. If you want to, they must be in the upstart format - see the getting started page. And for a load of example scripts, you could look at the uphack project which has been developing startup scripts for all the standard services. You can browse the event.d files on launchpad.net

Hamish Downer