Send request on shutdown

Hello Garuda users.

I have installed Garuda KDE for my laptop at my work. I would like to send SOAP request every time the laptop is shutting down. I found from manjaro / arch forums that running bash script on shutdown should be absolutelly doable. Although I still can not make it work. This is what I have now:

# custom systemd unit file
# file: /etc/systemd/system/customlogoff.service
#

[Unit]
Description=Run logoff task at shutdown
DefaultDependencies=no
Before=shutdown.target

Type=oneshot
ExecStart=/home/ryxwaer/Documents/automated_jobs/LogOff/timeshift.sh
TimeoutSec=3

[Install]
WantedBy=shutdown.target
#!/bin/sh 
# file: /home/ryxwaer/Documents/automated_jobs/LogOff/timeshift.sh

caflou_auth="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

curl -u "user:password" \
	--header "Content-Type: text/xml;charset=UTF-8" \
	--data \
	'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gs="http://www.baeldung.com/springsoap/gen"> \
	<soapenv:Header/> \ 
	<soapenv:Body> \ 
	<caflou_auth>'$caflou_auth'</caflou_auth> \ 
	</soapenv:Body> \ 
	</soapenv:Envelope>' \
	https://xxxxx-iflmap.hcisbt.eu1.hana.ondemand.com/cxf/worktime

Please could you point me in the right direction? I run out of ideas on what to try :confused:

Isn’t this a bit short?

Before=shutdown.target

Try adding NetworkManager.service there. You use curl so your script needs networking.

If you replace your script with something else, does it work?

2 Likes

Thanks for the quick answer but could you please be more specific on how to add NetworkManager.service? Should I add it as .target to the line WantedBy? For example like so: WantedBy=shutdown.target,NetworkManager.target? I am pretty new in the systemd services.

For your other suggestions. Timeout 3s should be enough as this request is executed in milliseconds. I tried to add mkdir ./test/ to the beginning of the executed script but I see no folder created so I quess it isn't working at all. But journalctl is showing some logs:

-- Boot f1360fdea930490f9cff9c5d04983c9d --
dec 02 14:44:06 ryxwaer-zenbook systemd[1]: Starting Run logoff task at shutdown...
dec 02 14:44:06 ryxwaer-zenbook timeshift.sh[4056]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
dec 02 14:44:06 ryxwaer-zenbook timeshift.sh[4056]:

Any ideas?

Forgive the trivial question, but just in case...
Have you given executable permission to the script?

1 Like

I think you would add

Before=shutdown.target network.target

under the [Unit] section. If you want it to run when you are rebooting also (instead of just shutting down), you need to add reboot.target as well (Before=shutdown.target reboot.target network.target).

It is convention to store scripts the system needs access to in /usr/local/bin, instead of a user’s home directory. I can’t remember if it will stop the service from working or not, but if it’s not working it might be worth moving it over just in case.

Where are you looking for the folder? That is not a full path so it is probably adding the directory to the home of root (not the home of ryxwaer), since systemd runs as root.

:point_up_2: It’s easy to forget this! chmod +x /path/to/script

If you still can’t get it working, it might be helpful to check what the service is logging for clues.

journalctl -u customlogoff.service
4 Likes

Sure I do
image

Oh I am sorry but it is not working. I have just turned off my laptop and nothing has happened.
I must have run the script manually by mistake.
I checked journalctl ant it isn't looking good

-- Boot 1ecf3704d49d43149ca2a651f15cd46a --
dec 02 18:57:20 ryxwaer-zenbook timeshift.sh[313]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
dec 02 18:57:20 ryxwaer-zenbook timeshift.sh[313]:                                  Dload  Upload   Total   Spent    Left  Speed
dec 02 18:57:20 ryxwaer-zenbook timeshift.sh[313]: [154B blob data]
dec 02 18:57:20 ryxwaer-zenbook systemd[1]: customlogoff.service: Main process exited, code=exited, status=6/NOTCONFIGURED
dec 02 18:57:20 ryxwaer-zenbook systemd[1]: customlogoff.service: Failed with result 'exit-code'.
dec 02 18:57:20 ryxwaer-zenbook systemd[1]: Failed to start Run logoff task at shutdown.
dec 02 19:02:16 ryxwaer-zenbook systemd[1]: Starting Run logoff task at shutdown...
dec 02 19:02:16 ryxwaer-zenbook timeshift.sh[4386]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
dec 02 19:02:16 ryxwaer-zenbook timeshift.sh[4386]:                                  Dload  Upload   Total   Spent    Left  Speed
dec 02 19:02:16 ryxwaer-zenbook timeshift.sh[4386]: [154B blob data]
dec 02 19:02:16 ryxwaer-zenbook systemd[1]: customlogoff.service: Main process exited, code=exited, status=6/NOTCONFIGURED
dec 02 19:02:16 ryxwaer-zenbook systemd[1]: customlogoff.service: Failed with result 'exit-code'.
dec 02 19:02:16 ryxwaer-zenbook systemd[1]: Failed to start Run logoff task at shutdown.

Alright, it looks like the network is coming down before the script runs. According to this resource:

At shutdown, a unit that is ordered after network.target will be stopped before the network — to whatever level it might be set up by then — is shut down. It is hence useful when writing service files that require network access on shutdown, which should order themselves after this target, but not pull it in.

It seems a little counterintuitive, but it looks like you are going to want it set to After=network.target.

[Unit]
Description=Run logoff task at shutdown
Before=shutdown.target
After=network.target
3 Likes

Hi,
So after the weekend I finally have some time to try out your suggestions.
A few days ago I also came across a threat where the network.target was put into After command but I couldn't believe it could work so I ignored it. Now I tried it as you suggested but unfortunately it is still not working. Is there some way of getting more specific log from systemd? Actual log is as follows:

-- Boot f2280f3397274e06aee0390368081f95 --
dec 04 17:55:28 ryxwaer-zenbook timeshift.sh[608]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
dec 04 17:55:28 ryxwaer-zenbook timeshift.sh[608]:                                  Dload  Upload   Total   Spent    Left  Speed
dec 04 17:55:28 ryxwaer-zenbook timeshift.sh[608]: [154B blob data]
dec 04 17:55:27 ryxwaer-zenbook systemd[1]: Starting Run logoff task at shutdown...
dec 04 17:55:28 ryxwaer-zenbook systemd[1]: customlogoff.service: Main process exited, code=exited, status=6/NOTCONFIGURED
dec 04 17:55:28 ryxwaer-zenbook systemd[1]: customlogoff.service: Failed with result 'exit-code'.
dec 04 17:55:28 ryxwaer-zenbook systemd[1]: Failed to start Run logoff task at shutdown.

Did you reload the daemon afterward?

sudo systemctl daemon-reload

And restart the service.

sudo systemctl restart customlogoff.service

Let’s double-check the permissions on the script:

ls -l /usr/local/bin/timeshift.sh

What version of the log are you looking at? You can pull up the journal for just this unit:

journalctl -u customlogoff.service
1 Like

Yes I have done that.

I was just enabling it so that could be the issue. I will try to restart the service and system now.

returns: .rwxr-xr-x 645 root 2 dec 19:13  /usr/local/bin/timeshift.sh

Restarting the service doesn't help. The log looks the same still. :cry:

It is interesting that the service is just outright failing. Even if something is wrong with the script, I do not think that would cause the service to fail. That leads me to believe there is a misconfiguration in the service.

Do you still have this line in there? I am thinking this may need to be removed, or if you really must have it you may need to add a Conflicts= line.

How about trying:

[Unit]

Requires=network-online.target

You should get more of the logs, not only the service part. We have to see in what phase it is invoked and previous components/services/targets activities.

It is reported as such!! :rofl:


IIUC, if it is removed, the default deps will start it at boot and run the script.
OTOH, if it is removed, they can replace ExecStart= with ExecStop= to run the script, so it will be started, and when shutdown starts, it has to be stopped, and the script will be triggered. I guess that in this case, Conflicts= with shutdown, halt, reboot might have a meaning to the overall process.

man says that this is a one-time-only invocation, during boot, and is better using network.target when you need network.

Also, since the script is on the file system, RequiresMount* something would make sure that FSs are not unmounted, and the script will be accessible.

Disclaimer: I have not tested or or have any real expereience on services, since I did not have the need (yet). Thus, test yourself whatever I advise :wink: .

Here is a similar case and solution. Service included.

Edit:
There is also a cleaner option IIUC, using a custom target unit.
Example:

  • Target unit sendmailonshutdown.target
[Unit]
Description=Send mail before shutdown
Requires=network.target
After=network.target

## Test this if the above fails
#Requires=network.target sendmailtask.service
#After=network.target sendmailtask.service

[Install]
WantedBy=shutdown.target
  • Service unit sendmailtask.service

[Unit]
Description=Run SOAP request task on shutdown
PartOf=sendmailonshutdown.target

### Not sure about nodeps. Uncomment these if it fails, to test
#DefaultDependencies=no
#After=network.target

Requires=network.target
Before=sendmailonshutdown.target

Type=oneshot

ExecStart=/usr/local/bin/senddownsoap.sh
TimeoutSec=7

### You may need to use ExecStop, if this doesn't work
#ExecStart=/usr/bin/echo "SOAP is in place"
#ExecStop=/usr/local/bin/senddownsoap.sh
#TimeoutStopSec=7


[Install]
WantedBy=sendmailonshutdown.target
  • Enable sendmailonshutdown.target
systemctl enable sendmailonshutdown.target

Of course, you should change names to what you like. I used my preferred ones, so helping can be more funny. :stuck_out_tongue_winking_eye:

2 Likes

I put sendmailonshutdown.target and sendmailtask.service into /etc/systemd/system/ and enabled them both.

sudo systemd-analyze verify sendmailtask.service returned error: Service has no ExecStart=, ExecStop=, or SuccessAction= so I added [Service] header before Type=.

But now nothing is happening and also no errors are visible in the logs.


Atleast when I restart the service it successfully sends the SOAP request. But not on the system shutdown :/.

Not both. IIUC, only target needs to be enabled. The service will be static, but should run when the target pulls it, on shutdown.

:+1: My mistake :cry:

Test again after disabling the service, and after reboot, check journal of previous boot.
I hope it works :zipper_mouth_face:

1 Like


Still no progress :confused:
But when i restart sendmailonshutdown.target it sends the request and everything is executed just fine. Could it be not triggered with system shutdown? I am turning off my laptop normally by clicking on these buttons: Application launcher -> shut down -> shut down.

It means the target is pulling in the service, as intended, so it works, but nothing else.

Have you tried alternative configuration, as noted inside the units?

Comment the active and uncomment the disabled lines. (#)

2 Likes