Learn › Build and operate services › Control the running service

Control the running service

2. Control the running service

A unit file is the saved instruction for starting a program. A process is one run of that program. Stopping the service ends the run but keeps the unit and earlier journal messages.

This fresh machine recreates the clock service from Lesson 1 and starts it for you. Each time systemd starts a new run of a service, it assigns an invocation ID. Checking that ID on either side of a command lets you tell whether the same run continued.

What happens when the clock is already running? In a Debian lab test, an active clock kept the same invocation ID after another start. It acquired a different ID after a stop and a start. Read the worked example below. Its commands show those two operations and its ID values are illustrative. Your machine will assign its own values.

$ systemctl show -P InvocationID camp-clock.service
ea144a868a944bc19c9f21be283f1637
$ sudo systemctl start camp-clock.service
$ systemctl show -P InvocationID camp-clock.service
ea144a868a944bc19c9f21be283f1637

The first reading identifies the clock that was already running. start asks systemd to have a running clock. It usually succeeds silently. The second reading is identical, which tells us the original run continued.

$ sudo systemctl stop camp-clock.service
$ systemctl is-active camp-clock.service
inactive
$ sudo systemctl start camp-clock.service
$ systemctl show -P InvocationID camp-clock.service
6e87e49c44ea4c4bae9025cd6a0cbb24

During the pause there was no running clock. The saved unit still described how to launch it. Starting it from that state produced a different ID, so an operator looking for fresh output would select messages from this second run.

Read the two sides of a start yourself. Use the practice terminal. First find Active: active (running) and Main PID in the status. Read the saved unit to find the clock program and account.

systemctl status camp-clock.service --no-pager
cat /etc/systemd/system/camp-clock.service

Now save the current ID, ask for the service to be running, and read the ID again.

before=$(systemctl show -P InvocationID camp-clock.service)
sudo systemctl start camp-clock.service
after=$(systemctl show -P InvocationID camp-clock.service)
printf 'Before start: %s\nAfter start:  %s\n' "$before" "$after"

show -P InvocationID asks for the run ID alone. The shell stores the two readings as before and after. The last line prints them with labels. Compare the complete strings. When both are present and match, you have evidence that this request left the original run in place. Read a message belonging to that run.

sudo journalctl _SYSTEMD_UNIT=camp-clock.service _SYSTEMD_INVOCATION_ID="$after" -n 5 --no-pager

Find a camp-clock tick and its time. If a reading is empty or the IDs differ, inspect the status again. When the clock is active and stable, you can repeat the commands and the comparison. Keep the first run ID so that you can recognize its messages after a pause. Use Check current service to record the running state in the practice panel.

Stop for a pause

This time the clock needs to pause so it produces no new ticks. Before you act, decide whether another start would meet that request. After a stop, which of these should you still be able to find: a current clock process, its saved unit, or a tick from the run you just saw? Use the worked example again if you need it. Then test your prediction.

sudo systemctl stop camp-clock.service
systemctl status camp-clock.service --no-pager
cat /etc/systemd/system/camp-clock.service
sudo journalctl _SYSTEMD_UNIT=camp-clock.service _SYSTEMD_INVOCATION_ID="$before" -n 5 --no-pager

stop asks systemd to end this run. Look for Active: inactive (dead). The status display may still include the PID of the process that exited. Read the word beside it. In this state the saved file is still readable, and the journal can show ticks from your recorded run even after the process has ended. Use Check again to compare the stopped state with the preceding observation.

Start it again, then restart it

Predict what start should do to an inactive service and what evidence would distinguish the new run from journal history.

sudo systemctl start camp-clock.service
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

start creates a process from the saved unit. Check Active: active (running) and a run ID different from the first one. The shell stores this ID in run_id; the journal command selects messages labeled with it. Find a camp-clock tick from this run. The clock prints one when it starts.

Suppose you need a fresh run of an already active service. Predict what should remain the same and what should be new. Then run:

sudo systemctl restart camp-clock.service
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

restart ends the current run and starts another from the same unit. Check a changed run ID, Active: active (running) and a tick in this run's journal. Use the run ID for comparison because a PID can eventually be reused.

Write a short operating note: when would you choose stop, start or restart? Use the active state and a current tick to support your decision. Explain why the saved unit and journal history remain after stop. If a service is already active and working, justify leaving it alone.

In Lesson 3 you will meet this decision again. The clock will already be healthy, and its saved launch instruction will change. Use the current arguments and a new run ID to decide whether that edit reached the running program. After that, you will build a service for a different supplied program with fewer prompts.

References: systemctl, systemd.service, systemd.exec, journal fields, journalctl.

Practice Control the running service in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.