LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-21-2010, 05:30 AM   #1
MaxM
LQ Newbie
 
Registered: Oct 2010
Posts: 5

Rep: Reputation: 0
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 kill.sh 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!
 
Old 10-21-2010, 05:48 AM   #2
igadoter
Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: slack 12.2, debian-Trinity , openbsd
Posts: 723

Rep: Reputation: 56
What about with awk call?
 
Old 10-21-2010, 05:49 AM   #3
MaxM
LQ Newbie
 
Registered: Oct 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by igadoter View Post
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!
 
Old 10-21-2010, 05:51 AM   #4
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 475

Rep: Reputation: 33
Hi, hope this will work

ps aux | grep 'mono' | grep 'user1.exe' | awk ‘{print $2}’ | xargs kill
 
Old 10-21-2010, 05:54 AM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
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.
 
Old 10-21-2010, 05:58 AM   #6
MaxM
LQ Newbie
 
Registered: Oct 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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..
 
Old 10-21-2010, 06:03 AM   #7
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
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.
 
Old 10-21-2010, 06:36 AM   #8
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,336
Blog Entries: 33

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Why not kill with the following?
Code:
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.
 
Old 10-21-2010, 06:54 AM   #9
igadoter
Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: slack 12.2, debian-Trinity , openbsd
Posts: 723

Rep: Reputation: 56
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.
 
Old 10-21-2010, 07:14 AM   #10
MaxM
LQ Newbie
 
Registered: Oct 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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 ./lifeline.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 ./lifeline.sh
root 19049 0.6 3.2 122032 34568 pts/0 Sl+ 08:50 2:13 mono user2.exe
 
Old 10-21-2010, 08:02 AM   #11
igadoter
Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: slack 12.2, debian-Trinity , openbsd
Posts: 723

Rep: Reputation: 56
For me it seems that each mono user$.exe is a child of ./lifeline.sh script. My suspicions is that in fact you are killing ./lifeline.sh script which causes all their childs being killed as well.
 
Old 10-21-2010, 08:32 AM   #12
MaxM
LQ Newbie
 
Registered: Oct 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Would a simple renaming of each of the users lifeline.sh scripts do the trick? I didnt think that this could be the reason...
 
Old 10-21-2010, 08:34 AM   #13
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,727

Rep: Reputation: 452Reputation: 452Reputation: 452Reputation: 452Reputation: 452
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 ...
 
1 members found this post helpful.
Old 10-21-2010, 08:41 AM   #14
igadoter
Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: slack 12.2, debian-Trinity , openbsd
Posts: 723

Rep: Reputation: 56
I don't know how ./lifeline.sh 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
 
Old 10-21-2010, 11:55 AM   #15
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,543
Blog Entries: 51

Rep: Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606
Quote:
Originally Posted by MaxM View Post
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?..
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Writing shell script to kill processes ben1173 Linux - General 1 10-19-2010 03:23 AM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 07:35 AM
script to kill processes tradnbillies Linux - Software 2 04-28-2006 03:00 PM
script/command to kill processes running on particular port varunbihani Linux - General 2 07-08-2005 03:35 AM
shell script to kill all processes on specified port varunbihani Linux - General 1 04-19-2005 05:39 AM


All times are GMT -5. The time now is 08:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration