2

I‘m running a host with x86-64 Arch Linux with Plasma with latest updates. Here I contact a NAS with wakeonlan, which works fine, so that I can mount an nfs and a Samba share after successful pinging the NAS.

This is done with a systemd service, started (enabled) when booting my host (tuxarch). I have installed an Arch OS for arm processors on my NAS. With the correct permissions on my remote NAS, I can shutdown (systemctl poweroff) this NAS via ssh from host tuxarch. I have a small batch file in which I unmount the nfs and the cifs mounted shares and after unmounting I send the "systemctl poweroff" command via ssh. This batch file does, what I expect and finally the NAS is shut down, powered off.

Now I want to execute this batch file just before shutting down my host via systemd service with still mounted shares (to unmount them safely) and still established network connection to the remote NAS to finally send the command „sudo -u tuxuser ssh -t [email protected] 'sudo systemctl poweroff'“ to shutdown (Poweroff) my NAS.

For this I created several different shutdown.Zyxel.NAS.service (s). One at one time, next with adoptions. To test and activate the actual service I do not enable this service, I just START the service with: systemctl start shutdown.Zyxel.NAS.service. Now, my problem is, that the batch file is executed immidiately when the service is started instead of wait until shutdown is triggered. I have tried several configurations of such services, here is my last one:

systemctl cat shutdown.Zyxel.NAS

/etc/systemd/system/shutdown.Zyxel.NAS.service
[Unit]
Description=Shutdown Remote Zyxel NAS
DefaultDependencies=no
Requires=network-online.target
RequiresMountsFor=/mnt/Zyxel-Nas
After=final.target

[Service]
User=tuxuser
Group=tuxuser
ExecStart=/home/tuxuser/ssh.Zyxel.NAS.poweroff.sh
Type=oneshot

[Install]
WantedBy=final.target[/code]

So, as I said, the service starts the batch file in /home/tuxuser immediately. What is wrong in my shutdown.Zyxel.Nas.service? I wonder, why this service does not hesitate until the signal to begin shutdown process is send. Any help, any good suggestion is appreciated.

Thank you in advance!

-Linuxwhisperer

1 Answer 1

2

I wonder, why this service does not hesitate until the signal to begin shutdown process is send.

There's nothing in the service which says that it should wait.

After= does not do what you think. It is not a trigger and not a timer. It only tells systemd how to sort jobs in the same task (transaction) – for example, during system boot, you have 20–30 services starting simultaneously, so Before= and After= are used to sort them between each other.

But if you manually start shutdown-NAS.service but final.target isn't in the process of starting right now, then After=final.target does nothing.

Suggestions:

  • Option 1: Just don't start the service manually at all, ever. Activate it using systemctl enable (so that it's invoked by final.target) and deactivate with systemctl disable.

  • Option 2: First change your unit to start on boot (multi-user.target like any other service) and have ExecStart=/bin/true, i.e. do nothing on startup. Then move your script to ExecStop=, like this:

    [Service]
    ExecStart=/bin/true
    ExecStop=/home/tuxuser/ssh.Zyxel.NAS.poweroff.sh
    Type=oneshot
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
    
2
  • I used your script above (unchanged) and enabled it, I rebooted. Displaying the status for this service after rebooting shows, the service is loaded and active. But when I then shutdown via Plasma Konsole command: qdbus org.kde.ksmserver /KSMServer logout 0 2 0 what I always do to have a save logout from Plasma desktop, then nothing happens with the NAS. It is still not shut down. Commented Apr 6, 2019 at 15:26
  • Finally I got the solution. Your service script extended by Requires=network,target, DefaultDependencies=no and Before=shutdown.target reboot.target. I deleted the 'user' and the 'group' lines. And the ExecStop command is located at /usr/bin/ssh.Zyxel.NAS.poweroff.sh and is executed as root: umount /mnt/Samba/public umount /mnt/Zyxel-nfs sudo -u tuxuser ssh -t [email protected] 'sudo systemctl poweroff' Now, everything is running fine. NAS will shutdown when host tuxarch is told to shutdown. Thank you for your advice. Commented Apr 9, 2019 at 21:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .