Start, Stop, and Restart a Service with systemctl
In this step, you will learn how to manage the lifecycle of system services using systemctl commands. You will practice starting, stopping, and restarting a service. For this exercise, we will use a dummy service that we will create. This approach ensures that we can safely manipulate a service without affecting critical system functions.
First, let's create a simple service unit file. This file will define a service that simply writes a timestamp to a log file every few seconds.
Create a new service unit file named mytest.service directly in the systemd system directory using nano:
sudo nano /etc/systemd/system/mytest.service
Paste the following content into the nano editor:
[Unit]
Description=My Test Service
After=network.target
[Service]
Type=simple
ExecStart=/bin/bash -c 'while true; do echo "$(date): My Test Service is running." >> /tmp/mytest.log; sleep 5; done'
ExecStop=/bin/bash -c 'echo "$(date): My Test Service stopped." >> /tmp/mytest.log'
Restart=on-failure
[Install]
WantedBy=multi-user.target
[Unit]: Contains generic information about the unit. Description provides a human-readable name, and After=network.target specifies that this service should start after the network is up.
[Service]: Defines the behavior of the service.
Type=simple: Indicates a simple service type where the ExecStart command is the main process.
ExecStart: The command to execute when the service starts. Here, it's a bash loop that writes a timestamped message to /tmp/mytest.log every 5 seconds.
ExecStop: The command to execute when the service stops. It writes a stop message to the log.
Restart=on-failure: Configures the service to restart if it exits with a non-zero status.
[Install]: Contains information about how the service should be installed. WantedBy=multi-user.target means this service should be started when the system reaches the multi-user runlevel.
Save the file by pressing Ctrl+X, then Y to confirm, and Enter to save the file.
Now, reload the systemd daemon to recognize the new service file:
sudo systemctl daemon-reload
Starting a Service
To start a service, use the systemctl start command.
Execute the following command to start mytest.service. Note that we need to use sudo because systemctl operations typically require root privileges.
sudo systemctl start mytest.service
There will be no immediate output if the command is successful.
Now, verify that the service is running by checking its status:
systemctl status mytest.service
You should see output indicating that the service is active (running):
● mytest.service - My Test Service
Loaded: loaded (/etc/systemd/system/mytest.service; disabled; preset: disabled)
Active: active (running) since ...
Main PID: ... (bash)
Tasks: 2 (limit: ...)
Memory: ...
CPU: ...
CGroup: /system.slice/mytest.service
├─... /bin/bash -c "while true; do echo \"\$(date): My Test Service is running.\" >> /tmp/mytest.log; sleep 5; done"
└─... sleep 5
...output omitted...
You can also check the log file to see if the service is writing messages:
tail -f /tmp/mytest.log
You should see new lines appearing every 5 seconds, similar to this:
Tue Jul 22 09:15:09 AM CST 2025: My Test Service is running.
Tue Jul 22 09:15:14 AM CST 2025: My Test Service is running.
Press Ctrl+C to exit tail.
Stopping a Service
To stop a running service, use the systemctl stop command.
Execute the following command to stop mytest.service:
sudo systemctl stop mytest.service
Again, there will be no immediate output.
Verify that the service has stopped:
systemctl status mytest.service
The output should now show Active: inactive (dead):
○ mytest.service - My Test Service
Loaded: loaded (/etc/systemd/system/mytest.service; disabled; preset: disabled)
Active: inactive (dead) since ...
...output omitted...
Check the log file /tmp/mytest.log again. You should see the "My Test Service stopped." message and no new "running" messages appearing.
tail /tmp/mytest.log
The output will look similar to this:
Tue Jul 22 09:15:24 AM CST 2025: My Test Service is running.
Tue Jul 22 09:15:28 AM CST 2025: My Test Service stopped.
Restarting a Service
To restart a service, use the systemctl restart command. This command first stops the service and then starts it again. This is useful when you've made changes to a service's configuration and need them to take effect.
Execute the following command to restart mytest.service:
sudo systemctl restart mytest.service
Verify that the service is running again:
systemctl status mytest.service
You should see Active: active (running) again, and the Main PID will likely be a new number, indicating a new process has started.
● mytest.service - My Test Service
Loaded: loaded (/etc/systemd/system/mytest.service; disabled; preset: disabled)
Active: active (running) since ...
Main PID: ... (bash)
Tasks: 2 (limit: ...)
Memory: ...
CPU: ...
CGroup: /system.slice/mytest.service
├─... /bin/bash -c "while true; do echo \"\$(date): My Test Service is running.\" >> /tmp/mytest.log; sleep 5; done"
└─... sleep 5
...output omitted...
Check the log file /tmp/mytest.log to confirm that the service has resumed writing "running" messages.
tail -f /tmp/mytest.log
You should see a "stopped" message followed by new "running" messages:
Tue Jul 22 09:15:28 AM CST 2025: My Test Service stopped.
Tue Jul 22 09:15:40 AM CST 2025: My Test Service is running.
Press Ctrl+C to exit tail.