init

Introduction

init is the first process started by the Linux kernel once booted. It has the process ID (PID) of 1. This init process is in charge of starting services on boot and shutting them down when turning the computer off. systemd is the preferred init system of modern Linux distributions that has succeeded SysVinit. [1][2]

systemd

File Locations

systemd unit files can be created and used in any of these locations [3]:

  • /usr/lib/systemd/system/ = Global systemd unit files installed by the system package manager.

  • /run/systemd/system/ = Unit files that were created automatically by a program.

  • /etc/systemd/system/ = User-created unit files. These override all other locations if a unit file exists with the same name.

  • ~/.config/systemd/user/ = User-created unit files that are managed locally by a user. These services only run while the user is logged in. These are managed with the command systemctl --user instead of the typical systemctl command.

When adding new unit files, it is required to run this command to update the systemd cache about available unit files [4]:

$ sudo systemctl daemon-reload

Creating a Unit File

All unit files are ini configuration files that use this layout:

[Unit]
Description=<DESCRIPTION_ABOUT_UNIT>

[<UNIT_TYPE>]
<UNIT_CONFIG_KEY>=<UNIT_CONFIG_VALUE>

[Install]
WantedBy=<SYSTEMD_TARGET>

Configuration options for unit files:

  • Run a system unit during a normal boot.

    [Install]
    WantedBy=multi-user.target
    
  • Run a user unit during login.

    [Install]
    WantedBy=default.target
    
  • Log standard output and/or standard error of a service to a file.

    [Service]
    StandardOutput=<PATH_TO_FILE>
    StandardError=<PATH_TO_FILE>
    
  • Create a service that runs exactly once.

    [Service]
    Type=oneshot
    ExecStart=<PATH_TO_EXECTUABLE>
    ExecStart=/bin/systemctl --no-reload disable %n
    
  • Run the unit file if the file or directory does not exist.

    [Unit]
    ConditionPathExists=!<PATH_TO_FILE>
    
  • Do not timeout while starting a service.

    [Service]
    TimeoutSec=infinity
    
  • Start a service after a specified amount of time.

    [Service]
    ExecStartPre=/bin/sleep 0.5
    ExecStart=/usr/bin/foobar
    
  • Automatically restart a service if it fails. [5]

    [Service]
    ExecStart=/usr/bin/foobar
    Restart=on-failure
    RestartSec=0.1s
    
  • Run two or more commands. systemd will run one command at a time starting from top to bottom.

    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c "echo foo"
    ExecStart=/bin/sh -c "echo bar"
    RemainAfterExit=yes
    TimeoutSec=0
    
  • Start a unit after the networking service is online. If there is no network interface on the computer, then systemd will consider the networking services to be online.

    [Unit]
    After=network-online.target
    Wants=network-online.target
    
    • Depending on the networking service used, enable it to wait to be online. [6]

      $ sudo systemctl enable NetworkManager-wait-online.service
      
      $ sudo systemctl enable systemd-networkd-wait-online.service
      
  • Configure environment variables in the systemd unit file or source them from an external file. [8]

    [Service]
    Environment=foo=bar
    EnvironmentFile=/app/env
    
    $ cat /app/env
    app_host=127.0.0.1
    app_address=80
    

Preset Files

System packages can define if a unit should be enabled or disabled by default. [7]

  • Create a file called /usr/lib/systemd/system-preset/<UNIT>.<UNIT_TYPE>.

  • Edit the file with the contents of enable <UNIT>.<UNIT_TYPE> or disable <UNIT.<UNIT_TYPE>.

Enable Services on Boot

Enable a system service to start on boot. [9]

  • Automatically:

    $ sudo systemctl enable <UNIT>
    
  • Manually:

    $ sudo ln -s /usr/lib/systemd/system/<UNIT>.service /etc/systemd/system/multi-user.target.wants/<UNIT>.service
    

Enable a user service to start when the login. [10]

  • Automatically:

    $ systemctl --user enable <UNIT>
    
  • Manually:

    $ ln -s ~/.config/systemd/user/<UNIT>.service ~/.config/systemd/user/default.target.wants/<UNIT>.service
    

History

Bibliography

  1. “What is an init system?” Fedora Magazine. October 31, 2015. Accessed May 11, 2023. https://fedoramagazine.org/what-is-an-init-system/

  2. “init.” ArchWiki. March 12, 2023. Accessed May 11, 2023. https://wiki.archlinux.org/title/init

  3. “Understanding Systemd Units and Unit Files.” DigitalOcean Tutorials. February 17, 2015. Accessed May 11, 2023. https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files

  4. “Where do I put my systemd unit file?” Unix & Linux Stack Exchange. March 10, 2023. Accessed May 11, 2023. https://unix.stackexchange.com/questions/224992/where-do-i-put-my-systemd-unit-file

  5. “Auto-restart a crashed service in systemd.” Mattias Geniar. January 13, 2020. Accessed May 11, 2023. https://ma.ttias.be/auto-restart-crashed-service-systemd/

  6. “Network Configuration Synchronization Points.” systemd.io. 2022. Accessed May 11, 2023. https://systemd.io/NETWORK_ONLINE/

  7. “systemd.preset.” systemd. Accessed May 16, 2023. https://www.freedesktop.org/software/systemd/man/systemd.preset.html

  8. “Using environment variables in systemd units.” Flatcar Container Linux. Accessed August 29, 2023. https://www.flatcar.org/docs/latest/setup/systemd/environment-variables/

  9. “Use systemd to Start a Linux Service at Boot.” Linode Docs. March 9, 2023. Accessed September 8, 2023. https://www.linode.com/docs/guides/start-service-at-boot/

  10. “Start a systemd user service at boot.” Super User. August 14, 2023. Accessed September 8, 2023. https://superuser.com/questions/1025091/start-a-systemd-user-service-at-boot