How to Run a Linux Program at Startup with systemd

A stylized terminal window on a laptop.Fatmawati Achmad Zaenuri / Shutterstock

Need to run a Linux program at startup? Systemd software offers a way to do this on any Linux distribution with systemd– which is most of them these days, including Ubuntu. We’ll walk you through the process of building an integrated service, and it’ll even communicate with the journal.

This tutorial shows how to configure a system service that starts when you start your system. To start a graphics program when you log in, use your computer’s startup manager instead.

Running programs at startup

Sometimes the software that you install on your computer connects to the Linux startup process, so the program is automatically launched each time you start the computer. You can easily achieve this same behavior with your own programs and scripts, or indeed any other program on your computer.

Programs started at startup are controlled by systemd, the system and service director. systemd is the first process to run on startup. He always has Process ID (PID) 1. All other processes running on your computer are started by systemd, or by a process that systemd has already started.

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

Integrated with systemd via a service unit file
Launched at startup
Controllable using systemctl, the control interface for systemd
Able to write in the journal

Creation of the service program

We need a program that systemd will run. We are going to create a simple script, called “htg.sh”. This tutorial uses the Gedit text editor, but you can use any text editor you want.

touch htg.sh
gedit htg.sh

tap htg.sh in a terminal window

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 :
make
TIMESTAMP = $ (date ‘+% Y-% m-% d% H:% M:% S’)
echo “htg.service: timestamp $ {TIMESTAMP}” | systemd-cat -p info
sleep 60
finished

Save your changes and close the editor.

Htg.sh script in the gedit editor

The script doesn’t do much, but there are a few things to note.

Both echo lines are routed through systemd-cat, a program that takes the exit a program and send it to the log. Journal entries have priority. We use the -p (priority) option to indicate that our messages are for informational (info) only. These are not errors or important warnings.
There is an endless while loop.
The TIMESTAMP variable is set to the current date and time. This is formatted in a message and sent to the log.
The script then goes to sleep for 60 seconds.
After 60 seconds, the loop is repeated. So this script writes a time stamped message to the log once per minute.

We will copy the script to the / usr / local / bin directory.

sudo cp htg.sh / usr / local / bin

sudo cp htg.sh / usr / local / bin in a terminal window

And we need to make it executable:

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

sudo chmod + x /usr/local/bin/htg.sh in a terminal window

Creating the service unit file

Every program started by systemd has a definition file, called a service unit file. This contains certain attributes that systemd can use to locate and start the program, and to define some of its behaviors.

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

sudo systemctl list-unit-files –type-service

sudo systemctl list-unit-files --type-service in a terminal window

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

List of systemd unit files in a terminal window

Our service will be called “htg.service”. No unit file has this name, so we can go ahead and create our unit file.

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

sudo gedit /etc/systemd/system/htg.service in a terminal window

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

[Unit]

Description = Example of Geek How-To service

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.

htg.service unit file in gedit editor

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

The description: This is a textual description of your service.
Wants: Our service wants – but does not require – the network to be up and running before our service starts.
After: A list of the device names that should be started after starting this service, if they are not already running.
Type: Easy. systemd will consider this service started as soon as the process specified by ExecStart has been forked.
ExecStart: The path to the process to be started.
To restart: When and if the service should be restarted. We set it to “on failure”.
RestartSec: How long to wait before attempting to restart the service. This value is in seconds.
KillMode: Defines how systemd should kill the process if we ask systemctl to stop the service. We have this set to “treat”. This causes systemd to use the SIGTERM signal only on the main process. If our service was a non-trivial program instead of a simple script, we would set it to “mixed” to ensure that all generated processes are also terminated.
Search by: We have set this parameter to “multi-user.target” which means that the service should be started as long as the system is in a state where multiple users can log in regardless of whether a GUI is available or not.

The unit file does not need to be executable, but the permissions on the unit file should restrict who can edit it. You don’t want a malicious or malicious user to modify the unit file to run a completely different program.

This command will give the owner read and write permissions and read permissions on the group. Others will have no authorization.

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

sudo chmod 640 /etc/systemd/system/htg.service in a terminal window

We can ask systemctl to check the syntax of our unit file for us, even if the service is not yet running. Any errors will be reported. (In fact, the “.service” part is optional for most commands.)

systemctl status htg.service

systemctl status htg.service in a terminal window

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 file, you must tell systemd to reload the unit file definitions.

sudo systemctl daemon-reload

If you want a service to be started at startup, you need to enable it:

sudo systemctl enable htg

Enabling a service does not start it, but only sets it to start at startup. To start the service now, you need to use systemctl with the start option.

sudo systemctl start htg

sudo systemctl daemon-reload in a terminal window

Service verification

After manually starting the service or after restarting the computer, we can verify that our service is working properly.

sudo systemctl status htg.service

sudo systemctl status htg.service in a terminal window

The status of the service is displayed to us.

Service status htg.service in a terminal widow

The green dot means that our service is working properly.
The name of the service is “htg.service”, and the long description is the one we provided in the unit file.
We show which unit file was loaded “/etc/systemd/system/htg.service”.
The service is active and the time the service was started is shown for us.
Its PID is 7762.
There are two tasks associated with the service.
A total of 928 Memory kibibytes are used by the service.
The monitoring group includes the script “htg.sh” and the sleep command, which was started by “htg.sh”. Most of the time, the sleep command will do the work for that service.

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

Stopping and deactivating the service

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

sudo systemctl stop htg.service

sudo systemctl stop htg.service in a terminal window

This stops the service, but it does not prevent it from restarting the next time the computer is restarted. To stop starting the service at startup, you must disable it:

sudo systemctl disable htg.service

sudo systemtctl disable htg.service in a terminal window

If the service is running, this command does not stop it. It just tells systemd not to start the service on the next restart.

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

Service advice

Make sure that your program is running as expected before trying to start it as a service.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.