How to Run a Linux Program at Startup with systemd

Posted under » Ubuntu » Linux on 15 October 2025

systemd is a system and service manager for Linux operating systems, designed to be backward compatible with SysVinit scripts. It is responsible for initializing the system, managing processes, and handling various system tasks.

systemd has widely become the new standard for Linux distributions like ubuntu. Due to its heavy adoption, familiarizing yourself with systemd is well worth the trouble, as it will make administering servers considerably easier. systemctl is the primary command-line utility used to interact with and control the systemd system and service manager. It allows users to manage "units," which are resources that systemd knows how to operate on and manage. These units are defined by configuration files called unit files.

The programs that are launched at startup are controlled by systemd. 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 hence the "d" at the end. We'll create an example service. To tick all the boxes, our service must:

We need to have a program that systemd will launch. We'll create a simple script, called test.sh As sudo we'll copy the script to the /usr/local/bin directory and we need to make it executable

$ cp test.sh /usr/local/bin
$ chmod +x /usr/local/bin/test.sh

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. Check that none of the existing unit files have the name we want to give our new service.

$ systemctl list-units --type=service
You will see this. It is safe to name our service test.service.
$ vim /etc/systemd/system/test.service

[Unit]
Description=a test service
Wants=network.target
After=syslog.target network-online.target

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

[Install]
WantedBy=multi-user.target

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.

You can try these commands.

$ systemctl daemon-reload
$ systemctl enable test # If you want a service to be launched at startup
$ systemctl disable test.service # or not
$ systemctl start test
$ systemctl status test.service
$ systemctl stop test.service


web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data