LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Scripting - Awk, if, then (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-awk-if-then-4175494149/)

T-Dub116 02-07-2014 08:46 AM

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...

grail 02-07-2014 09:08 AM

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.

T-Dub116 02-14-2014 10:11 AM

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.

szboardstretcher 02-14-2014 01:36 PM

This will give you the process id with a parent of 1.

Code:

ps -ef | grep lpd | grep " 1 " | awk '{print $2}'


All times are GMT -5. The time now is 11:06 AM.