Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help understanding the below two lines of code.

Posted on 2011-09-13
4
Medium Priority
?
291 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

886 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