Skip to content

Commit 8761836

Browse files
Rajakavitha1jddocs
andauthored
[update] Use systemd to Start a Linux Service at Boot (#7221)
* [update] Use systemd to Start a Linux Service at Boot Incorporated the review comments * Update index.md formatting changes * fix blueberry * copy edits and update code block formatting --------- Co-authored-by: jddocs <[email protected]>
1 parent 44fcb62 commit 8761836

File tree

1 file changed

+93
-69
lines changed
  • docs/guides/quick-answers/linux/start-service-at-boot

1 file changed

+93
-69
lines changed

docs/guides/quick-answers/linux/start-service-at-boot/index.md

Lines changed: 93 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: The systemd daemon allows you to control Linux system services. Thi
55
authors: ["Linode"]
66
contributors: ["Linode"]
77
published: 2018-05-01
8+
modified: 2025-03-27
89
keywords: ["systemd","service","enable service","Linux system service"]
910
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
1011
external_resources:
@@ -25,118 +26,141 @@ It is simple to create a custom systemd service that will run any script or proc
2526

2627
1. Create a script or executable that the service will manage. This guide uses a simple Bash script as an example:
2728

28-
{{< file "test_service.sh" bash >}}
29-
DATE=`date '+%Y-%m-%d %H:%M:%S'`
30-
echo "Example service started at ${DATE}" | systemd-cat -p info
29+
```file {title="test_service.sh" lang="bash"}
30+
DATE=`date '+%Y-%m-%d %H:%M:%S'`
31+
echo "Example service started at ${DATE}" | systemd-cat -p info
3132
32-
while :
33-
do
34-
echo "Looping...";
35-
sleep 30;
36-
done
37-
{{< /file >}}
33+
while :
34+
do
35+
echo "Looping...";
36+
sleep 30;
37+
done
38+
```
3839
3940
This script will log the time at which it is initialized, then loop infinitely to keep the service running.
4041
4142
2. Copy the script to `/usr/bin` and make it executable:
4243
43-
sudo cp test_service.sh /usr/bin/test_service.sh
44-
sudo chmod +x /usr/bin/test_service.sh
44+
```command
45+
sudo cp test_service.sh /usr/bin/test_service.sh
46+
sudo chmod +x /usr/bin/test_service.sh
47+
```
4548
46-
3. Create a **Unit file** to define a systemd service:
49+
3. Create a **Unit file** to define a systemd service in your application, where `/opt/myapp/` is the path to your application directory.
4750
48-
{{< file "/lib/systemd/system/myservice.service" conf >}}
49-
[Unit]
50-
Description=Example systemd service.
51+
```file {title="/opt/myapp/myservice.service"}
52+
[Unit]
53+
Description=Example systemd service.
5154
52-
[Service]
53-
Type=simple
54-
ExecStart=/bin/bash /usr/bin/test_service.sh
55+
[Service]
56+
Type=simple
57+
ExecStart=/bin/bash /usr/bin/test_service.sh
5558
56-
[Install]
57-
WantedBy=multi-user.target
58-
{{< /file >}}
59+
[Install]
60+
WantedBy=multi-user.target
61+
```
5962
60-
This defines a simple service. The critical part is the `ExecStart` directive, which specifies the command that will be run to start the service.
63+
* This defines a simple service that runs `/usr/bin/test_service.sh` through Bash.
64+
* The `ExecStart` directive specifies the command to run.
65+
* The `[Install]` section allows the service to be enabled at boot.
6166
62-
4. Copy the unit file to `/etc/systemd/system` and give it permissions:
67+
4. Link the unit file from your application directory. This approach can streamline deployments by keeping the service definition with your codebase:
6368
64-
sudo cp myservice.service /etc/systemd/system/myservice.service
65-
sudo chmod 644 /etc/systemd/system/myservice.service
69+
```command
70+
sudo systemctl enable /opt/myapp/myservice.service
71+
```
72+
73+
If the `[Install]` section is present, this creates the necessary symlinks in `/etc/systemd/system/`.
74+
75+
Alternatively, copy the unit file to `/etc/systemd/system/`, and adjust the permissions:
76+
77+
```command
78+
sudo cp myservice.service /etc/systemd/system/myservice.service
79+
sudo chmod 644 /etc/systemd/system/myservice.service
80+
```
6681
6782
For more information about the unit file and its available configuration options, see the [systemd documentation](https://www.freedesktop.org/wiki/Software/systemd/).
6883
6984
## Start and Enable the Service
7085
7186
1. Once you have a unit file, you are ready to test the service:
7287
73-
sudo systemctl start myservice
74-
88+
```command
89+
sudo systemctl start myservice
90+
```
7591
7692
2. Check the status of the service:
7793
78-
sudo systemctl status myservice
94+
```command
95+
sudo systemctl status myservice
96+
```
7997
8098
If the service is running correctly, the output should resemble the following:
8199
82-
{{< output >}}
83-
● myservice.service - Example systemd service.
84-
Loaded: loaded (/lib/systemd/system/myservice.service; enabled; vendor preset: enabled)
85-
Active: active (running) since Tue 2018-05-01 18:17:14 UTC; 4s ago
86-
Main PID: 16266 (bash)
87-
Tasks: 2
88-
Memory: 748.0K
89-
CPU: 4ms
90-
CGroup: /system.slice/myservice.service
91-
├─16266 /bin/bash /usr/bin/test_service.sh
92-
└─16270 sleep 30
93-
94-
May 01 18:17:14 localhost systemd[1]: Started Example systemd service..
95-
May 01 18:17:14 localhost cat[16269]: Example service started at 2018-05-01 18:17:14
96-
May 01 18:17:14 localhost bash[16266]: Looping...
97-
{{< /output >}}
100+
```output
101+
● myservice.service - Example systemd service.
102+
Loaded: loaded (/lib/systemd/system/myservice.service; enabled; vendor preset: enabled)
103+
Active: active (running) since Tue 2018-05-01 18:17:14 UTC; 4s ago
104+
Main PID: 16266 (bash)
105+
Tasks: 2
106+
Memory: 748.0K
107+
CPU: 4ms
108+
CGroup: /system.slice/myservice.service
109+
├─16266 /bin/bash /usr/bin/test_service.sh
110+
└─16270 sleep 30
111+
112+
May 01 18:17:14 localhost systemd[1]: Started Example systemd service..
113+
May 01 18:17:14 localhost cat[16269]: Example service started at 2018-05-01 18:17:14
114+
May 01 18:17:14 localhost bash[16266]: Looping...
115+
```
98116
99117
3. The service can be stopped or restarted using standard systemd commands:
100118
101-
sudo systemctl stop myservice
102-
sudo systemctl restart myservice
119+
```command
120+
sudo systemctl stop myservice
121+
sudo systemctl restart myservice
122+
```
103123
104124
4. Finally, use the `enable` command to ensure that the service starts whenever the system boots:
105125
106-
sudo systemctl enable myservice
126+
```command
127+
sudo systemctl enable myservice
128+
```
107129
108-
{{< output >}}
109-
Created symlink from /etc/systemd/system/multi-user.target.wants/myservice.service to /lib/systemd/system/myservice.service.
110-
{{< /output >}}
130+
```output
131+
Created symlink from /etc/systemd/system/multi-user.target.wants/myservice.service to /lib/systemd/system/myservice.service.
132+
```
111133
112134
5. Reboot your Linode from the Linode Manager and check the status of the service:
113135
114-
sudo systemctl status myservice
136+
```command
137+
sudo systemctl status myservice
138+
```
115139
116140
You should see that the service logged its start time immediately after booting:
117141
118-
{{< output >}}
119-
● myservice.service - Example systemd service.
120-
Loaded: loaded (/usr/lib/systemd/system/myservice.service; enabled; vendor preset: disabled)
121-
Active: active (running) since Wed 2018-05-02 15:03:07 UTC; 48s ago
122-
Main PID: 2973 (bash)
123-
CGroup: /system.slice/myservice.service
124-
├─2973 /bin/bash /usr/bin/test_service.sh
125-
└─3371 sleep 30
126-
127-
May 02 15:03:07 localhost systemd[1]: Started Example systemd service..
128-
May 02 15:03:07 localhost systemd[1]: Starting Example systemd service....
129-
May 02 15:03:07 localhost bash[2973]: Looping...
130-
May 02 15:03:37 localhost bash[2973]: Looping...
131-
{{< /output >}}
142+
```output
143+
● myservice.service - Example systemd service.
144+
Loaded: loaded (/usr/lib/systemd/system/myservice.service; enabled; vendor preset: disabled)
145+
Active: active (running) since Wed 2018-05-02 15:03:07 UTC; 48s ago
146+
Main PID: 2973 (bash)
147+
CGroup: /system.slice/myservice.service
148+
├─2973 /bin/bash /usr/bin/test_service.sh
149+
└─3371 sleep 30
132150
133-
For more information about using `systemctl` commands, see the [systemctl guide](/docs/guides/introduction-to-systemctl).
151+
May 02 15:03:07 localhost systemd[1]: Started Example systemd service..
152+
May 02 15:03:07 localhost systemd[1]: Starting Example systemd service....
153+
May 02 15:03:07 localhost bash[2973]: Looping...
154+
May 02 15:03:37 localhost bash[2973]: Looping...
155+
```
134156
157+
For more information about using `systemctl` commands, see the [systemctl guide](/docs/guides/introduction-to-systemctl).
135158
136159
## Troubleshooting
137160
138-
- "Example service started at ..." line does not appear in the output of the status command. The `systemd-cat` output is not reliable because of a race condition. As a workaround update the `test_service.sh` file as follows:
139-
{{< file "test_service.sh" bash >}}
161+
If the `Example service started at ...` line does not appear in the output of the status command, the `systemd-cat` output may not be reliable because of a race condition. As a workaround, you can update the `test_service.sh` file as follows:
162+
163+
```file {title="test_service.sh" lang="bash"}
140164
info=/tmp/myservice-systemd-cat-pipe-info
141165
mkfifo "$info"
142166
trap "exec 3>&-; rm $info" EXIT
@@ -151,4 +175,4 @@ do
151175
echo "Looping...";
152176
sleep 30;
153177
done
154-
{{< /file >}}
178+
```

0 commit comments

Comments
 (0)