Problem with self created service unit

Hello,
I have a question regarding a creation of systemd service unit.
I’m getting desperate with this project and hope that there is someone here who can help me a bit.
I actually want to run a script for an Xfce project when I click on the shutdown button before the system is no longer able to do so. This is a small 3 second sound file.
All solutions I found on the internet don’t work unfortunately.

Here is my unit and the commands I used to activate it.

[Unit]
Description=shutdown sound
Before=shutdown.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStop=/bin/bash -c "play -q /home/jag/.local/share/sounds/Smooth/stereo/desk>
TimeoutSek=5

[Install]
WantedBy=shutdown.target

Activated with:

sudo systemctl daemon-reload
sudo systemctl enable sound-shutdown.service --now
sudo systemctl status sound-shutdown.service
○ sound-shutdown.service - shutdown sound
     Loaded: loaded (/etc/systemd/system/sound-shutdown.service; enabled; vendo>
     Active: inactive (dead)

Also when I select a sound file from the root directory I have the same result. Nothing happens.
I have the suspicion that it is at …target.

What happens when you change the ExecStop line from /bin/bash -c "play... to full path of play command, for example ExecStop=/usr/bin/play -q ...

Hello, thank you for your participation.
I forgot that I have installed the SOX package which runs this play command. But I also tried canberra-gtk-play and even played with vlc. It all leads to the same result.

Another unit is the one that was marked as working in the XFCE forum but has not been allowed for a long time because the Boolean value “True” does not work anymore. Link:
https://forum.xfce.org/viewtopic.php?pid=64370#p64370

Another method to execute a script before shutdown is described here and I tried that too.
Script created and made executable with the content:

sh -c "play -q /home/jag/Downloads/Smooth/stereo/desktop-logout.oga"

I have no success with this either.

Now I have a suspicion where I hope that someone can confirm it; Can it be that Pipewire only works in Runlevel5 and no longer in Runlevel4?

Did you try ExecStop=/usr/bin/play -q /home/jag/Downloads/Smooth/stereo/desktop-logout.oga without sh -c or /bin/bash -c?

Does /usr/bin/play -q /home/jag/Downloads/Smooth/stereo/desktop-logout.oga work when you login to a tty without a graphical session?

/usr/bin/play -q /home/jag/Downloads/Smooth/stereo/desktop-logout.oga

works in TTY2 without problems.

I have reduced the unit again and tested it:

[Unit]
Description=shutdown sound
Before=shutdown.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStop=/usr/bin/play /home/jag/Downloads/Smooth/stereo/desktop-logout.oga

[Install]
WantedBy=shutdown.target

then:

sudo systemctl daemon-reload

and

jag@fedora:~$ sudo systemctl status sound-shutdown.service
○ sound-shutdown.service - shutdown sound
     Loaded: loaded (/etc/systemd/system/sound-shutdown.service; enabled; vendor preset: disabled)
     Active: inactive (dead)
jag@fedora:~$ 

Unfortunately no success. But it must be possible. I tinker there now already 2 days at it. It must be possible to execute a script (e.g. backup) or unit before the system shuts down.

Hi,

google search found this:

Which would suggest that it may be easier to have the service run on startup and remain on exit till shutdown. So the unit would look something like this:

[Unit]
Description=shutdown sound

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/usr/bin/play /home/jag/Downloads/Smooth/stereo/desktop-logout.oga

[Install]
WantedBy=multi-user.target

Also it may be better to put /usr/bin/play /home/jag/Downloads/Smooth/stereo/desktop-logout.oga in a bash script and call that instead with ExecStop

Thanks Tom.

1 Like

So far I was already true myself! :roll_eyes: Nevertheless, I have again worked through the instructions from your link.
Unfortunately, this does not work. But the service file seems to be correct.

[Unit]
Description=shutdown sound

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=sh -c '/etc/sound_shutdown.sh'

[Install]
WantedBy=multi-user.target

script in /etc/

#!/bin/bash 

/usr/bin/play -q /home/jag/Downloads/Smooth/stereo/desktop-logout.oga

status:

jag@fedora:~$ sudo systemctl status sound-shutdown.service
● sound-shutdown.service - shutdown sound
     Loaded: loaded (/etc/systemd/system/sound-shutdown.service; enabled; vendor pr>
     Active: active (exited) since Sun 2022-10-30 14:54:36 CET; 11min ago
    Process: 22993 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 22993 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Okt 30 14:54:36 fedora systemd[1]: Starting sound-shutdown.service - shutdown sound>
Okt 30 14:54:36 fedora systemd[1]: Finished sound-shutdown.service - shutdown sound.
lines 1-9/9 (END)
sudo service sound-shutdown.service start
sudo service sound-shutdown.service restart

The service file runs through without error but there is nothing to hear.

I put again the assumption in the room that Pipewire receives no stream.
How should I actually understand this explanation here?

Does it work if you convert the sound to a wav and use /usr/bin/aplay ?

Pipewire and wireplumber are both running as user services so ALSA maybe all that is available to you at that point.

It also looks like you can set that in for sox …
In the script you could add
export AUDIODRIVER=alsa

you may need to go through and specify the device if you have multiple sound cards.

1 Like

First of all I would like to thank you again for interest!

In /usr/schare/Sounds/… there are already files.
Basically the unit and the script works.
With both I do not get an error code.
Script with Geany tested and Unit with command(s).

jag@fedora:~$ /usr/bin/aplay /usr/share/sounds/purple/logout.wav
Wiedergabe: WAVE '/usr/share/sounds/purple/logout.wav' : Signed 16 bit Little Endian, Rate: 22050 Hz, stereo
jag@fedora:~$ 
jag@fedora:~$ sudo service sound-shutdown.service start
[sudo] Passwort für jag: 
Redirecting to /bin/systemctl start sound-shutdown.service
jag@fedora:~$

The problem must be with Pipewire because I also see with aplay in the task manager that Pipewire is started briefly. But there is still nothing to hear.
Apparently Pipewire hinders the process of the script.

The installation of moc as a further sound server has unfortunately also failed. I have no more nerves to deal with it!

I am running out of ideas.

How can I get back to ALSA? Is there an official manual? Maybe the crackling and popping and the interruptions when starting the browser will stop?

Hi,

I think you are hitting a race condition. Pulseaudio is being stopped before it can it can play the sound.

Would running on loggout suffice?

https://www.linuxcircle.com/2015/06/13/how-to-run-logout-script-or-sound-on-linux-xfce/

Regards Tom.

Yes, sure I can live with it but even if an idiot system like Windows can do that, then questions arise. :face_with_monocle:
I just wanted to activate the system sounds and came up with the idea of maybe creating a current article instead of the user having to search for everything from outdated and perhaps no longer applicable information.
Everything works except for the sound at shutdown.

I have a sound at logon and logoff, in the system and at reboot but not at shutdown and that annoys me. Despite intensive efforts I have not found a solution for Fedora and OpenSuse. So the error is not specific to Fedora! :wink:

Back to the topic.
What is the fault now SystemD or Pipewire?
How, where and with what info should we create a bug report?

Hi,

I’m convinced its fault with either.

Systemd is working as expected. Its shutting down its services in parallel rather than sequence, so its a timing issue rather than a bug.

I don’t think you can blame pipewire, as Its been shutdown when asked to play the file.

Regards Tom.

Pipewire is a user service in Fedora Linux right?

[/home/gregory]$ systemctl --user status pipewire.service
● pipewire.service - PipeWire Multimedia Service
     Loaded: loaded (/usr/lib/systemd/user/pipewire.service; disabled; vendor preset: disabled)
     Active: active (running) since Sun 2022-10-30 14:20:31 CDT; 7h ago
TriggeredBy: ● pipewire.socket
   Main PID: 1866 (pipewire)
      Tasks: 2 (limit: 18459)
     Memory: 7.8M
        CPU: 2min 19.705s
     CGroup: /user.slice/user-1000.slice/user@1000.service/session.slice/pipewire.service
             └─ 1866 /usr/bin/pipewire

Oct 30 14:20:31 hal9000 systemd[1624]: Started PipeWire Multimedia Service.

So if you need a sound to play before the Pipewire service stops, you probably need to

  1. Make your sound-shutdown.service a user service by placing it under ~/.config/systemd/user and running systemctl --user enable --now sound-shutdown.service.

  2. Place an ordering requirement of After=pipewire.service on the sound-shutdown.service so that it is started after Pipewire starts and stopped before Pipewire stops (yes, the ordering requirements have the reverse meaning on shutdown).

I don’t know for sure, but the above would be my guess. Good luck! :slightly_smiling_face:

P.S. It also looks like user services are normally “WantedBy” default.target.

[/home/gregory]$ grep default.target /usr/lib/systemd/user/*.service
/usr/lib/systemd/user/gamemoded.service:WantedBy=default.target
/usr/lib/systemd/user/gcr-ssh-agent.service:WantedBy=default.target
/usr/lib/systemd/user/p11-kit-client.service:WantedBy=default.target
/usr/lib/systemd/user/p11-kit-server.service:WantedBy=default.target
/usr/lib/systemd/user/pipewire-pulse.service:WantedBy=default.target
/usr/lib/systemd/user/pipewire.service:WantedBy=default.target
/usr/lib/systemd/user/podman-auto-update.service:WantedBy=default.target
/usr/lib/systemd/user/podman-restart.service:WantedBy=default.target
/usr/lib/systemd/user/podman.service:WantedBy=default.target
/usr/lib/systemd/user/rygel.service:WantedBy=default.target
/usr/lib/systemd/user/tracker-extract-3.service:WantedBy=default.target
2 Likes

Thanks for your tip! With this I have already come one step further.
When I shut down the system takes much longer than normal so probably a process is working until it is killed.

Things I have noticed:

  1. Hyphens in service names are not a good idea. Changed the name from sound-shutdown.service to soundshutdown.service. :upside_down_face:

  2. If I disable the service file it probably works obviously since the sound file is played. :joy:

At the moment the service looks like this:

Summary
[Unit]
Description=shutdown sound

[Service]
Type=oneshot
After=pipewire-pulse.service systemd-remount-fs.service
Before=dracut-shutdown.service
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=sh -c 'play -q /usr/share/sounds/freedesktop/stereo/service-logout.oga'

[Install]
WantedBy=default.target

But unfortunately I still have an error message that I don’t quite understand. It seems that another service is requested which is not active. Apparently the file system is already unmounted.

Summary
systemctl status /home/jag/.config/systemd/user/soundshutdown.service
Unit home-jag-.config-systemd-user-soundshutdown.service.mount could not be found.

I have then searched:

Summary
systemctl list-units --all | grep mount
● boot.automount                                                                              not-found inactive dead      boot.automount
  proc-sys-fs-binfmt_misc.automount                                                           loaded    active   waiting   Arbitrary Executable File Formats File System Automount Point
  -.mount                                                                                     loaded    active   mounted   Root Mount
  boot-efi.mount                                                                              loaded    active   mounted   /boot/efi
● boot.mount                                                                                  not-found inactive dead      boot.mount
  dev-hugepages.mount                                                                         loaded    active   mounted   Huge Pages File System
  dev-mqueue.mount                                                                            loaded    active   mounted   POSIX Message Queue File System
  home.mount                                                                                  loaded    active   mounted   /home
  proc-fs-nfsd.mount                                                                          loaded    inactive dead      NFSD configuration filesystem
  proc-sys-fs-binfmt_misc.mount                                                               loaded    inactive dead      Arbitrary Executable File Formats File System
  run-media-jag-550c6531\x2dbbe2\x2d4ca3\x2d8326\x2d1a16bc4a1049.mount                        loaded    active   mounted   /run/media/jag/550c6531-bbe2-4ca3-8326-1a16bc4a1049
  run-user-1000-doc.mount                                                                     loaded    active   mounted   /run/user/1000/doc
  run-user-1000.mount                                                                         loaded    active   mounted   /run/user/1000
  sys-fs-fuse-connections.mount                                                               loaded    active   mounted   FUSE Control File System
  sys-kernel-config.mount                                                                     loaded    active   mounted   Kernel Configuration File System
  sys-kernel-debug.mount                                                                      loaded    active   mounted   Kernel Debug File System
  sys-kernel-tracing.mount                                                                    loaded    active   mounted   Kernel Trace File System
● sysroot.mount                                                                               not-found inactive dead      sysroot.mount
  tmp.mount                                                                                   loaded    active   mounted   Temporary Directory /tmp
  var-lib-nfs-rpc_pipefs.mount                                                                loaded    active   mounted   RPC Pipe File System
● var.mount                                                                                   not-found inactive dead      var.mount
  dracut-mount.service                                                                        loaded    inactive dead      dracut mount hook
  dracut-pre-mount.service                                                                    loaded    inactive dead      dracut pre-mount hook
  nfs-mountd.service                                                                          loaded    inactive dead      NFS Mount Daemon
  ostree-remount.service                                                                      loaded    inactive dead      OSTree Remount OS/ Bind Mounts
  systemd-remount-fs.service                                                                  loaded    active   exited    Remount Root and Kernel File Systems
  umount.target

For another push in the right direction I would be grateful. :compass:

Hi,

Just found this with google:

15.10 - How do I get my script to run BEFORE any shutdown sequence like unmounting my external drive - Ask Ubuntu.

So if you have a separate home partition this may be worth ago:

[Unit]
Description=shutdown sound
RequiresMountsFor=/home

Or full a single partition

[Unit]
Description=shutdown sound
RequiresMountsFor=/

Also I think the After= and Before= should be in the unit section, not service.

May also require DefaultDependencies=no in the unit section aswell.

Thanks Tom.

Yeah, I forgot about that. Hyphens in the service name (-) can be translated into forward slashes (/). But I thought that was only for “mount” units. I’m quite certain that I’ve seen hyphens in “service” unit names before.

As for the Before=... and After=... lines, those need to be in the [Unit] section of the service file, not the [Service] section. BTW, there are man pages for these things (man systemd.unit and man systemd.service) but they are not light reading. :slightly_smiling_face:

As for something being unmounted, about the only thing I could think of there is that maybe the /home filesystem is being unmounted before your service runs. My suggestion would be to move your sound file to /opt which is normally part of the root partition. Also, if you want to make this sound play for all users instead of just your account, you might move it under /etc/systemd/user instead of ~/.config/systemd/user.

That’s about all I can come up with off-hand. But let us know if these changes don’t fix the problem. :slightly_smiling_face:

I don’t think that would be needed in this case. Services normally only need that if they are trying to run very early in the startup process or very late in the shutdown process. The user wants this service to start after pipewire starts and stop before pipewire stops, so that would not be a low-level service that would want/need DefaultDependencies=no.

1 Like

No Still not working. But I came on another track while browsing the Internet. Namely the service
„systemd-inhibit"
There I have read in briefly times. See here:
https://www.freedesktop.org/software/systemd/man/systemd-inhibit.html

If I now run:

systemd-inhibit --list > inhibit.txt
WHO                 UID  USER PID  COMM            WHAT                                                      WHY                                       MODE
ModemManager        0    root 864  ModemManager    sleep                                                     ModemManager needs to reset devices       delay
NetworkManager      0    root 1052 NetworkManager  sleep                                                     NetworkManager needs to turn off networks delay
UPower              0    root 1769 upowerd         sleep                                                     Pause device polling                      delay
xfce4-power-manager 1000 jag  1766 xfce4-power-man handle-suspend-key:handle-hibernate-key:handle-lid-switch xfce4-power-manager handles these events  block

4 inhibitors listed.

I see that the corresponding service is blocked.

This confirms that I should actually manage this graphically with an autostart entry (see input post) if this service would not be blocked!

Indirectly, this is for me a confirmation that this is not to be done even with a service unit.

So the guys from XFCE have a bug or whatever in the system. I think I’ll step on their feet so that we do not continue to turn in circles and waste time.
Or what do you think?

I don’t see sound-shutdown.service in the output in your previous post. Are you sure that it is being inhibited?

I wouldn’t until you are sure of what the problem is. I’m not convinced that it is because of systemd-inhibit.

Yes, according to this website logind is replaced by the xfce-power-manager.
https://docs.xfce.org/xfce/xfce4-power-manager/4.16/faq
But I can’t find the right file to make a change.
This will block systemd. It seems to me like a stopgap solution from XFCE to avoid a bigger problem.