1

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)
  • 1
    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) – guiverc Feb 07 '22 at 04:09

1 Answers1

0

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
  • 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? – Igor Shumakov Feb 08 '22 at 02:56
  • @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 – WinEunuuchs2Unix Feb 09 '22 at 00:32
  • Thanks. The service is executing on shutdown but problem is wmctl -l is still empty – Igor Shumakov Feb 10 '22 at 01:55