قالب وردپرس درنا توس
Home / Tips and Tricks / How to run a Linux program at startup with systemd

How to run a Linux program at startup with systemd



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

Do you need to start a Linux program at startup? Systemd software provides a way to do that on all Linux distros with systemd- which is most of these days, including Ubuntu. We guide you through the process of creating an integrated service ̵

1; and it even speaks to the magazine.

This tutorial shows you how to set up a system service that starts when you start your system. To start a graphics application when you log in, use the desktop startup manager instead.

Run programs at startup

Sometimes the software you install on your computer is connected to the Linux startup process, so that the program starts automatically each time the computer starts. You can easily achieve the same behavior with your own programs and scripts or in fact other programs that are on your computer.

Programs that start at startup are controlled by systemd, system and service manager. systemd is the first process run at startup. It always has a process ID (PID) 1. Every other process running on your computer is started systemd, or through a process such as systemd has already started.

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

  • Integrated with systemd through a service device file
  • Launched at launch
  • Steerable with systemctl, the control interface for systemd
  • Be able to write for the magazine

Create the service program

We must have a program that systemd will start. We create a simple script called “htg.sh”. This tutorial uses Gedit text editor, but you can use any text editor you prefer.

touch htg.sh
gedit htg.sh

press htg.sh in a terminal window

The gedit the editor opens. 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.

htg.sh script in gedit editor

The script does not do much, but there are some points that are worth noting.

  • The two echo wires are moved through systemd-cat, a program that takes output from a program and sends it to the journal. Contributions to the journal are given priority. We use -p (priority) option to indicate that our messages are for information only (info). They are not important errors or warnings.
  • There is an infinity while loop.
  • The TIMESTAMP the variable is set to the current date and time. This is formatted into a message and sent to the journal.
  • The script then sleeps for 60 seconds.
  • After 60 seconds, the loop is repeated. So this script writes a timestamp message to the journal once per minute.

We copy the script to /usr/local/bin catalog.

sudo cp htg.sh /usr/local/bin

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

And we must make it drivable:

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

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

Create the service device file

Each program started by systemd has a definition file, called a service device file. This has some attributes like systemd can use to find and start the program and to define part of its behavior.

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

sudo systemctl list-unit-files --type-service

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

You can browse the list of device files sorted alphabetically and make sure the name you want to use is not taken.

List of system drive files in a terminal window

Our service will be called “htg.service”. No device files have that name, so we can go ahead and create our device file.

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

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

The gedit the editor opens. 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.

htg.service device file in the gedit editor

The records have these meanings. These are typical items. Our simple service does not actually need most of them, but including them allows us to explain them.

  • Description: This is a text description of your service.
  • Wants: Our service wants – but does not require – the network to be up before our service is started.
  • After: A list of device names that should be started after starting this service, if they are not already running.
  • Type: Simple. systemd will consider that this service started as soon as the process specified by ExecStart has been forked.
  • ExecStart: The road to the process to be started.
  • Restart: When and if the service is to be restarted. We have set it to “in case of failure.”
  • Restart Sec: How long to wait before trying 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 setting “process”. This causes systemd to use SIGTERM only on the main process. If our service was a non-trivial program instead of a simple script, we would set this to “mixed” to ensure that any resulting processes were also terminated.
  • WantedBy: We have this setting to “multi-user.target”, which means that the service should be started as long as the system is in a state where several users can log in, regardless of whether a graphical user interface is available.

The drive file does not have to be executable, but the permissions of the drive file should restrict who can edit it. You do not want a malicious or malicious user to modify the device file to run another program entirely.

This command gives the owner read and write permissions and read permissions to the group. Others have no privileges.

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

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

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

systemctl status htg.service

systemctl status htg.service in a terminal window

No errors are marked, which means that our device file is syntactically correct.

Start the service

When adding a new device file or editing an existing one, you need to tell systemd to reload the device file definitions.

sudo systemctl daemon-reload

If you want a service to start at startup, you must Enable the:

sudo systemctl enable htg

Activating a service does not start it, it only sets it to start at start-up time. To start the service nowyou must use systemctl with start alternative.

sudo systemctl start htg

sudo systemctl daemon reload in a terminal window

Verify the service

After manually starting the service or after restarting the computer, we can verify that our service works correctly.

sudo systemctl status htg.service

sudo systemctl status htg.service in a terminal window

The status of the service is shown to us.

Status of htg.service in a terminal widnow

  • The green dot means that our service is running smoothly.
  • The name of the service is “htg.service” and the long description is the one we provided in the device file.
  • We are shown which device file has been loaded “/etc/systemd/system/htg.service”.
  • The service is active and the time when the service was launched is listed for us.
  • Its PID is 7762.
  • There are two tasks associated with the service.
  • A total of 928 kibytes of memory is used by the service.
  • The control group contains the script “htg.sh” and sleep command, launched by “htg.sh.” Mostly, sleep the command will do the job for this service.

We also show the last ten journal entries produced by this service. Not surprisingly, they are all at one minute intervals.

Stop and disable 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 does not prevent it from restarting the next time the computer is restarted. To stop the service that starts at startup, you must disable the:

sudo systemctl disable htg.service

sudo systemtctl disables htg.service in a terminal window

If the service is running, this command does not stop. It simply tells systemd not to start the service on the next reboot.

To stop the service and prevent it from starting at startup, use both commands.

Service tips

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




Source link