Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sierrezinal/a692263119a7d29e74516c4a24f90f5b to your computer and use it in GitHub Desktop.
Save sierrezinal/a692263119a7d29e74516c4a24f90f5b to your computer and use it in GitHub Desktop.

Revisions

  1. @drmalex07 drmalex07 revised this gist Jan 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README-oneshot-systemd-service.md
    Original 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 the setup/teardown actions
    ### Define setup/teardown actions

    Create executable file `/opt/foo/setup-foo.sh`:
    ```shell
  2. @drmalex07 drmalex07 revised this gist Jan 17, 2016. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion README-oneshot-systemd-service.md
    Original file line number Diff line number Diff line change
    @@ -24,4 +24,24 @@ else
    fi
    ```

    ### Define the service unit
    ### 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.
  3. @drmalex07 drmalex07 created this gist Jan 17, 2016.
    27 changes: 27 additions & 0 deletions README-oneshot-systemd-service.md
    Original 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