📒 

Use systemd to Start a Linux Service at Boot

systemd is the modern init system used by many Linux distributions to manage system services and resources. One of its key features is the ability to start services automatically at boot, ensuring that critical applications and services are up and running as soon as the system starts. This article will guide you through the steps to create a systemd service unit and enable it to start at boot.

What is systemd?

systemd is an init system that replaces older init systems like SysVinit and Upstart. It manages system processes and services, allowing for parallelization of service start-up, on-demand starting of daemons, and better resource control. With its unit file structure, systemd provides a flexible way to manage services, sockets, devices, and other system resources.

Creating a systemd Service

To start a service at boot using systemd, you need to create a service unit file. This file contains all the necessary information for systemd to manage the service. Here’s how to create and configure a service unit file:

Step 1: Check the Working Directory

  1. Open your service unit file to check the WorkingDirectory directive:
    sudo nano /etc/systemd/system/myapp.service
  2. Find the WorkingDirectory line. It should look something like this( Look for: Create the Service Unit File):
    WorkingDirectory=/etc/systemd/myapp
  3. Verify that the specified directory exists:
    ls -l /etc/systemd/myapp

    /path/to/myapp – replace with the actual path you want to use. If you receive an error that the directory does not exist, you’ll need to create it.

Step 2: Create the Working Directory

  1. If the directory does not exist, create it:
    sudo mkdir -p /etc/systemd/myapp
  2. Set the appropriate permissions so that the user specified in the User directive of your service can access it. For example, if your service runs as myuser:
    sudo chown myuser:myuser /etc/systemd/myapp

    Create the Service Unit File

    1. Open a terminal on your Linux system.
    2. Create a new service unit file in the /etc/systemd/system/ directory. The file name should end with .service. For example, to create a service for a custom application called myapp, you would run:
      sudo nano /etc/systemd/system/myapp.service
    3. Add the following content to the file, modifying it as necessary for your application:
      [Unit]
      Description=My Custom Application
      After=network.target
      [Service]
      ExecStart=/usr/bin/myapp
      Restart=always
      User=myuser
      WorkingDirectory=/etc/systemd/myapp
      [Install]
      WantedBy=multi-user.target

      • [Unit] Section: This section provides metadata and dependencies for the service.
        • Description: A brief description of the service.
        • After: Specifies that the service should start after the network is available.
      • [Service] Section: This section defines how the service behaves.
        • ExecStart: The command to start the service. Replace /usr/bin/myapp with the path to your executable.
        • Restart: Defines the restart behavior. always means the service will restart if it crashes.
        • User: Specifies the user under which the service should run.
        • WorkingDirectory: Sets the working directory for the service.
      • [Install] Section: This section defines how the service should be enabled.
        • WantedBy: This defines the target under which the service should be started. multi-user.target is a common target for user services.

Step 3: Verify Permissions

  1. Check that the user has the correct permissions to access the working directory:
    ls -ld /etc/systemd/myapp

    Ensure that the permissions allow the user to read and execute files in this directory.

Step 4: Test Executable in the Working Directory

  1. Make sure that your application can run from this working directory. You can test this by switching to the user and navigating to the directory:
    sudo -u myuser bash
    cd /etc/systemd/myapp
    /usr/bin/myapp
    Check if it runs without any errors. If it fails, troubleshoot the application itself.

Step 5: Reload systemd and Start the Service

  1. After ensuring the working directory exists and is correctly set up, reload the systemd configuration:
    sudo systemctl daemon-reload
  2. Try starting the service again:
    sudo systemctl start myapp.service

Step 6: Check the Status

  1. Check the status of the service to see if it started successfully:
    sudo systemctl status myapp.service

Step 7: Review Logs for Additional Errors

If the service still fails to start, review the logs for additional information:

journalctl -u myapp.service

Step 8: (Optional) Stop and Disable the Service

If you ever need to stop the service or prevent it from starting at boot, you can use the following commands:

  • To stop the service:
    sudo systemctl stop myapp.service
  • To disable the service from starting at boot:
    sudo systemctl disable myapp.service

Conclusion

Using systemd to start a Linux service at boot is a straightforward process that enhances system reliability and management. By creating a service unit file, you can define how your application should run and ensure it starts automatically with your system. This method not only simplifies service management but also provides powerful features for controlling service behavior, making systemd an essential tool for modern Linux system administration.

With these steps, you can effectively set up and manage your own services, improving the overall functionality of your Linux environment. If you encounter any issues, reviewing the logs with journalctl can provide helpful insights into what might be going wrong.