Solved

Help understanding the below two lines of code.

Posted on 2011-09-13
4
284 Views
Last Modified: 2012-05-12
Hi,
Can anyone explain me what is the logic/meaning for the below code. I am new to shell scripting.

if [ -n "$(ps -ef|egrep system_cron |egrep bash|egrep -v ${PID})" ]
  then
     ps -ef|awk '/system_cron/&&/bash/&&/'"${PID}"'/{print $2,$3}'|xargs kill
     exit 0
  fi
0
Comment
Question by:new_perl_user
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Maciej S earned 500 total points
ID: 36531351
if [ -n "$(ps -ef|egrep system_cron |egrep bash|egrep -v ${PID})" ]

-n means if following string is not empty. In your case the string is output from command:
ps -ef | egrep system_cron | egrep bash | egrep v- ${PID}
So - if there is some output, then this condition is true.

If it is true, run:
ps -ef | awk '/system_cron/&&/bash/&&/'"${PID}"'/{print $2,$3}'|xargs kill
which means: print list of processes (ps -ef) and print only lines containing patterns "system_cron", "bash", and something which is stored in PID variable. Print only second and third fields from this line (or lines). Then, run kill with this fields as parameters (which will be process id and parent process id - as these are second and third fields in ps -ef output).
After running this kill command, stop executing your script (exit 0).
0
 

Author Comment

by:new_perl_user
ID: 36531430

Thank you for the explanation and a small doubt too..

So are they trying to kill the previous process  or only the current one. because this code is written within
the same system_cron script.
0
 
LVL 9

Expert Comment

by:parparov
ID: 36531573
It depends where ${PID} is obtained from.
0
 

Author Comment

by:new_perl_user
ID: 36532072
Thank you.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now