Hello,
I am sorry to bother everyone on this matter, but I have been working on an issue for a week and I haven't been able to find a solution to my problem.
I can feel I am not far, but there is one last thing in my way which prevent me from moving forward.
To make it short, we are about to push Sierra upgrade on all Mac using LANDesk (Ivanti). So far everything works well.
I was able to write a script to bypass FV2, I also wrote a script which force quit all the Application in order to prevent a restart. But the last scenario that I am working on and which I am struggling is to Force log out any active user in a Mac except the current user.
Using the command line
sudo kill <pid>
With this command, I was able to force logout an active user of a test Mac, but to do so, I had to find the PID.
Now the difficulty is that in my company, we have many users, and I can't find the PID for each user on each computer manually.
So I need to write a script which will find the PID of all active user on a machine and force logout the active user except the current user.
So far, I have written this script: (This helps me to detect the other active users)
#!/bin/sh
CurrentUser=$(stat -f%Su /dev/console)
OtherActiveUsers=$( who | grep -v _mbsetupuser | grep -v ttys000 | grep -v "$CUser" | sed 's/console.*//' )
# If I run this command on a test machine where we have some test users:
infoPID=$( ps -Ajc | grep loginwindow | grep "$OUsers" )
echo "$infoPID"
# I get this result:
ld-imac-lab
fp.test1 1047 167 1047 0 0 Ss ?? 0:00.44 loginwindow
fp test4 1628 167 1628 0 0 Ss ?? 0:00.57 loginwindow
fp.test2 2094 167 2094 0 0 Ss ?? 0:00.38 loginwindow
This is where I am struggling. I need to be able to get only the PID:
1047
1628
2094
But because some users have a space in there logging name, I can't use "awk":
infoPID=$( ps -Ajc | grep loginwindow | grep "$OUsers" | awk '{print $2}' )
I can't use "cut" either:
infoPID=$( ps -Ajc | grep loginwindow | grep "$OUsers" | tr -s ' ' | cut -d' ' -f2 )
So if someone could help me get this PID by adding a command line it would be very much appreciate.
Right now, it works perfectly on the test computer when I don't have the account which has . space in the login name:
CurrentUser=$(stat -f%Su /dev/console)
OtherActiveUsers=$( who | grep -v _mbsetupuser | grep -v ttys000 | grep -v "$CUser" | sed 's/console.*//' )
ps -Ajc | grep loginwindow | grep "$OUsers" | awk '{print $2}' | sudo xargs kill -9