自動起動された.desktopアプリケーションがログアウト時に終了しない

Jun 17 2020

起動時に.desktopアプリケーションを自動起動しても機能しないという回答に従っていますが、正常に機能しています。Pythonスクリプトを起動しています。.desktopアプリケーションは、次のようになります。

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

これはうまく機能していますが、ログアウトしてログインすると、スクリプトの最初のインスタンスがまだ実行されていることがわかります。

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

ログアウト時にスクリプトが確実に終了するようにする方法はありますか?ログアウト時に終了するロジックを追加する必要がありますか?


logind.conf@binarystaの要求に応じて情報を更新する

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

更新します。2つの自動起動ファイルがあります。1つは上記のPythonスクリプトを起動し、もう1つはを起動しますgnome-terminal。再起動後、次のように表示されます。

$ 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

ログアウトしてログインすると、次のように表示されます。

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

回答

1 binarysta Jun 18 2020 at 12:56

これは、UbuntuではデフォルトであるためKillUserProcessesです。この設定により、ユーザーが完全にログアウトしたときにユーザープロセスが強制終了されなくなります。この動作を変更して、ユーザーのログアウト時にすべてのユーザープロセスを強制終了するには、設定して再ログインします。noKillUserProcesses=yes/etc/systemd/logind.conf

現在の値は次の方法で確認できます(true変更後の値である必要があります)

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

更新

ここでのgnome-terminal(GUIアプリケーション)とshutdown_notify.pyプロセスの違いは、gnome-terminal-server実行中の他のすべてのX11プロセスと同じTTYにプロセスがバインドされていることです。ログアウトすると、デスクトップ環境とウィンドウシステム(x11)が終了するため、gnome-terminal終了します。