Introduction to systemd

  1. What Is systemd?


  2. Systemd’s Core Concepts



  3. Most Common systemctl Commands

  4. sudo systemctl start nginx
    sudo systemctl stop nginx
    sudo systemctl restart nginx
    
    sudo systemctl enable nginx
    sudo systemctl disable nginx
    
    systemctl status nginx
    
    journalctl -u nginx
    


  5. A Minimal systemd Service Example

  6. [Unit]
    Description=My Sample App
    
    [Service]
    ExecStart=/usr/bin/python3 /opt/myapp/app.py
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    sudo systemctl daemon-reload
    sudo systemctl start myapp
    sudo systemctl enable myapp
    



Viewing Information in systemd

  1. Overview



  2. Check the Status of a Service

  3. systemctl status nginx
    


  4. List All Units

  5. systemctl list-units
    
    systemctl list-units --all
    


  6. List All Services Only

  7. systemctl list-units --type=service
    


  8. List Failed Units

  9. systemctl --failed
    


  10. View Logs of a Service

  11. journalctl -u nginx
    
    journalctl -u nginx -f
    
    journalctl -u nginx --since today
    
    journalctl -u nginx -b
    


  12. View System-Wide Logs

  13. journalctl
    
    journalctl -p warning
    journalctl -p err
    journalctl -p crit
    
    journalctl --since "2024-01-01" --until "2024-01-02"
    


  14. Inspect Unit Configuration Files

  15. systemctl cat nginx.service
    
    systemctl show nginx
    


  16. View Dependencies and Boot Order

  17. systemctl list-dependencies sshd
    
    systemctl list-dependencies --reverse sshd
    
    systemctl list-dependencies --all
    


  18. Check Boot Time and Performance

  19. systemd-analyze
    
    systemd-analyze blame
    
    systemd-analyze plot > boot.svg
    


  20. Check Current System Target (Runlevel)

  21. systemctl get-default
    



Working with Services in systemd

  1. Overview



  2. Starting and Stopping Services

  3. sudo systemctl start nginx
    
    sudo systemctl stop nginx
    
    sudo systemctl restart nginx
    
    sudo systemctl reload nginx
    


  4. Enabling and Disabling Services at Boot

  5. sudo systemctl enable nginx
    
    sudo systemctl disable nginx
    
    systemctl is-enabled nginx
    


  6. Checking Service Status

  7. systemctl status nginx
    


  8. Viewing Logs of a Service

  9. journalctl -u nginx
    
    journalctl -u nginx -f
    
    journalctl -u nginx -b
    


  10. Reloading systemd After Creating or Editing Unit Files

  11. sudo systemctl daemon-reload
    
    sudo systemctl restart myapp.service
    


  12. Masking and Unmasking Services

  13. sudo systemctl mask nginx
    
    sudo systemctl unmask nginx
    


  14. Viewing All Services

  15. systemctl list-units --type=service
    

    systemctl list-unit-files --type=service
    


  16. Autorestart and Service Recovery

  17. [Service]
    Restart=on-failure
    RestartSec=5
    


  18. Environments and Working Directory

  19. [Service]
    Environment="APP_ENV=production"
    WorkingDirectory=/opt/myapp
    ExecStart=/usr/bin/python3 app.py
    
    EnvironmentFile=/etc/myapp/env.conf
    



Changing System States in systemd

  1. Overview


  2. Common systemd Targets

  3. poweroff.target      → Shut down the system
    reboot.target        → Restart the system
    multi-user.target    → Multi-user text mode (server mode)
    graphical.target     → GUI login mode
    rescue.target        → Single-user maintenance mode
    emergency.target     → Very minimal recovery shell
    default.target       → The system’s default boot mode
    


  4. Check the Current System State

  5. systemctl get-default
    
    graphical.target
    multi-user.target
    


  6. Switching System States (Temporarily)

  7. sudo systemctl isolate multi-user.target
    
    sudo systemctl isolate graphical.target
    


  8. Changing the Default Boot Target

  9. sudo systemctl set-default multi-user.target
    
    sudo systemctl set-default graphical.target
    


  10. Rebooting, Powering Off, and Suspending

  11. sudo systemctl reboot       # Restart system
    sudo systemctl poweroff     # Shut down
    sudo systemctl halt         # Halt CPU (stop)
    sudo systemctl suspend      # Sleep mode
    sudo systemctl hibernate    # Hibernate
    


  12. Recovery States (Rescue / Emergency)

  13. sudo systemctl rescue
    
    sudo systemctl emergency
    


  14. Showing a Target’s Dependencies

  15. systemctl list-dependencies graphical.target
    



Creating Custom Services in systemd

  1. Overview


  2. Where to Put Custom Service Files

  3. /etc/systemd/system/myapp.service
    
    ~/.config/systemd/user/myapp.service
    


  4. Minimal Custom Service Example

  5. [Unit]
    Description=My Python App
    
    [Service]
    ExecStart=/usr/bin/python3 /opt/myapp/app.py
    
    [Install]
    WantedBy=multi-user.target
    
    /etc/systemd/system/myapp.service
    

    sudo systemctl daemon-reload
    sudo systemctl enable myapp
    sudo systemctl start myapp
    


  6. Using Working Directory and Environment Variables

  7. [Unit]
    Description=Node.js Backend
    
    [Service]
    WorkingDirectory=/opt/backend
    ExecStart=/usr/bin/node server.js
    Environment="NODE_ENV=production"
    Environment="API_KEY=12345"
    
    [Install]
    WantedBy=multi-user.target
    
    EnvironmentFile=/etc/backend/env.conf
    


  8. Restart Policies

  9. [Service]
    Restart=on-failure
    RestartSec=3
    


  10. Running Services as a Dedicated User

  11. [Service]
    User=myapp
    Group=myapp
    ExecStart=/usr/bin/myapp
    
    sudo useradd --system --no-create-home myapp
    


  12. Capturing Logs (journalctl)

  13. journalctl -u myapp -f
    
    StandardOutput=journal
    StandardError=journal
    


  14. Creating Services That Wait for Other Units

  15. [Unit]
    Description=Backend App
    After=network.target
    Requires=network.target
    


  16. Running a Script as a Service

  17. #!/bin/bash
    echo "Hello from my service!"
    sleep 60
    
    [Unit]
    Description=My Script Service
    
    [Service]
    ExecStart=/usr/local/bin/myscript.sh
    
    [Install]
    WantedBy=multi-user.target
    


  18. Reloading and Updating Services

  19. sudo systemctl daemon-reload
    sudo systemctl restart myapp
    


  20. Disabling, Masking, and Removing

  21. sudo systemctl disable myapp
    
    sudo systemctl mask myapp
    
    sudo rm /etc/systemd/system/myapp.service
    sudo systemctl daemon-reload