Learn › Build and operate services › 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.
Inspect the starting state:
systemctl status camp-clock.service --no-pager
cat /etc/systemd/system/camp-clock.service
Find Active: active (running) and Main PID. Check that the unit names /usr/local/bin/camp-clock and User=camp-clock.
systemctl show -P InvocationID camp-clock.service
show -P InvocationID prints the ID of the current service run. Record it so you can tell whether a later command creates a different run.
Suppose someone asks you to run start now. Predict whether that command will create a new run when the service is already active. Explain your reason. Then inspect the result:
sudo systemctl start camp-clock.service
systemctl status camp-clock.service --no-pager
start leaves this already active service running. Use restart when you need a fresh run.
Run systemctl show -P InvocationID camp-clock.service again. Its ID should match the one you recorded.
An operator needs the clock paused. Predict what happens to the process, the saved unit and older ticks. Then stop it and inspect:
sudo systemctl stop camp-clock.service
systemctl status camp-clock.service --no-pager
sudo journalctl -u camp-clock.service -n 10 --no-pager
stop asks systemd to end this run. Look for Active: inactive (dead) and no current Main PID. status may return a nonzero exit status; read its displayed state. The saved unit remains loaded, and the journal can still show earlier ticks. An old tick does not mean the clock is running now.
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.
After Lesson 3, 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.