Linux – How to execute script just before x server stop or gnome logout

chromefedoragnomelinuxx-server

Chrome shows "didn't close correctly" message every time on my Fedora 28 system. I turn off the "Continue running background apps when Google Chrome is closed" feature.

I think the problem is my script executed after the x server stop or gnome logout. That's why chrome and other app close incorrectly still.

The script works fine manually, however, I want to do it automatically. I tried three ways to achieve this.

Close-all-windows Script:

#!/bin/sh
#wmctrl -l | while read -r line
#do
#    wmctrl -c `echo "$line" | sed 's/.*  [0-9]* [fink168] //'`
#done
WIN_IDs=$(wmctrl -l | awk '$3 != "N/A" {print $1}')
for i in $WIN_IDs; do wmctrl -ic "$i"; done

/etc/systemd/system/closeWindow.service:

[Unit]
Description=CloseWindows
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target kexec.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/fink168/bin/close-all-windows

[Install]
WantedBy=multi-user.target


systemctl status closeWindow.service
● closeWindow.service - CloseWindows
   Loaded: loaded (/etc/systemd/system/closeWindow.service; enabled; vendor preset: dis>
   Active: active (exited) since Sun 2018-07-29 12:17:58 CST; 1h 4min ago
  Process: 619 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 619 (code=exited, status=0/SUCCESS)

Jul 29 12:17:58 fink168 systemd[1]: Started CloseWindows.

/etc/gdm/PostSession/Default:

#!/bin/sh
echo " Closing selected windows programs gracefully"
#export DISPLAY=$DISPLAY
su - fink168 -c /home/fink168/bin/close-all-windows
#su fink168 -c pkill -o chrome

I also made soft links to rc6.d,rc0.d and rc5.d.

$ ls -alrt /etc/rc6.d/
total 8
    lrwxrwxrwx.  1 root root   17 Apr 25 14:34 K90network -> ../init.d/network
    lrwxrwxrwx.  1 root root   20 Apr 25 14:34 K50netconsole -> ../init.d/netconsole
    lrwxrwxrwx.  1 root root   17 Apr 25 14:39 K99livesys -> ../init.d/livesys
    lrwxrwxrwx.  1 root root   22 Apr 25 14:39 K01livesys-late -> ../init.d/livesys-late
    drwxr-xr-x. 10 root root 4096 Jul 18 19:39 ..
    lrwxrwxrwx.  1 root root   23 Jul 28 22:56 K99pkillChrome -> /etc/init.d/pkillChrome
    drwxr-xr-x.  2 root root 4096 Jul 28 22:56 .

pkillChrome init script:

$ cat /etc/init.d/pkillChrome
#!/bin/bash
#pkill -o chrome
#pkill -f chrome
su - fink168 -c /home/fink168/bin/close-all-windows

None of these approaches works on my Fedora 28. What's wrong?

Best Answer

try to use the graphical target -- that might fix this.

Related Question