LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 11-18-2010, 01:00 PM   #1
d072330
Member
 
Registered: Nov 2007
Location: USA
Distribution: CentOS 5/6
Posts: 186

Rep: Reputation: 6
Question Perl to kill off some processes


I am at a loss on how to even start this Perl script. I have the following processes:

Command used to get this info:

ps aux --forest | grep -e process_name -e ksh | awk '{if ($1 == "user1" && $1 != "root" && $1 != "UID" && $1 != "xfs" && $1 != "mfg" && $1 != "mfgnet") print $0}'

Processes
user1 2819 0.0 0.0 4272 612 ? S Nov17 0:00 \_ -pksh-ksh
user1 2820 0.0 0.0 64956 1584 pts/833 Ss+ Nov17 0:00 \_ -ksh

user1 2918 0.0 0.0 4272 616 ? S Nov17 0:00 \_ -pksh-ksh
user1 2922 0.0 0.0 64956 1620 pts/689 Ss+ Nov17 0:00 \_ -ksh
user1 6657 0.0 0.4 1322176 140728 pts/689 S+ Nov17 0:00 \_ /usr/bin/process_name

I need a way to kill off the pids 2819, 2820 because they do not have a process tied to them like pids 2918, 2922 and 6657.

The way it works is peek shell (pid 2918)is opened then it starts a ksh (pid 2922) session then from there the end user runs a command (pid 6657).

Is there a way to do this. All I need is an idea to get started the rest I can do.

Last edited by d072330; 11-18-2010 at 01:01 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-18-2010, 01:26 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by d072330 View Post
All I need is an idea to get started the rest I can do.
Actually, the way we usually do things is you get started and run into a difficulty, and post your code here, and we help out with specific questions.

But if you want to get started, do this:
Code:
which perl | sed -e 's/^/#!/' > perlscript.pl
chmod 700 perlscript.perl
... and go from there.
 
Old 11-18-2010, 01:32 PM   #3
d072330
Member
 
Registered: Nov 2007
Location: USA
Distribution: CentOS 5/6
Posts: 186

Original Poster
Rep: Reputation: 6
Angry

@wje_lq - haha very funny!

I know that is protocol. I am having a brain cramp on how to started to basically seperate the pids with 2 lines (pksh, ksh process) and the pids with 3 lines (pksh, ksh, process_name).

Any real help will be appreciated.
 
Old 11-18-2010, 03:11 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
So wait. Your input data comes in paragraphs of either two or three lines, followed by a blank line. In the paragraphs consisting of exactly two lines, you want to kill the processes numbered at the beginning of each line. You want to drop on the floor the paragraphs consisting of three lines. You want to puke if a paragraph consists of fewer than two lines or more than three lines.

Is this correct?
 
Old 11-18-2010, 04:10 PM   #5
d072330
Member
 
Registered: Nov 2007
Location: USA
Distribution: CentOS 5/6
Posts: 186

Original Poster
Rep: Reputation: 6
Quote:
ps aux --forest | grep -e process_name -e ksh | awk '{if ($1 == "user1" && $1 != "root" && $1 != "UID" && $1 != "xfs" && $1 != "mfg" && $1 != "mfgnet") print $0}'
This is where my data comes from.

It returns this output. Notice I put a break between the ones that have a pksh,ksh,process and the only one that does not have all three. The very last one (in bold) that only has pksh, ksh those are the ones I need to kill off via a script.

Quote:
user1 23216 0.0 0.0 4272 612 pts/682 S+ 14:55 0:00 \_ -pksh-ksh
user1 23217 0.0 0.0 64956 1620 pts/707 Ss+ 14:55 0:00 \_ -ksh
user1 25830 0.0 0.0 1322180 13772 pts/707 S+ 15:03 0:00 \_ process_name
user1 23306 0.0 0.0 4272 620 pts/702 S+ 14:55 0:00 \_ -pksh-ksh
user1 23308 0.0 0.0 64956 1620 pts/732 Ss+ 14:55 0:00 \_ -ksh
user1 23422 0.0 0.0 1322164 9604 pts/732 S+ 14:55 0:00 \_ process_name
user1 25662 0.0 0.0 4272 620 ? S Nov16 0:00 \_ -pksh-ksh
user1 25663 0.0 0.0 64956 1620 pts/720 Ss+ Nov16 0:00 \_ -ksh
user1 26002 0.0 0.0 1323968 29840 pts/720 S+ Nov16 0:00 \_ process_name
user1 7138 0.0 0.0 4272 648 ? S Nov16 0:00 \_ -pksh-ksh
user1 7139 0.0 0.0 64956 1616 pts/1464 Ss+ Nov16 0:00 \_ -ksh
user1 8020 0.0 0.0 1322396 13588 pts/1464 S+ Nov16 0:00 \_ process_name
user1 30640 0.0 0.0 4272 628 ? S Nov16 0:00 \_ -pksh-ksh
user1 30641 0.0 0.0 64956 1624 pts/1066 Ss+ Nov16 0:00 \_ -ksh
user1 31728 0.0 0.0 1322164 12008 pts/1066 S+ Nov16 0:00 \_ process_name
user1 2773 0.0 0.0 4272 652 ? S Nov17 0:00 \_ -pksh-ksh
user1 2774 0.0 0.0 64956 1628 pts/1679 Ss+ Nov17 0:00 \_ -ksh
user1 3530 0.0 0.0 1322360 13088 pts/1679 S+ Nov17 0:00 \_ process_name
user1 2918 0.0 0.0 4272 616 ? S Nov17 0:00 \_ -pksh-ksh
user1 2922 0.0 0.0 64956 1620 pts/689 Ss+ Nov17 0:00 \_ -ksh
user1 6657 0.0 0.4 1322176 140728 pts/689 S+ Nov17 0:00 \_ process_name


user1 2819 0.0 0.0 4272 612 ? S Nov17 0:00 \_ -pksh-ksh
user1 2820 0.0 0.0 64956 1584 pts/833 Ss+ Nov17 0:00 \_ -ksh
And I need to do this for hundreds of users not just one user, thus the script.
 
Old 11-19-2010, 03:57 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
To the OP: do it the Perl way: http://search.cpan.org/~durist/Proc-...rocessTable.pm . I.e. you do not need 'ps'. 'grep', 'awk' - everything can be done from inside Perl.
 
2 members found this post helpful.
Old 11-19-2010, 10:08 AM   #7
d072330
Member
 
Registered: Nov 2007
Location: USA
Distribution: CentOS 5/6
Posts: 186

Original Poster
Rep: Reputation: 6
Thanks! That is the type of answer I was looking for.
 
Old 01-04-2011, 05:33 PM   #8
d072330
Member
 
Registered: Nov 2007
Location: USA
Distribution: CentOS 5/6
Posts: 186

Original Poster
Rep: Reputation: 6
Ok I have time to look at this again. I have created some perl code that gives me this:

user1 13794 13493 2010 ? 09:09:45 2011 pts/3874 -pksh-ksh
user1 13795 13794 2010 ? 09:09:45 2011 pts/3868 -ksh
user1 13854 13795 2010 ? 09:09:45 2011 pts/3868 db_process

user1 10094 10093 2010 ? 09:09:45 2011 pts/3874 -pksh-ksh
user1 10095 10094 2010 ? 09:09:45 2011 pts/3868 -ksh

I thought the "?" meant it was dead but it does not because I can connect to the 13794 Peek session and it has the question mark.

I need to be able to kill the second group because it has no database connection and from what we can tell it is dead. If we try and reconnect to that session id via Peek it states this session is no longer valid. The one with the db_process is able to be connected to. This would not be such an issue but we only have 1600 Peek connections and need to keep this tidy. If this was killing one process or a few I could do it by hand but it is 100's.

So how do I get my perl code to check this line:
user1 13794 13493 2010 ? 09:09:45 2011 pts/3874 -pksh-ksh

and grab the 13794 id and then check line number two in the third column for this very same number 13794 and then check the 2nd column, 2nd line id 13795 and see if it matches column 3, line3 13795.

I was told to use Proc::ProcessTable but I am not sure how to kill off the ones I want with this output.

This is above my scripting know how and it would be great if someone out there could give me some direction.

Thanks in advance!

Last edited by d072330; 01-04-2011 at 05:34 PM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
To kill all processes except mine balakrishnay Linux - General 2 11-06-2009 10:27 AM
Use only one "kill" to kill father and child processes Xosen Programming 7 08-28-2008 03:33 AM
Can't kill certain processes.... Basslord1124 Slackware 2 01-29-2008 10:28 AM
how to kill processes htamayo Linux - Newbie 2 09-25-2007 10:27 AM
how to use kill to kill a batch of processes with same name? dr_zayus69 Linux - Software 2 09-03-2005 06:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration