Manage Linux services on the command line (Ubuntu)

Updated . Posted . Visible to the public.

Ubuntu 18.04 uses systemd to manage services.

There are basically two commands for listing all services and manipulating the state of a certain service: service and systemctl:

  • service manages System V init scripts
  • systemctl controls the state of the systemd system and service manager. It is backwards compatible to System V and includes the System V services

Therefore I prefer to use systemctl.


See which services are there

>systemctl list-units -a --type=service
  UNIT                                 LOAD      ACTIVE   SUB     DESCRIPTION                
  accounts-daemon.service              loaded    active   running Accounts Service           
  acpid.service                        loaded    active   running ACPI event daemon          
  alsa-restore.service                 loaded    active   exited  Save/Restore Sound Card State                                                   
  alsa-state.service                   loaded    inactive dead    Manage Sound Card State (restore and store)                                     
  anacron.service                      loaded    inactive dead    Run anacron jobs           
  apache-htcacheclean.service          loaded    active   running Disk Cache Cleaning Daemon for Apache HTTP Server                               
  apache2.service                      loaded    active   running The Apache HTTP Server     
  apparmor.service                     loaded    active   exited  AppArmor initialization    
  apport-autoreport.service            loaded    inactive dead    Process error reports when automatic reporting is enabled                       
  apport.service                       loaded    active   exited  LSB: automatic crash report generation                                          
  apt-daily-upgrade.service            loaded    inactive dead    Daily apt upgrade and clean activities                                          
  apt-daily.service                    loaded    inactive dead    Daily apt download activities                                                             
  avahi-daemon.service                 loaded    active   running Avahi mDNS/DNS-SD Stack    
  avahi-dnsconfd.service               loaded    inactive dead    Avahi DNS Configuration Daemon                                                  
  blk-availability.service             loaded    active   exited  Availability of block devices                                                   
  bluetooth.service                    loaded    active   running Bluetooth service   
...

Manage Services

To temporarily start or stop an service, you can use :

  • systemctl start <name>
  • systemctl stop <name>

To prevent a service from being started e.g. on boot, you have to disable it. Likewise, if you want a service to be started on boot, you have to enable it:

  • systemctl enable <name>
  • systemctl disable <name>

If you disable a running service, it will not be stopped. It will just not be started on the next boot. So if you want to stop a service now and disable it, you have to do both steps:

Example: Stop and disable bluetooth

>systemctl list-units -a --type=service | grep bluetooth
  bluetooth.service               loaded    active   running Bluetooth service      

>sudo systemctl stop bluetooth.service

>systemctl list-units -a --type=service | grep bluetooth
  bluetooth.service               loaded    inactive dead    Bluetooth service >sudo systemctl disable bluetooth

>sudo systemctl disable bluetooth.service
Synchronizing state of bluetooth.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable bluetooth
insserv: warning: current start runlevel(s) (empty) of script `bluetooth' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `bluetooth' overrides LSB defaults (0 1 6).
insserv: warning: current start runlevel(s) (empty) of script `bluetooth' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `bluetooth' overrides LSB defaults (0 1 6).

service / System V

If you don't want to use systemd's systemctl, you can also manage the System V init scripts with the service command. Note that non system V services will not be listed and you can not manage them with service.

To get an overview which system V init scripts are available and running / not running, you can use service --status-all:

>service --status-all
 [ + ]  acpid
 [ - ]  alsa-utils
 [ - ]  anacron
 [ + ]  apache-htcacheclean
 [ - ]  apache2
 [ + ]  apparmor
 [ + ]  apport
 [ + ]  avahi-daemon
 [ + ]  avahi-dnsconfd
 [ - ]  bluetooth
...

+ means running, - means not running, ? means unknown

Manage Services

To temporarily start or stop an service, you can use:

  • service <name> start
  • service <name> stop

The other commands that are available with service depend on the service.

Judith Roth
Last edit
Judith Roth
License
Source code in this card is licensed under the MIT License.
Posted by Judith Roth to makandra dev (2020-10-15 08:46)