How to log out *background* users from command line

Joined
Aug 10, 2010
Messages
13
Reaction score
0
Points
1
I'm looking for a way to log out a background user who forgot to log out - from the terminal.

I know this is trivial in the *nix world, just killing a process probably; I just can't find the command to do it in Mac's flavor (I'm a casual *nix shell user)

BTW, I don't want the Prefs->Security->General "logout after inactivity" setting.... that logs out everybody (including me) and I can't have my account logged out (due to running processes).

this is the closest I have gotten so far: but this will obviously log me out.
osascript -e 'tell application "System Events" to log out'

Thank you,
Jason
 
Joined
Sep 30, 2007
Messages
9,962
Reaction score
1,235
Points
113
Location
The Republic of Neptune
Your Mac's Specs
2019 iMac 27"; 2020 M1 MacBook Air; macOS up-to-date... always.
I'm having a tough time finding a way to do this with Terminal. A couple tricks I read of and tried that involved killing the "loginwindow" process for another user didn't work as expected. At the moment, here's the best solution I can find but involves a 3rd party utility:
How to log out a fast switched user
 
OP
A
Joined
Aug 10, 2010
Messages
13
Reaction score
0
Points
1
Excellent tip. This looks like it's going to work fine. I'm testing right now....


Thanks!
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Remember, OS X is Unix so if there's some technique that you know of in Unix, it may work here. Now, the variants of Unix are all quite different so I can't guarantee this is the case here. What do you do on a *nix box to accomplish this?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Remember, OS X is Unix so if there's some technique that you know of in Unix, it may work here. Now, the variants of Unix are all quite different so I can't guarantee this is the case here. What do you do on a *nix box to accomplish this?

well, something like...


kill -9 `ps -u <username> | grep -v PID | awk '{ printf ("%s ", $1); }'`


would accomplish this ;) it's ummm well, kinda forceful though
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Code:
echo "ARE YOU SURE!?!?!? PRESS ENTER IF YOU'RE SURE!" ; read
kill -9 `ps -u <username> | grep -v PID | awk '{ printf ("%s ", $1); }'`
Does that help to soften it up? ;)
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
well, getting rid of the -9 might be friendlier ;)
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
You mean the yelling to ensure the confidence of the user wasn't enough?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Oh I'm ALWAYS sure of my commands when I issue them ;)
 
OP
A
Joined
Aug 10, 2010
Messages
13
Reaction score
0
Points
1
well, something like...


kill -9 `ps -u <username> | grep -v PID | awk '{ printf ("%s ", $1); }'`


would accomplish this ;) it's ummm well, kinda forceful though

This is what I'm looking for - more-so than the 3rd party program.

However, when I try this, the <username> is an invalid argument of "ps". Should this be user ID?

When I do $ps -u, I only see my username.
$ps -A lists a lot of processes, but I don't see any from the other users in the background....

Can you help correct this?
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Umm yea, it should be a valid username, without the <>'s

Oh and you'd need to issue that with either root or superuser authority.
 
OP
A
Joined
Aug 10, 2010
Messages
13
Reaction score
0
Points
1
Umm yea, it should be a valid username, without the <>'s

Oh and you'd need to issue that with either root or superuser authority.

oops, I see how my comment would be misinterpreted... I meant that when I use a valid username - it gives errors. I changed lower case 'u' (userID) to upper case 'U' (username) and that seems to kill everything for that user.

The user still shows up in the FSU list however, and switching to that user hangs the GUI/console - so it's definitely not clean! But it is an option, and I thank you!

Thanks greatly!
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Ohhh ok, gotcha. But I think it's the opposite, that username is lowercase and UID is upper.

Yea, it's not the cleanest method in the world. It's a bit like hitting a nail with a sledgehammer :)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top