Skip to content

Instantly share code, notes, and snippets.

Last active October 5, 2023 20:00
Star You must be signed in to star a gist
What would you like to do?
An example with an oneshot service on systemd. #systemd #systemd.service #oneshot


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 setup/teardown actions

Create executable file /opt/foo/

echo "Setting up foo ..."
touch /tmp/foo-activated

Create executable file /opt/foo/

echo "Tearing down foo ..."
if [ -f /tmp/foo-activated ]; then
    rm /tmp/foo-activated
    echo "Doesnt seem to be up: Skipping ..."

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.

Description=Setup foo



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.

Copy link

rowanj commented May 10, 2016

Just what I was looking for, thanks!

Copy link

jimlynnjulian commented Nov 12, 2016

Don't you have to include "/bin/bash" right after "ExecStart="? Example: ExecStart=/bin/bash /opt/foo/

Copy link

@jimlynnjulian you do, if your has no +x bit and a shebang (first #!/bin/bash line).

Copy link

Copy link

Copy link

stoivo commented Aug 24, 2017

This is the command to reload the systemd daemon systemctl daemon-reload

Copy link

ramseyrt commented Jan 4, 2018

How do you incorporate this script in a target (assuming I wanted to start multiple scripts as a group)?

I created /etc/systemd/system/ with the following contents:

Description=Foo Application Target
Conflicts=rescue.service rescue.service

I also created the's directory, /etc/systemd/system/, and a symbolic link to foo.service in that directory.

With this configuration, I'm able to successfully start the target using systemctl start but I'm unable to stop it properly (using systemctl stop When I stop systemctl looks like it completes but the /tmp/foo-activated file still exists meaning didn't run.

Thanks in advance!


Copy link

very useful.. thank you.

Copy link

You have to enable the service so that it starts on boot:

sudo systemctl enable foo.service

It should result in a message

Created symlink /etc/systemd/system/ → /etc/systemd/system/foo.service.

Copy link

Du you have an idea for processes like rsync in a script?
I have a backup-script, where several rsync-processes are startet to sync different home-directories, /opt, /usr/local...

When script starts the first rsync-process, it will fork an systemd stops the whole unit. My backup does not get finished.

Only RemailAfterExit=true can solve the problem in combination with Type=oneshot.
But in combinstion with systemd-timer, which starts the backup-serviice every day, it will not start backup.service on the next day, because the service is active in case of RemainAfterExit=true...

systemd.timer manpage say:

"Note that in case the unit to activate is already active at the time the timer elapses it is not restarted, but simply left running. There is no concept of spawning new service instances in this case. Due to this, services with RemainAfterExit= set (which stay around continuously even after the service's main process exited) are usually not suitable for activation via repetitive timers, as they will only be activated once, and then stay around forever."

Copy link

The problem with your setup is that systemd will have no idea when your backup is finished.
A solution would be to define a service of Type=simple (with RemainAfterExit=false) and modify your backup program to stay alive (e.g by waiting on a lock file) until all children processes have finished (successfully or not).
These kinds of services are easily activated by timers.

If the program that performs the backup is out of your control, then you should maybe resort in a plain-old cronjob.

Copy link

This description was very helpful even for configuring my service which did not require an ExecStop; now I know how to actually write these correctly

Copy link

aviezab commented Aug 31, 2021

Great, thanks for sharing!

Copy link

Thanks for sharing, helpful for begginer like me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment