My Script For Process Checking is NOT Working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My Script For Process Checking is NOT Working
# 1  
Old 04-24-2012
My Script For Process Checking is NOT Working

Hello there ULF,

Good day! Just want to share my code and as well as my problem on why I'm not getting the output that I want. My original code was:

Code:
#!/usr/bin/sh

echo
echo -n "Please input an IP-Pool: "
read ip
echo
echo "Please wait....."
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker1.sh $ip > temp_output
sleep 15
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker2.sh $ip | grep -v NOTE >> temp_output
sleep 1
cp temp_output final_output
pd1=`cat temp_output | awk 'NR==10 {print $3}'`
bd1=`cat temp_output | awk 'NR==11 {print $3}'`
pu1=`cat temp_output | awk 'NR==12 {print $3}'`
bu1=`cat temp_output | awk 'NR==13 {print $3}'`
pd2=`cat temp_output | awk 'NR==18 {print $3}'`
bd2=`cat temp_output | awk 'NR==19 {print $3}'`
pu2=`cat temp_output | awk 'NR==20 {print $3}'`
bu2=`cat temp_output | awk 'NR==21 {print $3}'`
pd=$(expr $pd2 - $pd1)
bd=$(expr $bd2 - $bd1)
pu=$(expr $pu2 - $pu1)
bu=$(expr $bu2 - $bu1)
echo >> final_output
echo "Delta Summary:" >> final_output
echo "==============" >> final_output
echo "Delta Pkts-Dwn: $pd" >> final_output
echo "Delta Bytes-Dwn: $bd" >> final_output
echo "Delta Pkts-Up: $pu" >> final_output
echo "Delta Bytes-Up: $bu" >> final_output
cat final_output
rm temp_output
rm final_output
echo

Now since this script can be used by other users, there will be a possibility of having simultaneous running-process for this script which will lead to a wrongly data to be parsed, so in order for this one not to happen, I want to check if the process is running it will prompt the user that it's not yet safe to run the script otherwise begin using it. So I've decided to put something like this into my code:
Code:
#!/usr/bin/sh

psef=`ps -ef | grep IP-PoolSession-Summary.sh | grep -v grep | wc -l`
if [ $psef -eq 1 ]; 
then
echo
echo -e "\033[41;37mSorry! Another user is currently running the script. Please try again later.\033[0m"
echo
exit
else
echo -n "Please input an IP-Pool: "
read ip
echo
echo "Please wait....."
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker1.sh $ip > temp_output
sleep 15
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker2.sh $ip | grep -v NOTE >> temp_output
sleep 1
cp temp_output final_output
pd1=`cat temp_output | awk 'NR==10 {print $3}'`
bd1=`cat temp_output | awk 'NR==11 {print $3}'`
pu1=`cat temp_output | awk 'NR==12 {print $3}'`
bu1=`cat temp_output | awk 'NR==13 {print $3}'`
pd2=`cat temp_output | awk 'NR==18 {print $3}'`
bd2=`cat temp_output | awk 'NR==19 {print $3}'`
pu2=`cat temp_output | awk 'NR==20 {print $3}'`
bu2=`cat temp_output | awk 'NR==21 {print $3}'`
pd=$(expr $pd2 - $pd1)
bd=$(expr $bd2 - $bd1)
pu=$(expr $pu2 - $pu1)
bu=$(expr $bu2 - $bu1)
echo >> final_output
echo "Delta Summary:" >> final_output
echo "==============" >> final_output
echo "Delta Pkts-Dwn: $pd" >> final_output
echo "Delta Bytes-Dwn: $bd" >> final_output
echo "Delta Pkts-Up: $pu" >> final_output
echo "Delta Bytes-Up: $bu" >> final_output
cat final_output
rm temp_output
rm final_output
echo
fi

The problem with this code is that it's not prompting the user and still continues and proceed with the original code (which is after the else function) even if I already ran one before running it again.

Then I've modified the if/else by doing something like this:
Code:
#!/usr/bin/sh

psef=`ps -ef | grep IP-PoolSession-Summary.sh | grep -v grep | wc -l`
if [ $psef -eq 1 ]; then
echo
echo -e "\033[41;37mSorry! Another user is currently running the script. Please try again later.\033[0m"
echo
exit
elif [ $psef -eq 0 ]; then
echo
echo -n "Please input an IP-Pool: "
read ip
echo
echo "Please wait....."
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker1.sh $ip > temp_output
sleep 15
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker2.sh $ip | grep -v NOTE >> temp_output
sleep 1
cp temp_output final_output
pd1=`cat temp_output | awk 'NR==10 {print $3}'`
bd1=`cat temp_output | awk 'NR==11 {print $3}'`
pu1=`cat temp_output | awk 'NR==12 {print $3}'`
bu1=`cat temp_output | awk 'NR==13 {print $3}'`
pd2=`cat temp_output | awk 'NR==18 {print $3}'`
bd2=`cat temp_output | awk 'NR==19 {print $3}'`
pu2=`cat temp_output | awk 'NR==20 {print $3}'`
bu2=`cat temp_output | awk 'NR==21 {print $3}'`
pd=$(expr $pd2 - $pd1)
bd=$(expr $bd2 - $bd1)
pu=$(expr $pu2 - $pu1)
bu=$(expr $bu2 - $bu1)
echo >> final_output
echo "Delta Summary:" >> final_output
echo "==============" >> final_output
echo "Delta Pkts-Dwn: $pd" >> final_output
echo "Delta Bytes-Dwn: $bd" >> final_output
echo "Delta Pkts-Up: $pu" >> final_output
echo "Delta Bytes-Up: $bu" >> final_output
cat final_output
rm temp_output
rm final_output
echo
fi

As you can see, I've just changed the if/else to if/elif, but again the output is that after executing the script, it automatically exits. No prompt appeared even.

Can you please help me with this problem? I'm working on a SUSE Linux.


Thanks and Best Regards,
rymnd_12345

Last edited by rymnd_12345; 04-24-2012 at 04:56 AM.. Reason: typo error
# 2  
Old 04-24-2012
Try :
Code:
psef=`ps -ef | grep IP-PoolSession-Summary.sh | grep -v grep | wc -l`
if [ $psef -ge 1 ]; 
then
    echo
    echo -e "\033[41;37mSorry! Another user is currently running the script. Please try again later.\033[0m"
    echo
    exit
else

or
Code:
if ps -ef | grep -q [I]P-PoolSession-Summary.sh
then
    echo
    echo -e "\033[41;37mSorry! Another user is currently running the script.  Please try again later.\033[0m"
    echo
    exit
else

jean-Pierre.
# 3  
Old 04-24-2012
I would use a lock file for this (the number of the process may vary between different shells).
# 4  
Old 04-24-2012
Hi aigles,

Thanks for your input, I've tried using those two (2) suggestions but both of them resulted to same output like below:

Code:
user# ./IP-PoolSession-Summary.sh

Sorry! Another user is currently running the script.  Please try again later.

This is what I always get even if I didn't yet run the script prior running it again and I've also checked that no one uses it.


Hi radoulov.

How's that?


Thanks!

BR,
Raymond
# 5  
Old 04-24-2012
Try printing the output of the ps command at the beginning of your script:

Code:
(ps -ef | grep [I]P-PoolSession-Summary.sh)

You'll see the current instance of your script.
Than try to invoke the script like this and compare the output:

Code:
bash <your_script_name>
ksh <your_script_name>

To elaborate further: you cannot only check the presence of your script in the process list for obvious reasons: your script
is running when you're running the ps .. | grep .. pipeline, so this thest will always return true.
If you count with ps .. | grep -c .. the output will depend on the shell implementation
(some shells thread differently the last command in a pipeline).

So, again, for a more robust solution, I'd suggest the implementation of some
locking mechanism.

Last edited by radoulov; 04-24-2012 at 08:14 AM..
# 6  
Old 04-24-2012
Assuming that the script containing the duplicate session test is called
IP-PoolSession-Summary.sh then surely one copy running is not a problem ($psef -eq 1). The second and subsequent copies are a problem ($psef -ge 2). Don't forget to count the script you are running the test from!

Code:
psef=`ps -ef | grep "IP-PoolSession-Summary.sh" | grep -v "grep" | wc -l`
if [ $psef -ge 2 ]

I too recommend using flag files. The output from ps is not reliable enough for this type of processing.

Last edited by methyl; 04-24-2012 at 08:41 AM..
# 7  
Old 04-24-2012
Hi radoulov,

How is it? about the locking mechanism? How to do it?



Hi methyl,

I've tried simulating it, and still it returned the error prompt even without running the script yet, then I noticed that as if it includes the count of the
Code:
grep -v "grep"

still. So what I've did is to have it this way:
Code:
psef=`ps -ef | grep "IP-PoolSession-Summary.sh" | grep -v "grep" | wc -l`
if [ $psef -ge 3 ]; then
echo
echo -e "\033[41;37mSorry! Another user is currently running the script. Please try again later.\033[0m"

and it worked... don't know why it behaved that way.

Thanks for all you input.
Still expecting for a better solution like locking mechanism though.

BR,
Raymond
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking DataPump Process

Hi All, I am writing script for Env refresh for Oracle DB. I am using Datapump for that. If i start expdp or impdp, how can i know that export or import has completed. I have query for that. How will i integrate with script?. Or any command i can run from Linux side. Please share you... (1 Reply)
Discussion started by: pvmanikandan
1 Replies

2. Shell Programming and Scripting

Problem with a script for checking the state of a process

Hello Everyone, I have a process that should be always running. Unfortunately, this process is getting down almost every 10 minutes. I want to make a script that verify the state of this process: If the process is up, the script shouldn't do nothing and if it's down he should run it. Can... (3 Replies)
Discussion started by: adilyos
3 Replies

3. Solaris

Python script not working in batch process

Good morning, I have a python 2.3 script that runs stand alone as intended when tested, then it was put into a ksh script. when running the ksh script it runs as intended. The problem is that my script does not run when the ksh script is called by another user who runs a batch process (as of right... (1 Reply)
Discussion started by: mhahe
1 Replies

4. AIX

Script Checking LVM Mirror not Working

Hi, I need to know who can I create an script in order to check server mirror in AIX. I got this script !/usr/bin/ksh # # Check if a VG is mirrored. # # lsattr -El <lvname> -a strictness -a copies # If copies=2 and scrictness=y, then VG is mirrored # # LVs are retrieved via 'lsvg -l... (5 Replies)
Discussion started by: fede_mont
5 Replies

5. Shell Programming and Scripting

block process checking

How can i check block process in Linux? If found any what action is required? How to check the pid of process? How to kill the block process? How to find out bottleneck process? (3 Replies)
Discussion started by: learnbash
3 Replies

6. Shell Programming and Scripting

Process checking loop

Hi, I want to create a script who will check if the java process is running & if it finds the process is still there it continues to execute & when the process completes it exit from the script. I have written a code to check & notify the process existence but i am not getting how to write... (4 Replies)
Discussion started by: d8011
4 Replies

7. Shell Programming and Scripting

The checking of folder existance is not working...

Hi all, I have the following code: if ; then echo 'folder not exist'; else echo 'folder exist'; fi The "testing" folder is not exist in /home/batch , but thhe result is 'folder exist'. It seems that the code cannot detect that the folder "testing" not exist. ANybody know the... (1 Reply)
Discussion started by: suigion
1 Replies

8. Shell Programming and Scripting

Checking for multiple instances of a process

Hi I have a scenario where i need to check multiple instances of a running shell script (abc.sh) . How can I find from inside a running shell script whether any other instance of the same script is running or not? If any other instance of same shell script is running I need to exit from... (4 Replies)
Discussion started by: raghu.amilineni
4 Replies

9. Shell Programming and Scripting

Checking the cron process in unix

Hi Experts, Is there any command by which i can chk that the cron process is running fine? Say i have scheduled the cron to run at 10 o clock every monday,Do i need to wait for the time it runs and then chk using ps -ef? Please shed some light. Thanks Ashok. (2 Replies)
Discussion started by: Ashok_oct22
2 Replies

10. HP-UX

checking process existed

On HP-UX, in application, if the process id has been get with the getpid() and sotred in database, then other monitor process want to check it if the process is existed, are there any system function can do it? I do not want to use the shell script, because it should use popen function to excuted... (5 Replies)
Discussion started by: Frank2004
5 Replies
Login or Register to Ask a Question