Forked from drmalex07/README-oneshot-systemd-service.md
Created
February 22, 2021 22:15
-
-
Save sierrezinal/a692263119a7d29e74516c4a24f90f5b to your computer and use it in GitHub Desktop.
Revisions
-
drmalex07 revised this gist
Jan 17, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ no running processes remain). A common pattern for these type of service is to b Let's create a example `foo` service that when started creates a file, and when stopped it deletes it. ### Define setup/teardown actions Create executable file `/opt/foo/setup-foo.sh`: ```shell -
drmalex07 revised this gist
Jan 17, 2016 . 1 changed file with 21 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,4 +24,24 @@ else fi ``` ### Define the service unit Now, we define the systemd unit file as `/etc/systemd/system/foo.service`. Note that we must specify `RemainAfterExit=true` so that systemd considers the service as active after the *setup* action is successfully finished. ```ini [Unit] Description=Setup foo #After=network.target [Service] Type=oneshot ExecStart=/opt/foo/setup-foo.sh RemainAfterExit=true ExecStop=/opt/foo/teardown-foo.sh StandardOutput=journal [Install] WantedBy=multi-user.target ``` Reload the systemd daemon and start the service as normally (`systemctl start foo.service`). Check the status to verify that the correct actions are taking place. -
drmalex07 created this gist
Jan 17, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ ## README Services declared as `oneshot` are expected to take some action and exit immediatelly (thus, they are not really services, no running processes remain). A common pattern for these type of service is to be defined by a *setup* and a *teardown* action. Let's create a example `foo` service that when started creates a file, and when stopped it deletes it. ### Define the setup/teardown actions Create executable file `/opt/foo/setup-foo.sh`: ```shell #!/bin/bash echo "Setting up foo ..." touch /tmp/foo-activated ``` Create executable file `/opt/foo/teardown-foo.sh`: ```shell #!/bin/bash echo "Tearing down foo ..." if [ -f /tmp/foo-activated ]; then rm /tmp/foo-activated else echo "Doesnt seem to be up: Skipping ..." fi ``` ### Define the service unit