Before learning how to start, stop, and disable services from the command line, it's important to understand Ubuntu's startup process and how Ubuntu determines which programs to run when it starts.
Ubuntu uses System V style init scripts. The init scripts are located in a special directory named /etc/init.d. Even though there may be several init scripts present in /etc/init.d , not all of them are run at boot time. Ubuntu organizes which scripts to run for different circumstances into runlevels; most Linux systems have seven runlevels, ranging from 0 to 6.
A some of these runlevels are set aside for special states in a Linux system:
Runlevel 0 Halts the system.
Runlevel 1 Sets up single-user mode.
Runlevels 2-5 Set up different multi user modes. Although, typically, only one or two of these are used by a distribution.
Runlevel 6 Reboots the system.
Each runlevel has a directory that stores symlinks to certain init scripts in /etc/init.d, which are started when that runlevel is selected and stopped when it is exited. Ubuntu puts these symlinks under /etc/rc.d for example, all runlevel 2 scripts are located in /etc/rc2.d/.
If you look in one of these runlevel directories, you'll notice that many of the symlinks to scripts in /etc/init.d have odd names that begin with an S, K, or D; then a number; and finally the name of the script. Ubuntu defaults to runlevel 2, so here is a partial sample of my /etc/rc2.d directory:
lrwxrwxrwx 1 root root 19 2009-05-13 09:58 S01policykit -> ../init.d/policykit
lrwxrwxrwx 1 root root 15 2009-05-13 09:58 S10acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 14 2009-05-13 09:58 S10apmd -> ../init.d/apmd
lrwxrwxrwx 1 root root 18 2009-05-13 09:58 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root 18 2009-05-13 09:58 S99rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 19 2009-05-13 09:58 S99rmnologin -> ../init.d/rmnologin
lrwxrwxrwx 1 root root 24 2009-05-13 09:58 S99stop-readahead -> ../init.d/stop-readahead
As you can see, this directory is full of symlinks that point to a script in the init.d directory.
The letter at the beginning of each filename tells init when to execute this script. If the script begins with an S, then init starts the script when it goes through the runlevel. If the script begins with a K, then init stops (or kills) the script when it changes to a different runlevel. If the script begins with a D, then that script is disabled for the time being, and init ignores it. init runs the scripts in numerical order, so the numbers in each script let you know in which order they are to be run. This is useful to ensure that dependent services start after the service they are dependent on.
Ubuntu 9.04 uses upstart daemon to manage init scripts. The configuration files for upstart are located in /etc/event.d/ . ( On earlier systems, the start up was managed from /etc/inittab. You will not find this config file in new ubuntu releases.). When ubuntu boots up the default runlevel is determined by a small script in /etc/event.d/rc-default.
Next, upstart loads any system scripts from a special system runlevel directory at /etc/rcS.d. These scripts load daemons and services that are vital to the boot process. Lastly, upstart runs any startup scripts for the default runlevel in alphabetical order. Scripts in /etc/rcS.d are run in runlevels 1 through 5, so you should generally leave them alone unless you know what you are doing. If you accidentally disable a crucial service, you may have to resort to a rescue disc to undo the mistake.
Change the Runlevel
You can change the runlevel yourself on the command line with the init command. To switch to single-user mode from the command line, type:
$ sudo init 1
If you're running X11 when you issue this command, beware, since it will kill X and your desktop environment! This command runs all of the shutdown scripts for your current runlevel and then any start up scripts for single-user mode. To change back to the default multi-user runlevel for Ubuntu, type:
$ sudo init 2
You can also use init to halt or reboot a machine: just change to runlevel 0 and runlevel 6, respectively.
Manually Start and Stop Services
You can start and stop scripts manually by running the script with the start or stop argument. For example, to stop the CUPS service from the command line, type:
$ sudo /etc/init.d/cupsys stop
To start the service back up, type:
$ sudo /etc/init.d/cupsys start
Most scripts also support a restart argument that will run stop, then start for you. Most init scripts are also configured to output the list of arguments they support when you execute them without any options:
~$ sudo /etc/init.d/cups
Password:
Usage: /etc/init.d/cups {start|stop|restart|force-reload|status}
Disable Scripts from Starting
To disable a script, you must know your default runlevel. On Ubuntu, the default runlevel is usually set to 2, but you may want to double-check your default runlevel before you start disabling services.
You can find out your current runlevel with
~$ runlevel
N 2
As you can see, in this example, the default runlevel is in fact 2. Now change to the directory containing all of the scripts for that runlevel (/etc/rc2.d) and find the script you want to disable. To disable a service, just rename its script by changing the S to a D. For example, to disable the cupsys service, type:
~$ cd /etc/rc2.d
/etc/rc2.d$ sudo mv S50cups D50cups
To enable it again, rename it back by changing the D to an S:
~$ cd /etc/rc2.d
/etc/rc2.d$ sudo mv D50cupsys S50cups
You'll still need to stop the service as shown earlier if you want to shut it down right away, but renaming it will control whether it's started the next time you reboot (or change runlevels).
Alternately, you can use the update-rc.d command to manipulate the starting /stopping of init scripts.
No comments:
Post a Comment