💾 Archived View for pwshnotes.flounder.online › gemlog › 2023-03-05-terminate-on-logout.gmi captured on 2024-02-05 at 09:29:09. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-03-20)

-=-=-=-=-=-=-

Killing All Processes When Logging Out

I happened to run the Xfce Task Manager to troubleshoot an unrelated issue. And I noticed that the previous user of my computer had processes running in the background. I thought this was strange because my assumption is that all processes are terminated when a user logs off. In this case, I'm sure the user logged out instead of switching users.

It turns out that Arch Linux and Manjaro have a different default configuration. By default, Arch and Manjaro allow their service manager, systemd, to run in the background after logging off. The reason given is this allows terminal multiplexers--like tmux and screen--to work. So, in my case, it was systemd and a group of child processes that I saw running in the background.

Below, I demonstrate two ways to reconfigure your system so that all processes are terminated when a user logs off. Both are based on the logind.conf file.

If you have a problem with these steps then check out the links in the References section. Those web pages contain useful information about other options and troubleshooting steps. For example, you can change the default and then create a list of users who are excluded from that setting.

Also note that I used the Xfce application "Task Manager" to troubleshoot this issue. If you're not using the Xfce desktop environment then you can use "ps aux" as root in a terminal to list all processes. This should work for everyone.

sudo ps aux

[sudo] password for michael: 

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 169404 13784 ?        Ss   Feb25   0:02 /sbin/init
root           2  0.0  0.0      0     0 ?        S    Feb25   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   Feb25   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   Feb25   0:00 [rcu_par_gp]
...

Terminate Processes for All Users

These instructions tell you how to change the default setting. By changing this setting, all processes will terminate when a user logs off.

/etc/systemd/logind.conf

For example, I can use the "sudo" command-line utility to invoke the advanced text editor Vim. And, I can include the file path in the same command. Below, I'm prompted by sudo for my regular user password. After entering my password, Vim opens logind.conf with the correct permissions.

sudo vim /etc/systemd/logind.conf

[sudo] password for michael: 

# Vim opens the logind.conf file. 
# ... 

The logind.conf file requires some explanation. First, the comment symbol for this type of file is the number sign or hash symbol #. And you will notice that logind.conf lists all the default settings. But, each setting is disabled with a leading #. These disabled defaults provide a convenient way of viewing the settings and making changes.

Remember the line will begin with a # and it will have its default setting.

It should look like this:

#KillUserProcesses=no

After making the changes, the line should look like this:

KillUserProcesses=yes

By making this change, all processes will terminate when a user logs off.

You can confirm this by checking your task manager. For example, I use Xfce on Manjaro. So I can log on as two different users. Then I can log off one of them. Next, I can run the "Task Manager" app with its "Show all processes" option set. I will see processes running under accounts that I don't recognize. This is normal. But, importantly, I won't see any processes running as the user that I logged off. That's what I expect to see now.

Terminate Processes for Select Users

These instructions tell you how to change the logoff behavior for specific users. This was a better match for my situation where one user rarely logs on and it makes sense for their processes to terminate when they log off.

It should look like this.

#KillOnlyUsers=

Keep in mind these users will have all their processes terminate when they log off. And other users will not be affected.

For example, if I want to change the behavior when Bob logs off then I add the 'bob' user to the setting.

KillOnlyUsers=bob

Additional users can be added. Separate each user with a space.

I am sure there is some official way to list the user names present on a system. But, my system is relatively simple. So, I just navigated to the /home/ directory and copied the user name from there. The name of each home directory is based on the user name. So this provides a convenient list of user names with the correct spelling and capitalization. Linux is case-sensitive.

Now, after Bob logs off, all his user processes are closed. And I can confirm this by using the Xfce Task Manager while logged on as myself.

References

'Kill user processes on logout' | systemd | ArchWiki

Manual Page for logind.conf

Help with the 'ps' command | Hostinger

Created: Sunday, March 5, 2023

Updated: Sunday, March 5, 2023