LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-07-2014, 08:46 AM   #1
T-Dub116
Member
 
Registered: Aug 2013
Location: Dolyestown
Posts: 88

Rep: Reputation: Disabled
Shell Scripting - Awk, if, then


My lpd printing seems to be running my CPU high and I want to put together a script to search and destroy, so to speak.

so far I have:

sar -u 2 5 |awk '{print $5}' # if this outputs a 0 that means the cup is running very hard
if [ $status = 0 ]
then
ps -ef |grep lpd |awk '{print $3}'
$status = 1
kill -9 |awk '{print $2}'
when |awk '{print $3}' = 1
else
do nothing
fi


# What I am looking for, When - sar -u 2 5 |awk '{print $5}' comes out with 0 I want to then run ps -ef |grep lpd |awk '{print $3}' and if this displays a 1 I want to kill process number before the 1 in the same row just different column:

root 10516 3772 0 09:41:10 ? 00:00:00 /usr/lib/lpd
root 3772 1 82 09:41:00 ? 00:00:00 /usr/lib/lpd
root 10476 7780 2 09:41:10 ttyp5 00:00:00 grep lpd

In this case the process number I want killed is 3772 but this always changes the 1 is always the same.

Then check sar -u 2 5 again to confirm that it does not = 0 and done.

From the begining if cpu does not = 0 else do nothing...
 
Old 02-07-2014, 09:08 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure I follow your logic? When PPID is 1 this means that it was kicked off as part of your init processes. So I would expect times and so on to be large as it has been running since
your machine started. Are you really sure this should be killed and in a way that does not seem to stop the service that created it??

You may want to indicate actual code and pseudo code as some of what you have written will not work at all as displayed.

Also, please use [code][/code] tags around code and data to make it more legible and preserve formatting.

Lastly, grep is not required if awk is being used as it can already do regex matching.
 
1 members found this post helpful.
Old 02-14-2014, 10:11 AM   #3
T-Dub116
Member
 
Registered: Aug 2013
Location: Dolyestown
Posts: 88

Original Poster
Rep: Reputation: Disabled
Let me explain my problem,

For some reason printing has just stopped in my office for printers that are connected to this server (Linux). I have to run the command /usr/lib/lpd for the printers to start printing again, but I can not leave this command running because it puts the CPU to full capacity. So I have to kill the process and I check to make sure the CPU is not running hard. (sar -u 2 5)

Do you have any idea what might be causing this to happen. If I have to print a lot but not all at once I have to keep running the command and killing the process so my CPU is not running hard, other wise I get an E-mail from my VM Serve Tech telling me that my CPU us running to hard.
 
Old 02-14-2014, 01:36 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
This will give you the process id with a parent of 1.

Code:
ps -ef | grep lpd | grep " 1 " | awk '{print $2}'
 
1 members found this post helpful.
  


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
win32,shell code,shell programming,shell scripting? mr.cracker Linux - Newbie 4 07-12-2013 11:20 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
awk shell scripting ApacheRoseXbones Programming 7 07-11-2008 12:42 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:07 PM.

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