How to tell which services run at startup on Raspberry Pi? (raspbian)

I have some services that run on boot but I've actually forgotten how I set them up! If I want to change their activity how might I do that? For instance, maybe I want to disable a service from starting on boot or maybe I want to start it using a different method.

Specifically, I've got on openvpn service that runs successfully on boot but some tutorials say I should tie the service of filesharing programs to the vpn itself so that if the vpn goes down then so does the filesharing service. But I think perhaps the autorunning of my vpn is complicating things and I should disable it or modify the way it launches. But how to check?


Solution 1:

I am assuming you have a Raspberry using raspian, which, to the best of my knowledge, has not yet moved on to systemd. In this case, the answer is as follows.

EDIT: for systemd (Jessie or later), see at the bottom.

First, you can determine which system is currently running by means of the command:

  sudo service --status-all

You will get a list somewhat like this:

  [ + ]  triggerhappy
  [ + ]  udev
  [ ? ]  udev-mtab
  [ ? ]  umountfs
  [ ? ]  umountnfs.sh
  [ ? ]  umountroot
  [ - ]  urandom
  [ + ]  vsftpd
  [ + ]  weewx
  [ - ]  x11-common

The +'s indicate services running, the -'s services not running, the ?'s services which do not allow status as a possible command: they are mostly commands that do not need to run all the time, just occasionally.

To go back to your question, you need to understand runlevels in Linux. Runlevels (0-6 + S) identify which services may/must be run. A boot sequence progresses from an initial runlevel (specified in /etc/inittab, in my Raspberry

 # The default runlevel.
 id:2:initdefault:

this line identifies it as 2, and then progresses to your distro standard final runlevel (from 2 to 5). You identify your current runlevel by means of the command

 # runlevel
 N 2

(in my case, a headless server). The other runlevels are (again from é/etc/inittab*):

 # Runlevel 0 is halt.
 # Runlevel 1 is single-user.
 # Runlevels 2-5 are multi-user.
 # Runlevel 6 is reboot.

Single user (1) is often also identified as S.

In a normal boot, thus you start at 2, and progress up to 5. To find the services started in this progression, issue the command:

 # grep -nrI Default-Start /etc/init.d
 umountroot:7:# Default-Start:
 checkroot.sh:8:# Default-Start:     S
 kbd:6:# Default-Start:     S
 isc-dhcp-server:11:# Default-Start:     2 3 4 5
 mountkernfs.sh:7:# Default-Start:     S
 hostname.sh:7:# Default-Start:     S
 mountnfs.sh:7:# Default-Start:     S
 halt:6:# Default-Start:
 dphys-swapfile:20:# Default-Start:     2 3 4 5
 bootlogs:7:# Default-Start:     1 2 3 4 5
 rc.local:6:# Default-Start:     2 3 4 5
 hostapd:9:# Default-Start:      2 3 4 5
 keyboard-setup:8:# Default-Start:     S
 lightdm:7:# Default-Start:     2 3 4 5
 procps:11:# Default-Start:     S
 mysql:9:# Default-Start:     2 3 4 5
 single:6:# Default-Start:     1
 vsftpd:7:# Default-Start:       2 3 4 5
 samba:7:# Default-Start:     2 3 4 5
 mountall.sh:6:# Default-Start:     S
 weewx:20:# Default-Start:     2 3 4 5
 umountnfs.sh:7:# Default-Start:
 triggerhappy:6:# Default-Start:     2 3 4 5
 ifplugd:24:# Default-Start:     2 3 4 5
 alsa-utils:9:# Default-Start:     S
 bootmisc.sh:7:# Default-Start:     S
 sendsigs:6:# Default-Start:
 console-setup:7:# Default-Start:     S
 nfs-common:7:# Default-Start:     2 3 4 5 S
 checkroot-bootclean.sh:6:# Default-Start:     S
 rsyslog:7:# Default-Start:     2 3 4 5
 x11-common:7:# Default-Start:     S
 mountdevsubfs.sh:7:# Default-Start:     S
 cron:10:# Default-Start:     2 3 4 5
 mountnfs-bootclean.sh:6:# Default-Start:     S
 mountall-bootclean.sh:6:# Default-Start:     S
 screen-cleanup:11:# Default-Start:     S
 udev:6:# Default-Start:     S
 ssh:7:# Default-Start:  2 3 4 5
 haveged:8:# Default-Start:     2 3 4 5
 hwclock.sh:26:# Default-Start:     S
 plymouth:9:# Default-Start:     2 3 4 5
 motd:7:# Default-Start:     1 2 3 4 5
 killprocs:6:# Default-Start:     1
 networking:6:# Default-Start:     S
 fake-hwclock:9:# Default-Start:     S
 udev-mtab:6:# Default-Start:     S
 apache2:6:# Default-Start:     2 3 4 5
 reboot:6:# Default-Start:
 umountfs:6:# Default-Start:
 plymouth-log:9:# Default-Start: S
 openvpn:11:# Default-Start:     2 3 4 5
 kmod:8:# Default-Start:     S
 ntp:7:# Default-Start:   2 3 4 5
 rpcbind:9:# Default-Start:     S 2 3 4 5
 urandom:6:# Default-Start:     S
 rmnologin:6:# Default-Start:     2 3 4 5
 checkfs.sh:7:# Default-Start:     S
 sudo:8:# Default-Start:     2 3 4 5
 mtab.sh:6:# Default-Start:     S
 dbus:6:# Default-Start:     2 3 4 5
 rsync:8:# Default-Start:     2 3 4 5

This prints the line that starts with Default-Start in all files in /etc/init.d/, which is where the files that identify the various services are kept. The line in question identifies the runlevel where each service is to be started. If a service is started at runlevel N, when the system progresses to N+1 it does not need to be started again, but if it found not running it is started.

This gives you an exact idea of the different stages at which services are started. But within the same runlevel, the lines Required-Start and Should-Start define hard and soft dependencies for the service in question, i.e. services which must run and should run before the service being examined. Without the Required-Start services, the service in question will fail, without the Should-Start services instead there will be no general failure, possibly just the lack of some useful features. The same occurs on stop with Required-Stop and Should-Stop.

SYSTEMD:

The command to list all services is

systemctl list-unit-files

which will spit out much info. To select all the services enabled, i.e, those which are started at boot, just grep the above command as follows:

 systemctl list-unit-files | grep enabled 

I prefer this command to systemctl list-units: the difference is that this one will only display services your OS has in memory = the OS thinks it needs), while systemctl list-unit-files will display all installed services, including those which are masked (i.e. those which are supposed never to be started, not even manually), and those which it will not activate, which are called static. In other words, the command I suggested is more comprehensive than the one listing only active services.

NB: systemd is very different from previous init systems. In particular, it does not have runlevels, but targets. runlevels still exist for compatibility reasons, and can be see as specific targets by listing all available targets via

systemctl list-unit-files --type=target

while you can see your current target by means of

$ systemctl get-default
  graphical.target

For more on this, you may read DigitalOcean´s excellent introductory page.

Solution 2:

For simple startup service management on Raspberry Pi I recommend tool rcconf. It allows you to easily turn on/off services in /etc/init.d/. You can also see if they are enabled and will run at startup.

To install the tool

sudo apt-get install rcconf

And is very simple to use it with text UI

sudo rcconf

Solution 3:

sudo nano /etc/rc.local

Here is where you insert batch file or commands to run on boot. I auto run Openvpn on boot too :)