Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-07-2014, 09:46 AM   #1
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 ]
ps -ef |grep lpd |awk '{print $3}'
$status = 1
kill -9 |awk '{print $2}'
when |awk '{print $3}' = 1
do nothing

# 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, 10:08 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 11:11 AM   #3
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, 02:36 PM   #4
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,827

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

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


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
win32,shell code,shell programming,shell scripting? mr.cracker Linux - Newbie 4 07-13-2013 12:20 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
awk shell scripting ApacheRoseXbones Programming 7 07-11-2008 01:42 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM

All times are GMT -5. The time now is 08:24 PM.

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