-   Programming (
-   -   Kill script kills too many processes? (

MaxM 10-21-2010 05:30 AM

Kill script kills too many processes?
Hello all,

English isnt my native language, so bear with me, I am trying to make this clear and short.

User 1 and User 2 each start a mono process with sudo:

"sudo mono user1.exe" "sudo mono user2.exe"

Each user has a in their directory, which is being called by user1.exe/user2.exe to kill the process.

The script itself is

ps aux | grep 'mono user1.exe' | awk ‘{print $1}’ | xargs kill

which in theory should pull *only* the PID of "mono user1.exe" and kill only that.

The problem: It kills any and every single instance of mono that is running on my system, every userx.exe thats open.

I am confused, as a simple "ps aux | grep 'mono user1.exe'" does only return the mono user1.exe process and not the others. "ps aux | grep 'mono'" returns them all though.

Any help on how I can modify that script so that it only kills the specific process?

Would "pkill -9 -f 'mono MCuser1.exe'" work as well - or would it too kill every instance of mono? I cant do a lot more of trial and error, its not good I am killing those instances accidently...

Thank you so much for your help!

igadoter 10-21-2010 05:48 AM

What about with awk call?

MaxM 10-21-2010 05:49 AM


Originally Posted by igadoter (Post 4134592)
What about with awk call?

With me you need to make things ultra-clear, otherwise I fail badly.

How would I change the script?

Thank you for helping!

procfs 10-21-2010 05:51 AM

Hi, hope this will work

ps aux | grep 'mono' | grep 'user1.exe' | awk ‘{print $2}’ | xargs kill

GrapefruiTgirl 10-21-2010 05:54 AM

I don't see why the OP's first idea won't work, simply by changing the $1 to a $2 as shown by procfs above. The PID is the second field... Unless there's some different version of `ps` involved here.

MaxM 10-21-2010 05:58 AM

I will try as soon as I have access again, in a few hours. I didnt expect help that quickly! If it works you are my personal hero, it feels like I spent so many hours on this already..

GrapefruiTgirl 10-21-2010 06:03 AM

Another idea (for future reference), if this fails, is to give each individual user a symlink to the mono binary. That way, each user will show a unique process name for mono and you can kill based on that.

For example:

user #1 has a symlink like /usr/bin/mono1 ---> /usr/bin/mono
user #2 has a symlink like /usr/bin/mono2 ---> /usr/bin/mono
user #3 has a symlink like /usr/bin/mono3 ---> /usr/bin/mono

So it gives a unique executable name for each user.

Finally, if none of this works, I'd be questioning if mono spawns instances of itself, basically keeping one top parent process off of which the individual ones are created, and so killing the wrong single process will kill them all. If still stuck, please show us the output of your `ps aux` command while more than one mono process is running.

sag47 10-21-2010 06:36 AM

Why not kill with the following?

ps aux | sed 's/^ *//' | grep 'mono user1\.exe' | cut -d\  -f1 | xargs kill
By the way the . in grep needs to be canceled. Remember grep is using the . as a regex for all characters.

In fact your original command may work if you simply escape the period in your grep pattern.

igadoter 10-21-2010 06:54 AM

Post ps -aux output format, why don't try cut? Be aware of delimiters. What shell are you using? Be aware of delimiters. Eg. Bash always expands tabulators so cat foo | cut -f4 doesn't get expected results.

MaxM 10-21-2010 07:14 AM

Thank you all once again for the input. I cant work on this till I get home, but I can access a web console for small things.

ps aux gives the following (removed all clutter)

user1 18967 0.0 0.1 6064 1108 ? Ss 08:47 0:00 SCREEN
user1 18968 0.0 0.1 6552 1868 pts/1 Ss 08:47 0:00 /bin/sh
root 18969 0.0 0.1 5684 1128 pts/1 S+ 08:47 0:00 /bin/sh ./
root 18970 0.2 2.2 78928 23816 pts/1 Sl+ 08:47 0:52 mono user1.exe
user2 19043 0.0 0.1 6064 1104 ? Ss 08:50 0:00 SCREEN
user2 19044 0.0 0.1 6596 1980 pts/0 Ss 08:50 0:00 /bin/bash
root 19048 0.0 0.1 5688 1128 pts/0 S+ 08:50 0:00 /bin/sh ./
root 19049 0.6 3.2 122032 34568 pts/0 Sl+ 08:50 2:13 mono user2.exe

igadoter 10-21-2010 08:02 AM

For me it seems that each mono user$.exe is a child of ./ script. My suspicions is that in fact you are killing ./ script which causes all their childs being killed as well.

MaxM 10-21-2010 08:32 AM

Would a simple renaming of each of the users scripts do the trick? I didnt think that this could be the reason...

gnashley 10-21-2010 08:34 AM

I'd look at using ps -ux (the 'a' lists all processes -without it it should only list processes which belong to the user) or I'd use pgrep or even ps -U username ...

igadoter 10-21-2010 08:41 AM

I don't know how ./ looks like - I thought that if a signal cannot be catched by a child it may go up and be catched by a parent. This may explain why ./lifline can be killed. You can always verify if ./lifeline runs after your killing command
ps -A | grep lifeline

unSpawn 10-21-2010 11:55 AM


Originally Posted by MaxM (Post 4134573)
Any help on how I can modify that script so that it only kills the specific process? Would "pkill -9 -f 'mono MCuser1.exe'" work as well - or would it too kill every instance of mono?

If a user (LOGNAME env var) runs 'mono' as root UID then using 'pkill -9 MC${LOGNAME}.exe' should work. If there are no specific requirements for root privileges then users running 'mono' under their own UID would be preferable and way easier as pkill would only let them select and kill processes they own. In any case I can't see why any 'ps|grep|awk|sed|cut' kludges would be necessary when one has pgrep/pkill?..

All times are GMT -5. The time now is 10:48 AM.