1

I'm following the answer of Autostarting .desktop application at startup not working, and it is working well. I'm launching a python script. The .desktop application looks like this:

[Desktop Entry]
Type=Application
Name=Autostart Script
Exec=shutdown_notify.py
Icon=system-run

This is working well - but if I log out and log in I see the first instance of my script is still running.

$ ps -C shutdown_notify.py
  PID TTY          TIME CMD
 4026 ?        00:00:01 shutdown_notify
25421 ?        00:00:00 shutdown_notify

Is there a way to make sure the script exits on log out? Do I need to add logic to quit on log out?


Update with logind.conf info as requested by @binarysta

$ grep -E 'KillUserProcesses|KillOnlyUsers|KillExcludeUsers' /etc/systemd/logind.conf
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root

Update, I have two autostart files: one launches the python script above, the other launches gnome-terminal. After a reboot I see:

$ ps aux | grep -E "gnome-terminal|shutdown_notify"
training  6495  0.9  0.1  84936 29360 ?        S    07:13   0:00 /usr/bin/python3 /usr/local/bin/shutdown_notify.py
training  6565  0.7  0.2 622880 34448 ?        Sl   07:13   0:00 /usr/lib/gnome-terminal/gnome-terminal-server
training  9647  0.0  0.0  13264  2564 pts/0    S+   07:13   0:00 grep --color=auto -E gnome-terminal|shutdown_notify

Log out, log in, and I see:

training  6495  0.1  0.1  85076 29360 ?        S    07:13   0:00 /usr/bin/python3 /usr/local/bin/shutdown_notify.py
training 19110  3.1  0.1  84936 29636 ?        S    07:15   0:00 /usr/bin/python3 /usr/local/bin/shutdown_notify.py
training 19141  2.3  0.2 696496 34584 ?        Sl   07:15   0:00 /usr/lib/gnome-terminal/gnome-terminal-server
training 19421  0.0  0.0  13264  2696 pts/0    S+   07:15   0:00 grep --color=auto -E gnome-terminal|shutdown_notify
russau
  • 155
  • 1
  • 6
  • please provide `grep -E 'KillUserProcesses|KillOnlyUsers|KillExcludeUsers' /etc/systemd/logind.conf` – binarysta Jun 17 '20 at 10:56
  • @binarysta I've updated the question with the results of the grep – russau Jun 17 '20 at 20:58
  • While it'd be interesting to know the rationale behind the design of a system that autostarts user programs at login without (by default) shutting them down at logout, using systemd `--user` units instead of `~/.config/autostart/` desktop entries is likely to give you grater control and flexibility. – fra-san Jun 18 '20 at 00:09

1 Answers1

2

This is because KillUserProcesses is no by default in Ubuntu. This setting causes user processes not to be killed when the user completely logs out. To change this behavior in order to have all user processes killed on the user's logout, set KillUserProcesses=yes in /etc/systemd/logind.conf and re-login.

current value can be checked by (it should be true after change)

busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses
b true

UPDATE

The difference here between gnome-terminal (GUI Application) and shutdown_notify.py processes is, gnome-terminal-server process bound to the same TTY as all other X11 processes running. By logging out, the desktop environment and windowing system (x11) will be terminated so thats why gnome-terminal will exit.

binarysta
  • 2,912
  • 10
  • 14