- Have Cygwin Installed on your Windows System to run any of your Unix commands. Or you can directly run these commands from any of the Unix based systems
- Launch Cygwin shell command prompt. On Unix based system launch the terminal that runs the Bash Shell
- We use SSH to remotely execute the commands on Unix systems. Whenever you run any SSH command, it’s prompt you to provide your password to remotely connect to the system. To avoid prompt/typing password have the SSH key infrastructure setup as detailed in Automate command execution on MAC OS X/Unix Systems from Windows Systems remotely via SSH without prompting for Password everytime
- 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 loginwindowGovardhan@Gunnala-laptop ~
$ ssh [email protected] ps -Ajc | grep loginwindow | grep -i standuser00* | awk ‘{ print $2 }’
53045
53126Govardhan@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 loginwindowGovardhan@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 "
53045Govardhan@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] "sudo /bin/kill -9 $(ps -Ajc | grep loginwindow | grep standuser002 | cut -f4 -d' ');" <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   56810 36920 56810      0    0 Ss     ??    0:00.49 loginwindow </p> <p>Govardhan@Gunnala-laptop ~ <br />$</p> <p> </p> <p> </p> <p>To avoid even typing in password for sudo</p> <p>Govardhan@Gunnala-laptop ~ <br />$ ssh -t [email protected] "echo testpwd123 | sudo -S /bin/kill -9 $(ps -Ajc | grep loginwindow | grep standuser003 | cut -f4 -d' ');" <br />Connection to 192.22.4.108 closed. </p> <p>Govardhan@Gunnala-laptop ~</p>
<
p>