Problem running script using systemd service

Hi!

First, I’m still pretty new to using linux in general and to bash scripting, so apologies for any obvious mistakes I’ve made here.

I have installed kdocker and I use it to keep an instance of firefox running in the system tray. There are a couple of reasons I do this, but it’s mostly a convenient way to avoid having to re-log into bitwarden every time I close the browser without having to set my vault timeout to “never.” I have actually set kdocker and firefox to autostart like this. However, whenever I reboot the system, kdocker causes it to hang and I have to wait for an extra 2 minutes for a stop job to run.

I wanted to avoid having to manually exit out of kdocker before I do a reboot, so I wrote this very basic kill_kdocker.sh bash script.

#!/bin/bash

killall kdocker

and I’ve placed it in /usr/local/sbin

After reading up a bit about systemd services, I then attempted to create the following “killkdocker.service” systemd service that would run the script before a reboot or shutdown occurs.

[Unit]
Description=Kill kdocker before reboot
Before=reboot.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/kill_kdocker.sh
StandardOutput=journal

[Install]
WantedBy=reboot.target

I can enable the service with systemctl enable killkdocker.service, but when I run systemctl start killkdocker.service, the killall kdocker command in my script runs immediately instead of waiting until a reboot starts. So kdocker is immediately terminated.

When I run systemctl status killkdocker.service I get the following output:

● kdockerkill.service - Kill kdocker before reboot
Loaded: loaded (/etc/systemd/system/kdockerkill.service; enabled; vendor preset: disabled)
Active: inactive (dead)

I am sure there is something obvious that I am missing here, but I can’t figure it out. I’m not sure if the issue is with my systemd service file, or with my simple bash script. Like I said, this is pretty new for me. I have also tried using “poweroff.target,” but I get the same result.

How can I set this up so my kill kdocker script only runs before a reboot or shutdown?

If anyone can shed some light on the probably straight forward mistake I’m making, it would be greatly appreciated!

First of all if it is meant to keep your firefox open then that means it needs to run under your user account which will only be active after a login.
So you should not create a system-wide service for that purpose, but instead you need a per-user service.
eg. under ~/.config/systemd/user

That said:
When you “start” a service it is indeed run, because you explicitly ask for it.
Systemd units that are “waiting” to be activated are all shown as “loaded” and “enabled”…

Thanks for the reply!

I disabled the system wide service and instead moved it to the location you specified, but now when I run the systemctl enable it says the service does not exist. Is there another step I need to take for a per-user service to be enabled?

Edit: And thanks for laying out what should have been more obvious to me about when I “start” a service. I knew I was missing something painfully straight forward…

Edit2: Answered my own question. For anyone who finds this later. You need to run the following:

systemctl --user daemon-reload

And then you can enable it with:

systemctl --user enable <your.service>

Hey thanks again, but ultimately, my problem is still not solved. I moved the service to the .config/systemd/user directory and can enable it, but it doesn’t actually trigger my script on reboot.

Is there something wrong with the way I’ve written my service that is stopping it from triggering the bash script before a reboot?

If I were you I would just drop my script in /usr/lib/systemd/system-shutdown.

More info:

man systemd-halt.service

(PS - for your service the issue is probably because you are missing the Before variables)

Thanks for the reply. I’ve tried removing my service and placing my killall kdocker script in /usr/lib/systemd/system-shutdown instead, but it doesn’t actually seem to work. The problem of kdocker causing a 2 minute stop job when I reboot still happens. And I have isolated kdocker with firefox as the cause of the stop job.

So I guess I’m back to trying the systemd service. Any advice on how I should be setting the Before variables? Or a link to where I can find it? When searching for this issue, the examples of similar systemd services didn’t actually include those.

For reference, I found a similar example on the archived forum from a couple of years ago and a probably very outdated reddit discussion from 6 years ago…

An example would be … let me find a link …

Thanks again, I actually had already looked at that!

But I’m still confused about my problem. I did include Before=reboot.target in my service. Am I missing something else?

Oh sorry … I missed that somehow.

Anyways … I might try something like this:

[Unit]
Description=Throw off kdock with concrete shoes
DefaultDependencies=no
After=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/kill_kdocker.sh

[Install]
WantedBy=shutdown.target

See the bottom of this page:
https://www.freedesktop.org/software/systemd/man/bootup.html

That seems to have solved it!

Switching to After=shutdown.target instead of Before=reboot.target seems to effectively launch my script and kill the kdocker process. I no longer have a 2 minute stop job running when I reboot with firefox in kdocker.

Thanks for your help and the useful link! The issue was clearly connected to when the script was triggered. I think the previous version of my service was trying to run the script too late in the shut down process and therefore not actually preventing the stop job.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.