Learn › Build and operate services › Give a program a service name

Give a program a service name

1. Give a program a service name

Turn a program into a service you can start and inspect. The lab supplies /usr/local/bin/camp-clock and a camp-clock account. The program prints camp-clock tick with the current time as soon as it runs, then every 15 seconds. If you run it yourself, it occupies your terminal.

A running program is a process. On this VM, systemd is the service manager: it starts and tracks processes described by unit files. A .service unit gives a program a service name. The manager runs it independently of your terminal, leaving the terminal free for other work. You will inspect the starting state, write and start a unit, then verify both the process and its message.

Inspect the starting state

The program exists, but this lab starts without camp-clock.service. Predict what systemd can report for a service name it has not been given.

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.

Write and start the unit

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.

Verify the process and its message

systemctl status camp-clock.service --no-pager
sudo journalctl -u camp-clock.service -n 10 --no-pager

In status, find Loaded: loaded, Active: active (running), and Main PID. A PID is a process number. These observations show systemd loaded the unit and currently tracks a running process. The journal collects messages. In journalctl, -u selects this service, and -n 10 shows its ten latest entries. Find a camp-clock tick stamped during this run. An old entry alone would not show what the current process did.

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. If either observation differs, read what status and the journal say before changing the unit.

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.