Debian and Ubuntu use the service command to control services and update-rc.d for adding and removing services from start up. Using the service command we can start, stop, restart and display all available services. With update-rc.d we can add and remove services and add them to the Ubuntu/ Debian start up scripts. As Linux operating systems have multiple states, or runlevels, you need to make sure you add any new services to the correct runlevels. For example, you would not want to start a web service application before starting networking.
See my post on runlevels for more information about runlevels in Linux..
What services are available for startup?
Use the status-all switch to list all services which are registered with the OS and issues them a status command. You will then get one of the following displayed next to each service:
- [ + ] – Services with this sign are currently running.
- [ – ] – Services with this sign are not currently running..
- [ ? ] – Services that do not have a status switch.
service --status-all
Sample output:
service --status-all
[ ? ] acpid
[ - ] apparmor
[ ? ] apport
[ ? ] atd
[ - ] bootlogd
[ ? ] console-setup
[ ? ] cron
[ ? ] dbus
[ ? ] dmesg
[ ? ] dns-clean
[ ? ] friendly-recovery
[ ? ] glusterfs-server
[ - ] grub-common
[ ? ] gssd
[ ? ] hostname
[ ? ] hwclock
[ ? ] hwclock-save
[ ? ] idmapd
[ ? ] irqbalance
[ ? ] killprocs
[ ? ] module-init-tools
[ ? ] network-interface
[ ? ] network-interface-container
[ ? ] network-interface-security
[ ? ] networking
[ ? ] ondemand
[ ? ] passwd
[ ? ] plymouth
[ ? ] plymouth-log
[ ? ] plymouth-ready
[ ? ] plymouth-splash
[ ? ] plymouth-stop
[ ? ] plymouth-upstart-bridge
[ ? ] portmap
[ ? ] portmap-wait
[ ? ] pppd-dns
[ ? ] procps
[ ? ] rc.local
[ ? ] resolvconf
[ ? ] rpcbind-boot
[ - ] rsync
[ ? ] rsyslog
[ ? ] screen-cleanup
[ ? ] sendsigs
[ ? ] setvtrgb
[ + ] ssh
[ ? ] statd
[ ? ] statd-mounting
[ - ] stop-bootlogd
[ - ] stop-bootlogd-single
[ ? ] sudo
[ ? ] udev
[ ? ] udev-fallback-graphics
[ ? ] udev-finish
[ ? ] udevmonitor
[ ? ] udevtrigger
[ ? ] ufw
[ ? ] umountfs
[ ? ] umountnfs.sh
[ ? ] umountroot
[ - ] unattended-upgrades
[ - ] urandom
[ ? ] whoopsie
Start a service
Starting a service is done using the command service followed by the service name and the command to start the service.
service apache2 start
Stop a service
Use the stop keyword with service to stop a service.
service apache2 stop
Check the status of a service
Each service has a status, usually running or not running. Some services, such as network, may have a different output and output more information on the service.
service apache2 status
Remove a service
Use the remove keyword with update-rc.d to remove the service start up command for an application. You will need to use the -f switch if the applications /etc/init.d start up file exists.
update-rc.d -f apache2 remove
Add a service
Adding a service to Ubuntu or Debian is done with the update-rc.d command. You can specify which runlevels to start and stop the new service or accept the defaults. The init.d file will be added to the relevent rc.d startup folders.
update-rc.d apache2 defaults
Setting Start and Kill priority
If you need more control over when your service is asked to start and stop, you may need to set the start and kill (S and K) values.
For a given runlevel, you may have several services starting. For example, you may have apache2 and mysql both starting on runlevel 3. Ideally, you’d want mysql to start before apache2 and shutdown after apache2. In this case we need to give mysql the priority in startup, but apache2 the priority in shutdown.
When starting, the lower the number, the earlier it will start. A start priority of 10 will start before a priority of 20. When killing, it’s the opposite. A higher number will be killed before a lower number.
To set the start and kill priority we simply add them to the above update-rc.d command with the start priority first, followed by the kill priority.
update-rc.d apache2 defaults [START] [KILL]
The below command will start mysql first, then apache2. On shutdown, the kill will be the reverse of the start with apache2 being killed first and mysql second.
update-rc.d apache2 defaults 90 90
update-rc.d mysql defaults 10 10
Because, in our example, both start and kill priorities are the same we can shorted the command to just
update-rc.d apache2 defaults 90
update-rc.d mysql defaults 10
Manually set the RunLevel to Start and Kill a service
You can manually specify the Linux RunLevel that the system must be in to Start and Kill your service. See my other blog post for more information on RunLevels.
To extend the above example, we can specify exactly which RunLevel apache2 will be started and stopped.
update-rc.d apache2 start 10 2 3 4 5 . stop 90 0 1 6 .
apache2 will be started (as long as it isn’t already) when the system enters RunLevel 2, 3, 4 or 5 with a priority of 10. It will then be asked to stop when the system enters RunLevel 0, 1 or 6 with a priority of 90.
Let me know in the comments if you think anything is missing.