From Windows, how to remotely force logoff users on MAC OS X Systems (and/or) how to remotely find a process running as a given user and kill it on Unix systems

    Frame the command line syntax that filters the required processes.
    Ps –Ajc: Lists all the running processing grep: to filter the specific processes and of specific user
    awk:  to filter the results and fetch only the PID of the matching processes OR
    cut: to filter while passing command to the remote system within double quotes, as the special characters in awk syntax will cause issues there.
    Example:

Govardhan@Gunnala-laptop ~
$ ssh [email protected] ps -Ajc | grep loginwindow | grep -i standuser00*
standuser001   53045 36920 53045      0    0 Ss     ??    0:00.60 loginwindow
standuser002   53126 36920 53126      0    0 Ss     ??    0:00.61 loginwindow

Govardhan@Gunnala-laptop ~
$ ssh [email protected] ps -Ajc | grep loginwindow | grep -i standuser00* | awk ‘{ print $2 }’
53045
53126

Govardhan@Gunnala-laptop ~ (Instead of getting just the PID we get all the results as awk filtering fails)
$ ssh [email protected] "pid=$(ps -Ajc | grep loginwindow | grep standuser001 | awk ‘{ print $2 }’); echo $pid"
standuser001 53045 36920 53045 0 0 Ss ?? 0:00.68 loginwindow

Govardhan@Gunnala-laptop ~ (Alternatively we can use cut as shown below)
$ ssh [email protected] "pid=$(ps -Ajc | grep loginwindow | grep standuser001 | cut -f4 -d’ ‘); echo $pid "
53045

Govardhan@Gunnala-laptop ~

 

 

 

 

    Kill –9: to close the chosen process
    Sudo –S: If you are attempting to close/terminate any system processes or other user processes, you need to provide an administrator level elevated credentails by using Sudo
    echo <sudo passwd>: if you want to use this command via automation where you want password to be automatically typed for Sudo elevation

    Govardhan@Gunnala-laptop ~ (Process to be killed is running under two users)
    $ ssh [email protected] ps -Ajc | grep loginwindow | grep -i standuser00*
    standuser002   56686 36920 56686      0    0 Ss     ??    0:00.54 loginwindow
    standuser001   56810 36920 56810      0    0 Us     ??    0:00.49 loginwindow

    <p>Govardhan@Gunnala-laptop ~ (<strong>Process killed remotely for standuser002</strong> )      <br />$ ssh -t [email protected] &quot;sudo /bin/kill -9 $(ps -Ajc | grep loginwindow | grep standuser002 | cut -f4 -d' ');&quot;      <br />Connection to 192.22.4.108 closed. </p>    <p>Govardhan@Gunnala-laptop ~ (<strong>User standuser002 process is NO longer running</strong>)      <br />$ ssh [email protected] ps -Ajc | grep loginwindow | grep -i standuser00*      <br />standuser001&#160;&#160; 56810 36920 56810&#160;&#160;&#160;&#160;&#160; 0&#160;&#160;&#160; 0 Ss&#160;&#160;&#160;&#160; ??&#160;&#160;&#160; 0:00.49 loginwindow </p>    <p>Govardhan@Gunnala-laptop ~     <br />$</p>    <p>&#160;</p>    <p>&#160;</p>    <p>To avoid even typing in password for sudo</p>    <p>Govardhan@Gunnala-laptop ~      <br />$ ssh -t [email protected] &quot;echo testpwd123 | sudo -S /bin/kill -9 $(ps -Ajc | grep loginwindow | grep standuser003 | cut -f4 -d' ');&quot;      <br />Connection to 192.22.4.108 closed. </p>    <p>Govardhan@Gunnala-laptop ~</p> 
    

    <

    p> 

     

                      Leave a Reply

                      Your email address will not be published. Required fields are marked *