In this guide, we will walk you through the process of configuring a service to autostart in Ubuntu 20.04 using systemd. Systemd is a system and service manager for Linux operating systems and is designed to be backward compatible with SysV init scripts. It allows you to manage services and processes, and also provides mechanisms for dependency resolution and service configuration.
To autostart a process in Ubuntu 20.04 using systemd, you need to create a service file in the
/etc/systemd/system directory, define the service by specifying its description, dependencies, and command to run, enable and start the service using the
systemctl command, and check the service status with
systemctl status. Troubleshoot any issues related to permissions, executable path, manual start, and logs.
Creating a Service File
To start, you need to create a service file. This file will define the service and its configuration. The file should have a
.service extension and should be placed in the
/etc/systemd/system directory. For example, you might create a file called
To create this file, you can use the
nano text editor:
sudo nano /etc/systemd/system/my_service.service
Defining the Service
Once you’ve created the service file, you need to define the service. This involves specifying a description of the service, any dependencies it has, and the command to run the service. Here’s an example of what your service file might look like:
[Unit] Description=My Service After=network.target [Service] WorkingDirectory=/home/user/my_service ExecStart=/home/user/my_service/my_script.sh KillMode=process StandardOutput=syslog StandardError=syslog SyslogIdentifier=my_service Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target
In this example, the
After=network.target directive ensures that the service starts after the network is up. The
ExecStart directive specifies the command to run the service.
KillMode=process tells systemd to only kill the main process, not all processes in the process group.
StandardError=syslog redirect standard output and standard error to the syslog service.
Restart=on-failure tells systemd to restart the service if it fails, and
RestartSec=10 specifies a 10-second delay before restarting. Finally,
WantedBy=multi-user.target ensures the service starts during the normal multi-user boot sequence.
Enabling and Starting the Service
After defining the service, you can enable it to start on boot and start it immediately with the following commands:
sudo systemctl enable my_service sudo systemctl start my_service
systemctl enable command creates a symbolic link that points the system to the service file you created, thus enabling the service to start on boot. The
systemctl start command starts the service immediately.
Checking the Service Status
You can check the status of your service with the
systemctl status command:
sudo systemctl status my_service
If the service is running correctly, you should see output indicating that the service is active and running.
If you encounter issues, there are several things you can check:
- Permissions: Ensure that the user and group specified in the service file have the necessary permissions to access the working directory and execute the script. Adjust the ownership and permissions of the relevant files and directories if necessary.
- Executable path: Verify that the
ExecStartpath in the service file points to the actual location of the script.
- Manual start: Try starting the process manually with the command specified in
ExecStartto identify any errors or issues.
- Logs: Check the system logs for error messages related to your service. You can view the logs with the
journalctl -u my_service
This command shows logs specific to your service.
By following these steps, you should be able to configure a service to autostart on Ubuntu 20.04 using systemd. Remember to replace
my_service with the actual name of your service.
Systemd is a system and service manager for Linux operating systems. It is designed to be backward compatible with SysV init scripts and provides mechanisms for managing services and processes, as well as dependency resolution and service configuration.
The service file should be created in the
/etc/systemd/system directory. You can use the
sudo nano /etc/systemd/system/my_service.service command to create the file.
To define the service, you need to specify a description of the service, any dependencies it has, and the command to run the service. You can refer to the example in the guide for a template of how to define the service in the service file.
You can enable the service to start on boot and start it immediately using the
sudo systemctl enable my_service and
sudo systemctl start my_service commands, respectively.
You can check the status of your service using the
sudo systemctl status my_service command. This will provide information about whether the service is active and running correctly.
In the commands and file names mentioned in the guide, you should replace
my_service with the actual name of your service. For example, if your service is named
example_service, you would use
sudo systemctl enable example_service and
sudo systemctl start example_service to enable and start the service.