Solved

Help understanding the below two lines of code.

Posted on 2011-09-13
4
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

635 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