From ca65fa6ce2acdaf7a5dcbd5bbcc089c9df1544fc Mon Sep 17 00:00:00 2001 From: Rajakavitha Kodhandapani Date: Tue, 18 Mar 2025 19:23:09 +0530 Subject: [PATCH 1/4] [update] Use systemd to Start a Linux Service at Boot Incorporated the review comments --- .../linux/start-service-at-boot/index.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/guides/quick-answers/linux/start-service-at-boot/index.md b/docs/guides/quick-answers/linux/start-service-at-boot/index.md index e4ab6e271f4..bf9f53fb3d0 100644 --- a/docs/guides/quick-answers/linux/start-service-at-boot/index.md +++ b/docs/guides/quick-answers/linux/start-service-at-boot/index.md @@ -43,9 +43,9 @@ done sudo cp test_service.sh /usr/bin/test_service.sh sudo chmod +x /usr/bin/test_service.sh -3. Create a **Unit file** to define a systemd service: +3. Create a **Unit file** to define a systemd service in your application such as `/opt/myapp/`. - {{< file "/lib/systemd/system/myservice.service" conf >}} + {{< file "/opt/myapp/myservice.service" conf >}} [Unit] Description=Example systemd service. @@ -57,12 +57,19 @@ ExecStart=/bin/bash /usr/bin/test_service.sh WantedBy=multi-user.target {{< /file >}} - This defines a simple service. The critical part is the `ExecStart` directive, which specifies the command that will be run to start the service. + * This defines a simple service that runs `/usr/bin/test_service.sh` through Bash. + * The `ExecStart` directive specifies the command to run. + * The `[Install]` section allows the service to be enabled at boot. +4. Link the unit file from your application directory. This approach can streamline deployments by keeping the service definition with your codebase: -4. Copy the unit file to `/etc/systemd/system` and give it permissions: + sudo systemctl enable /opt/myapp/myservice.service + + If the [Install] section is present, this creates the necessary symlinks in `/etc/systemd/system/`. - sudo cp myservice.service /etc/systemd/system/myservice.service - sudo chmod 644 /etc/systemd/system/myservice.service + Alternatively, copy the unit file to `/etc/systemd/system/` and give permissions: + + sudo cp myservice.service /etc/systemd/system/myservice.service + sudo chmod 644 /etc/systemd/system/myservice.service For more information about the unit file and its available configuration options, see the [systemd documentation](https://www.freedesktop.org/wiki/Software/systemd/). From ac205d67ad2d23bd62ba6e4e5812387812740779 Mon Sep 17 00:00:00 2001 From: Rajakavitha Kodhandapani Date: Tue, 18 Mar 2025 20:24:51 +0530 Subject: [PATCH 2/4] Update index.md formatting changes --- .../linux/start-service-at-boot/index.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/guides/quick-answers/linux/start-service-at-boot/index.md b/docs/guides/quick-answers/linux/start-service-at-boot/index.md index bf9f53fb3d0..6955fcbf265 100644 --- a/docs/guides/quick-answers/linux/start-service-at-boot/index.md +++ b/docs/guides/quick-answers/linux/start-service-at-boot/index.md @@ -57,19 +57,19 @@ ExecStart=/bin/bash /usr/bin/test_service.sh WantedBy=multi-user.target {{< /file >}} - * This defines a simple service that runs `/usr/bin/test_service.sh` through Bash. - * The `ExecStart` directive specifies the command to run. - * The `[Install]` section allows the service to be enabled at boot. + * This defines a simple service that runs `/usr/bin/test_service.sh` through Bash. + * The `ExecStart` directive specifies the command to run. + * The `[Install]` section allows the service to be enabled at boot. 4. Link the unit file from your application directory. This approach can streamline deployments by keeping the service definition with your codebase: - sudo systemctl enable /opt/myapp/myservice.service + sudo systemctl enable /opt/myapp/myservice.service If the [Install] section is present, this creates the necessary symlinks in `/etc/systemd/system/`. - Alternatively, copy the unit file to `/etc/systemd/system/` and give permissions: + Alternatively, copy the unit file to `/etc/systemd/system/` and give permissions: - sudo cp myservice.service /etc/systemd/system/myservice.service - sudo chmod 644 /etc/systemd/system/myservice.service + sudo cp myservice.service /etc/systemd/system/myservice.service + sudo chmod 644 /etc/systemd/system/myservice.service For more information about the unit file and its available configuration options, see the [systemd documentation](https://www.freedesktop.org/wiki/Software/systemd/). From 9a2ce0a5bff1b79d32e76d8dc7981cbcc6684412 Mon Sep 17 00:00:00 2001 From: Rajakavitha Kodhandapani Date: Tue, 18 Mar 2025 20:31:20 +0530 Subject: [PATCH 3/4] fix blueberry --- .../quick-answers/linux/start-service-at-boot/index.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/guides/quick-answers/linux/start-service-at-boot/index.md b/docs/guides/quick-answers/linux/start-service-at-boot/index.md index 6955fcbf265..43b7a3349db 100644 --- a/docs/guides/quick-answers/linux/start-service-at-boot/index.md +++ b/docs/guides/quick-answers/linux/start-service-at-boot/index.md @@ -63,7 +63,7 @@ WantedBy=multi-user.target 4. Link the unit file from your application directory. This approach can streamline deployments by keeping the service definition with your codebase: sudo systemctl enable /opt/myapp/myservice.service - + If the [Install] section is present, this creates the necessary symlinks in `/etc/systemd/system/`. Alternatively, copy the unit file to `/etc/systemd/system/` and give permissions: @@ -79,7 +79,6 @@ WantedBy=multi-user.target sudo systemctl start myservice - 2. Check the status of the service: sudo systemctl status myservice @@ -139,7 +138,6 @@ May 02 15:03:37 localhost bash[2973]: Looping... For more information about using `systemctl` commands, see the [systemctl guide](/docs/guides/introduction-to-systemctl). - ## Troubleshooting - "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: @@ -158,4 +156,4 @@ do echo "Looping..."; sleep 30; done -{{< /file >}} +{{< /file >}} From b4d04025595cc96de734e9e8a8d9e4e7c1c769de Mon Sep 17 00:00:00 2001 From: jddocs Date: Thu, 27 Mar 2025 14:46:09 -0400 Subject: [PATCH 4/4] copy edits and update code block formatting --- .../linux/start-service-at-boot/index.md | 155 ++++++++++-------- 1 file changed, 87 insertions(+), 68 deletions(-) diff --git a/docs/guides/quick-answers/linux/start-service-at-boot/index.md b/docs/guides/quick-answers/linux/start-service-at-boot/index.md index 43b7a3349db..8fc202426cb 100644 --- a/docs/guides/quick-answers/linux/start-service-at-boot/index.md +++ b/docs/guides/quick-answers/linux/start-service-at-boot/index.md @@ -5,6 +5,7 @@ description: The systemd daemon allows you to control Linux system services. Thi authors: ["Linode"] contributors: ["Linode"] published: 2018-05-01 +modified: 2025-03-27 keywords: ["systemd","service","enable service","Linux system service"] license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)' external_resources: @@ -25,51 +26,58 @@ It is simple to create a custom systemd service that will run any script or proc 1. Create a script or executable that the service will manage. This guide uses a simple Bash script as an example: - {{< file "test_service.sh" bash >}} -DATE=`date '+%Y-%m-%d %H:%M:%S'` -echo "Example service started at ${DATE}" | systemd-cat -p info + ```file {title="test_service.sh" lang="bash"} + DATE=`date '+%Y-%m-%d %H:%M:%S'` + echo "Example service started at ${DATE}" | systemd-cat -p info -while : -do -echo "Looping..."; -sleep 30; -done -{{< /file >}} + while : + do + echo "Looping..."; + sleep 30; + done + ``` This script will log the time at which it is initialized, then loop infinitely to keep the service running. 2. Copy the script to `/usr/bin` and make it executable: - sudo cp test_service.sh /usr/bin/test_service.sh - sudo chmod +x /usr/bin/test_service.sh + ```command + sudo cp test_service.sh /usr/bin/test_service.sh + sudo chmod +x /usr/bin/test_service.sh + ``` -3. Create a **Unit file** to define a systemd service in your application such as `/opt/myapp/`. +3. Create a **Unit file** to define a systemd service in your application, where `/opt/myapp/` is the path to your application directory. - {{< file "/opt/myapp/myservice.service" conf >}} -[Unit] -Description=Example systemd service. + ```file {title="/opt/myapp/myservice.service"} + [Unit] + Description=Example systemd service. -[Service] -Type=simple -ExecStart=/bin/bash /usr/bin/test_service.sh + [Service] + Type=simple + ExecStart=/bin/bash /usr/bin/test_service.sh -[Install] -WantedBy=multi-user.target -{{< /file >}} + [Install] + WantedBy=multi-user.target + ``` * This defines a simple service that runs `/usr/bin/test_service.sh` through Bash. * The `ExecStart` directive specifies the command to run. * The `[Install]` section allows the service to be enabled at boot. + 4. Link the unit file from your application directory. This approach can streamline deployments by keeping the service definition with your codebase: - sudo systemctl enable /opt/myapp/myservice.service + ```command + sudo systemctl enable /opt/myapp/myservice.service + ``` - If the [Install] section is present, this creates the necessary symlinks in `/etc/systemd/system/`. + If the `[Install]` section is present, this creates the necessary symlinks in `/etc/systemd/system/`. - Alternatively, copy the unit file to `/etc/systemd/system/` and give permissions: + Alternatively, copy the unit file to `/etc/systemd/system/`, and adjust the permissions: - sudo cp myservice.service /etc/systemd/system/myservice.service - sudo chmod 644 /etc/systemd/system/myservice.service + ```command + sudo cp myservice.service /etc/systemd/system/myservice.service + sudo chmod 644 /etc/systemd/system/myservice.service + ``` For more information about the unit file and its available configuration options, see the [systemd documentation](https://www.freedesktop.org/wiki/Software/systemd/). @@ -77,71 +85,82 @@ WantedBy=multi-user.target 1. Once you have a unit file, you are ready to test the service: - sudo systemctl start myservice + ```command + sudo systemctl start myservice + ``` 2. Check the status of the service: - sudo systemctl status myservice + ```command + sudo systemctl status myservice + ``` If the service is running correctly, the output should resemble the following: - {{< output >}} -● myservice.service - Example systemd service. - Loaded: loaded (/lib/systemd/system/myservice.service; enabled; vendor preset: enabled) - Active: active (running) since Tue 2018-05-01 18:17:14 UTC; 4s ago - Main PID: 16266 (bash) - Tasks: 2 - Memory: 748.0K - CPU: 4ms - CGroup: /system.slice/myservice.service - ├─16266 /bin/bash /usr/bin/test_service.sh - └─16270 sleep 30 - -May 01 18:17:14 localhost systemd[1]: Started Example systemd service.. -May 01 18:17:14 localhost cat[16269]: Example service started at 2018-05-01 18:17:14 -May 01 18:17:14 localhost bash[16266]: Looping... -{{< /output >}} + ```output + ● myservice.service - Example systemd service. + Loaded: loaded (/lib/systemd/system/myservice.service; enabled; vendor preset: enabled) + Active: active (running) since Tue 2018-05-01 18:17:14 UTC; 4s ago + Main PID: 16266 (bash) + Tasks: 2 + Memory: 748.0K + CPU: 4ms + CGroup: /system.slice/myservice.service + ├─16266 /bin/bash /usr/bin/test_service.sh + └─16270 sleep 30 + + May 01 18:17:14 localhost systemd[1]: Started Example systemd service.. + May 01 18:17:14 localhost cat[16269]: Example service started at 2018-05-01 18:17:14 + May 01 18:17:14 localhost bash[16266]: Looping... + ``` 3. The service can be stopped or restarted using standard systemd commands: - sudo systemctl stop myservice - sudo systemctl restart myservice + ```command + sudo systemctl stop myservice + sudo systemctl restart myservice + ``` 4. Finally, use the `enable` command to ensure that the service starts whenever the system boots: - sudo systemctl enable myservice + ```command + sudo systemctl enable myservice + ``` - {{< output >}} -Created symlink from /etc/systemd/system/multi-user.target.wants/myservice.service to /lib/systemd/system/myservice.service. -{{< /output >}} + ```output + Created symlink from /etc/systemd/system/multi-user.target.wants/myservice.service to /lib/systemd/system/myservice.service. + ``` 5. Reboot your Linode from the Linode Manager and check the status of the service: - sudo systemctl status myservice + ```command + sudo systemctl status myservice + ``` You should see that the service logged its start time immediately after booting: - {{< output >}} -● myservice.service - Example systemd service. - Loaded: loaded (/usr/lib/systemd/system/myservice.service; enabled; vendor preset: disabled) - Active: active (running) since Wed 2018-05-02 15:03:07 UTC; 48s ago - Main PID: 2973 (bash) - CGroup: /system.slice/myservice.service - ├─2973 /bin/bash /usr/bin/test_service.sh - └─3371 sleep 30 + ```output + ● myservice.service - Example systemd service. + Loaded: loaded (/usr/lib/systemd/system/myservice.service; enabled; vendor preset: disabled) + Active: active (running) since Wed 2018-05-02 15:03:07 UTC; 48s ago + Main PID: 2973 (bash) + CGroup: /system.slice/myservice.service + ├─2973 /bin/bash /usr/bin/test_service.sh + └─3371 sleep 30 -May 02 15:03:07 localhost systemd[1]: Started Example systemd service.. -May 02 15:03:07 localhost systemd[1]: Starting Example systemd service.... -May 02 15:03:07 localhost bash[2973]: Looping... -May 02 15:03:37 localhost bash[2973]: Looping... -{{< /output >}} + May 02 15:03:07 localhost systemd[1]: Started Example systemd service.. + May 02 15:03:07 localhost systemd[1]: Starting Example systemd service.... + May 02 15:03:07 localhost bash[2973]: Looping... + May 02 15:03:37 localhost bash[2973]: Looping... + ``` -For more information about using `systemctl` commands, see the [systemctl guide](/docs/guides/introduction-to-systemctl). + For more information about using `systemctl` commands, see the [systemctl guide](/docs/guides/introduction-to-systemctl). ## Troubleshooting -- "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: -{{< file "test_service.sh" bash >}} +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: + +```file {title="test_service.sh" lang="bash"} info=/tmp/myservice-systemd-cat-pipe-info mkfifo "$info" trap "exec 3>&-; rm $info" EXIT @@ -156,4 +175,4 @@ do echo "Looping..."; sleep 30; done -{{< /file >}} +```