Learn › Build and operate services › Give a program a service name
A program can keep working after you close your terminal. On this machine, systemd starts programs and keeps track of them. To give it a program to manage, you write a unit file. Its filename gives the service a name. Inside the file, ExecStart= tells systemd what to run. One running copy of a program is a process. Linux gives it a number called a process identifier (PID).
A service that already has a name. In an earlier Linux Camp lab, a program called the reporter announced when it was ready. This worked example reconstructs that run from its recorded PID, time and message. The commands illustrate how to inspect those observations, and the journal excerpt shortens the message. Read the example before opening your own terminal.
$ systemctl cat camp-reporter.service | grep '^ExecStart='
ExecStart=/usr/local/bin/camp-reporter
The filename gives this service its name. The line inside the file says which program the manager should launch. To see whether it did, the operator could ask systemd for its current state and main process number.
$ systemctl show -p LoadState -p ActiveState -p MainPID camp-reporter.service
MainPID=1449
LoadState=loaded
ActiveState=active
Systemd had read the unit and was tracking process 1449. What had the program actually done? The journal is the machine's record of messages. At 01:32:14 UTC the journal contained a message from the reporter's current run.
$ sudo journalctl -b _SYSTEMD_UNIT=camp-reporter.service _PID=1449 -o cat -n 1 --no-pager
camp-reporter ready …
The command selects this boot, this service and this process, then prints the message text. The word ready came from the program. That gives the operator evidence that it ran far enough to announce itself. The PID and time belong to the example machine.
Try reading a named service. Open Terminal on your practice machine. Debian already runs systemd-journald.service, the program that collects journal messages. Inspect it while it continues working.
systemctl cat systemd-journald.service | grep '^ExecStart='
systemctl status systemd-journald.service --no-pager
sudo journalctl -b -u systemd-journald.service -n 5 --no-pager
The first command shows the launch instruction in the installed unit. Read the program path after ExecStart=. In the second result, find Loaded, Active and Main PID. These tell you whether systemd has read the unit and whether it currently tracks a process. In the last result, read the time and sender of one entry. -b selects this boot, -u selects the unit, and -n 5 limits the display to five entries. A line from systemd[1] is the manager reporting about the service. A line from systemd-journald is a message from the journal service itself.
If the output is unfamiliar, read it again in this order. Point to the path systemd was told to launch, the number of the process it tracks now, then a dated message about the service. You can return to the reporter example at any point.
Now try a different case. The lab supplies a program file, /usr/local/bin/camp-clock, and an account named camp-clock. The program prints camp-clock tick with the time when it starts and every 15 seconds after that. Nobody has written its unit yet. Before you run these commands, which one can find the clock, and what do you expect the other one to report? If you're unsure, look back at which command read the journal service's unit.
ls -l /usr/local/bin/camp-clock
systemctl status camp-clock.service --no-pager
ls -l checks that the program file exists. systemctl status asks systemd about the named service. --no-pager keeps the result in your terminal. Compare the observations with your prediction: the file exists, but the unit is not found. That nonzero status is expected. You have not created the unit yet.
A unit file tells systemd which program to execute and which account should run it. Use sudoedit because /etc/systemd/system is protected. SUDO_EDITOR=nano chooses nano for this command, the editor used in Linux Foundations, Chapter 19, Editing text.
SUDO_EDITOR=nano sudoedit /etc/systemd/system/camp-clock.service
Type these lines into the editor:
[Unit]
Description=Camp clock
[Service]
Type=exec
User=camp-clock
ExecStart=/usr/local/bin/camp-clock
The bracketed headings group settings. Each other line is a name=value instruction. Description= labels the unit, User= selects the supplied account, and ExecStart= names the program. Type=exec makes a failure to execute the program visible when starting it; it does not prove the program keeps working.
In nano, press Ctrl+O (the letter O), then Enter to confirm the filename and save. Press Ctrl+X to exit. These are the save and exit steps in the Foundations editor lesson. Read the saved file before starting anything:
cat /etc/systemd/system/camp-clock.service
cat prints the saved file. Check the program path and account against the supplied VM facts. Now predict what should change after systemd reads this file and starts the service.
sudo systemctl daemon-reload
sudo systemctl start camp-clock.service
daemon-reload makes systemd read the new unit. start asks it to run the program now. Neither command alone proves that the program stayed running or printed a tick.
systemctl status camp-clock.service --no-pager
run_id=$(systemctl show -P InvocationID camp-clock.service)
sudo journalctl _SYSTEMD_UNIT=camp-clock.service _SYSTEMD_INVOCATION_ID="$run_id" -n 10 --no-pager
In status, find Loaded: loaded, Active: active (running) and Main PID. The second command saves the ID systemd assigned to this particular run in a shell variable named run_id. The last command asks the journal for entries belonging to both the clock unit and that run. Read the time on one camp-clock tick. You now have two observations to report, a live process and a tick emitted during its run.
In a few sentences, explain your starting prediction, why the unit names this program and account, and which two observations show that the service is running and producing messages. Use Check current service to compare your observations with the machine check. If an observation differs, read the status and journal before editing the unit again.
Next you will stop, start and restart this service. After another lesson, you will apply the unit idea to a different supplied program with fewer prompts.
References: sudoedit, systemd.service, systemd.exec, systemctl, journalctl.
Practice Give a program a service name in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.