Solved

Help understanding the below two lines of code.

Posted on 2011-09-13
4
287 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…

821 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