AllInfo

How to Run a Linux Program at Startup with systemd

Fatmawati Achmad Zaenuri/Shutterstock

Need to launch a Linux program at boot? The systemd software provides a way to do it on any Linux distro with systemd—which is most of them these days, including Ubuntu. We’ll walk you through the process of creating an integrated service—and it even talks to the journal.

This tutorial demonstrates how to set up a system service that launches when you start your system. To launch a graphical program when you sign in, use your desktop’s startup manager instead.

Running Programs at Startup

Sometimes the software you install on your computer hooks itself into the Linux startup process, so that the program is automatically launched each time the computer is started. You can easily achieve this same behavior with your own programs and scripts, or in fact any other program that is on your computer.

The programs that are launched at startup are controlled by systemd, the system and service manager. systemd is the first process to run at startup. It always has process ID (PID) 1. Every other process running in your computer is started by systemd, or by a process that systemd has already started.

Programs that run in the background are called daemons or services. The “d” at the end of systemd stands for daemon. In this article, we’ll create an example service. To tick all the boxes, our service must be:

Creating the Service Program

We need to have a program that systemd will launch. We’ll create a simple script, called “htg.sh”. This tutorial uses the Gedit text editor, but you can use whatever text editor you prefer.

touch htg.sh
gedit htg.sh

The gedit editor will open. Copy and paste the following text into the editor.

#!/bin/bash

echo “htg.service: ## Starting ##” | systemd-cat -p info

while :
do
TIMESTAMP=$(date ‘+%Y-%m-%d %H:%M:%S’)
echo “htg.service: timestamp ${TIMESTAMP}” | systemd-cat -p info
sleep 60
done

Save your changes and close the editor.

The script doesn’t do a whole lot, but there are a few points worth noting.

We’ll copy the script to the /usr/local/bin directory.

sudo cp htg.sh /usr/local/bin

And we need to make it executable:

sudo chmod +x /usr/local/bin/htg.sh

Creating the Service Unit File

Each program that is started by systemd has a definition file, called a service unit file. This holds certain attributes that systemd can use to locate and launch the program, and to define some of its behavior.

We need to create a unit file for our new service, but it is prudent to make sure none of the existing unit files have the name we want to give our new service.

sudo systemctl list-unit-files –type-service

You can scroll through the list of unit files, which is sorted alphabetically, and check that the name you want to use is not taken.

Our service is going to be called “htg.service”. No unit files have that name, so we can proceed and create our unit file.

sudo gedit /etc/systemd/system/htg.service

The gedit editor will open. Copy and paste the following text into the editor:

[Unit]
Description=How-To Geek Service Example

Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/htg.sh
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target

Save your changes and close the editor.

The entries have these meanings. These are typical entries. Our simple service doesn’t actually need most of them, but including them allows us to explain them.

The unit file doesn’t need to be executable, but the permissions on the unit file should restrict who can edit it. You don’t want a malicious or mischievous user changing the unit file so that it executes a different program altogether.

This command will give the owner read and write permissions, and read permissions to the group. Others will have no permissions.

sudo chmod 640 /etc/systemd/system/htg.service

We can have systemctl check the syntax of our unit file for us, even if the service isn’t running yet. Any errors will be reported. (Actually, the “.service” part is optional for most commands.)

systemctl status htg.service

No errors are highlighted, which means our unit file is syntactically correct.

Starting the Service

When you add a new unit file or edit an existing one, you must tell systemd to reload the unit file definitions.

sudo systemctl daemon-reload

If you want a service to be launched at startup you must enable it:

sudo systemctl enable htg

Enabling a service doesn’t start it, it only sets it to be launched at boot time. To start the service now, you must use systemctl with the start option.

sudo systemctl start htg

Verifying the Service

After manually starting the service or after rebooting the computer, we can verify that our service is running correctly.

sudo systemctl status htg.service

The status of the service is displayed for us.

We’re also shown the last 10 journal entries produced by this service. Unsurprisingly, they are all one minute apart.

Stopping and Disabling the Service

If you need to stop the service, you can do so with this command:

sudo systemctl stop htg.service

This stops the service, but it doesn’t prevent it from restarting next time the computer is rebooted. To stop the service being launched at startup, you need to disable it:

sudo systemctl disable htg.service

If the service is running, this command doesn’t stop it. It simply tells systemd not to launch the service at the next reboot.

If you want to stop the service and prevent it from launching at startup, use both commands.

Service Tip

Make sure your program runs as expected before you try to launch it as a service.

READ NEXT

Exit mobile version