In my project i need to write a function that will take the name of a command in parameter, and then will kill the process that is executing this command. I don't know how exactly to go about this problem. Can anyone help me please?
- Shell means in linux or windows?Rahul– Rahul2014-04-05 17:35:03 +00:00Commented Apr 5, 2014 at 17:35
- 1NO, that's wrong understanding. Shell, doesn't necessarily mean linux. Every OS has a command environment to run command which we call Shell and so every OS has it's own Shell. So, you should be specific about the OS you are using based on which answer depends.Rahul– Rahul2014-04-05 18:20:02 +00:00Commented Apr 5, 2014 at 18:20
- noted! thank you. Anyways do you have a suggestion to my problem?Codious-JR– Codious-JR2014-04-05 20:15:46 +00:00Commented Apr 5, 2014 at 20:15
Add a comment |
1 Answer
Assuming you mean linux/unix: to capture the process ID of a command, you can use pgrep:
pgrep COMMAND This returns the process ID as an integer, which you can then pass to kill.
kill -9 $(pgrep COMMAND) COMMAND is an extended regex pattern - so pgrep test would match commands called test, pgrep *test* would match test, tester, bashtest etc. You may also have access to pkill, which is similar but skips a step by directly killing the process matched.