Score:1

how to execute script on shutdown for wmctrl -l?

cn flag

system: ubuntu 20

[Unit]
Description=Run Scripts at Start and Stop

[Service]
Type=oneshot
RemainAfterExit=true  
ExecStop=/home/user/same_test

[Install]
WantedBy=multi-user.target

same_test:

#!/bin/sh
echo test >> home/user/same_test_echo.txt (works)
wmctrl -l >> /home/user/same_test.txt (don't work)

Result is:

  • same_test.txt is empty (don't work)
  • same_test_echo.txt contain test (works)
guiverc avatar
cn flag
Ubuntu 20? So this is a Ubuntu Core 20 system? as they are usually headless. (Ubuntu releases using the *year* format are different products to the far more common *year.month* systems, ie. 20 != 20.04)
Score:0
in flag

Option 1 - Check the PATH

The first step is to see if the problem lies in where the commands are located:

$ type -a echo

echo is a shell builtin
echo is /bin/echo

$ type -a wmctrl

wmctrl is /usr/bin/wmctrl

The echo command is built into the shell and also located in the /bin directory, which is the most popular directory for programs.

The wmctrl command is located in the /usr/bin directory which may not be part of your systemd PATH.

So similar to this question:

The solution there was to add a line to systemd service:

[Service]
Environment=PATH=/home/someUser/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Another option is to hard-code the directory for the command when you call it: /usr/bin/wmctrl.


Option 2 - WantedBy=multi-user.target

NOTE: This is generic option for those Start Up and doesn't apply to Shut down like the OP is doing.

If the path wasn't the problem, the next step is to check the WantedBy option:

The answer there explains:

Note that a Wants or WantedBy only says that the system should startup one service whenever another service or target is also started, but it specifies nothing at all about the startup/shutdown order. If you need service B to be already running when service A starts up, you'd need to add Before=A.service in the B.service file to explicitly specify the start-up order dependency.

Consider the fact the GUI may not even be running when your script is run. So there are no windows for wmctrl -l to report.

If this is the problem After (during startup) rather than WantedBy is a likely solution.


Option 3 - Use /bin/true

From comments in this Q&A:

Replace:

RemainAfterExit=true

With:

RemainAfterExit=/bin/true

Note: true is a shell builtin and a command:

$ type -a true
true is a shell builtin
true is /bin/true
Igor Shumakov avatar
cn flag
I think the problem is service running on shutdown after all apps and maybe gui closed. I tried: Before=graphical.target but don't work How to run service before all apps and gui closed when shutdown/reboot?
WinEunuuchs2Unix avatar
in flag
@IgorShumakov Here is some additional reading while I dig a little deeper for a perfect fit: https://unix.stackexchange.com/questions/284598/systemd-how-to-execute-script-at-shutdown-only-not-at-reboot
Igor Shumakov avatar
cn flag
Thanks. The service is executing on shutdown but problem is wmctl -l is still empty
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.