Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`
# 1  
Old 07-30-2014
Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count.

Code:
`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

basically it is assigned to a variable

Code:
ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`


the problem is that most of the time it returns inconsistent number when there are no other same process in this case the sfs_pcard_load_file.ksh is currently running. it returns 2 or 3 when it should be only returning 1 right?

i tried this code also and giving me the same issue.

Code:
ISRUNNING=`ps -ef | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i am using the code in this korn shell script below.
Code:
#!/usr/bin/ksh
#####################################################################
#
# Initialization file
#--------------------------------------------------------------------
. ./pcard_load_data.ini


#####################################################################
#
# Parameter definition
#--------------------------------------------------------------------
pSec=$1
pLoopCnt=$2

#
#####################################################################
#
# Variable Initialization
#--------------------------------------------------------------------
#
CURDATE=`date +%m/%d/%Y" "%H:%M:%S`
#LOGDATE=`date +%m%d%Y"_"%H%M%S`
LOGDATE=`date +%m%d%Y"`
JOBNAME=`basename $0 | cut -d. -f1`
LOG_FILE=${LOG_DIR}/${JOBNAME}_${LOGDATE}.log
LOG_FILE_ERROR=${LOG_DIR}/${JOBNAME}_${LOGDATE}_err.log
ORALOGFILE=${LOG_DIR}/PCARDDAILY${LOGDATE}.log

# log file header
echo "+------------------------------------------------------------+" >> $LOG_FILE
echo "This is the log file of pcard_load_file.ksh script"  >> $LOG_FILE
echo "Run date: $CURDATE\n" >> $LOG_FILE

#####################################################################
#
# Check for required number of parameters if any are expected
#--------------------------------------------------------------------
#
if [[ $# == 0 ]]; then
  print "WARNING: Required Parameters Missing."
  print "USAGE: `basename $0`"
  echo "WARNING: Required Seconds and Number of Attempts Parameters Missing"  >> $LOG_FILE
  echo "example usage: `basename $0` 30 10\n">> $LOG_FILE
  # use the default number when parameter is missing
  pSec=60
  pLoopCnt=10
  #exit 1
fi
#


#
#####################################################################
#
# Step: 1
#
# Check for already running process and any files to process
#
#--------------------------------------------------------------------
#
echo "Step 1 Start..." >> $LOG_FILE
ctr=0
while [ ${ctr} -lt ${pLoopCnt} ] 
do
  ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`
  ctr=$(($ctr + 1))
  echo "number of same process currently running: $ISRUNNING" >> $LOG_FILE
  echo "number of attempt to check for duplicate runs: $ctr" >> $LOG_FILE
  if (( ${ISRUNNING} > 1 )); then
    echo "ERROR: Process is already running." >> $LOG_FILE
    if (( ${ctr} == ${pLoopCnt} )); then
      echo "Please contact apps dev group" >> $LOG_FILE
    fi
    echo "+-----+\n" >> $LOG_FILE
    if ((${ctr} == ${pLoopCnt})) then
      exit 0
    fi
  else
    ctr=10
  fi
  sleep $pSec
done
#
NUMFILES=`ls -1 $INPUT_DIR | wc -l`
 if (( ${NUMFILES} == 0 )); then
   echo "WARNING: There are no files in input directory to process.\n" >> $LOG_FILE
   exit 1
 fi
echo "End of Step 1\n" >> $LOG_FILE
#
#####################################################################
#
# Step: 2
#
# Process files in INPUT_DIR
#
#--------------------------------------------------------------------
#
echo "Step 2 Start...\n" >> $LOG_FILE
cd $INPUT_DIR
#
NUMFILES=`ls -1 *.csv | wc -l`
 if (( ${NUMFILES} == 0 )); then
   echo "WARNING: There are no files in process directory.\n" >> $LOG_FILE
   #if there are no input files there will be no output file to be created
   #needs to create a null output file
   cp pcard.null PCARDDAILY${LOGDATE}.txt
   echo "         Created the $INPUT_DIR/PCARDDAILY${LOGDATE}.txt null file to replicate the required file.\n" >> $LOG_FILE
   #needs to create a database log file PCARDDAILY${LOGDATE}.log
   echo "WARNING: There are no extracted files.\n" >> $ORALOGFILE
   exit 0
 fi
#
 for filename in $(ls -1 *.csv)
 do
  echo "Processing $filename \n" >> $LOG_FILE
  #
  echo "Copied $filename to log directory: $LOG_DIR\n" >> $LOG_FILE
  cp $filename $LOG_DIR
  #
  echo "Executed PL/SQL package for $filename\n" >> $LOG_FILE
  SQL_LOG_FILE=${LOG_DIR}/${filename}_${LOGDATE}.log
  DB_RSLT=`sqlplus -s <<EOF >> $SQL_LOG_FILE
   $USERID
   declare
    v_result varchar2(200);
   begin
    cca_app.Pa_SFS_PCARD_Daily_Bulkload.Pr_Daily_Process('$filename');
   end;
/ 
   exit 0
  EOF`
  wait
  #
  RC=$?
  #
  echo $DB_RSLT >> $LOG_FILE
  echo "Deleted $filename from $INPUT_DIR\n" >> $LOG_FILE
  rm -f $filename
  #
 done
 echo "End of Step 2\n" >> $LOG_FILE

#
#####################################################################
#
# Step: 3
#
# check for file PCard (Output PCARDDAILY*.txt) 
#
#--------------------------------------------------------------------
#
echo "Step 3 Start...\n" >> $LOG_FILE
cd $INPUT_DIR
#
NUMFILES=`ls -1 PCARD*.txt | wc -l`
 if (( ${NUMFILES} == 0 )); then
   echo "WARNING: There are no PCARD*.txt files in the output $INPUT_DIR directory.\n" >> $LOG_FILE
   cp pcard.null PCARD${LOGDATE}.txt
   echo "         Created a PCARD${LOGDATE}.txt null file to replicate the file.\n" >> $LOG_FILE
   exit 1
 fi
#
echo "End of Step 3\n" >> $LOG_FILE

#
#####################################################################
#
# Step: 4
#
# delete files from log and completed dir > 30 days old   
#
#--------------------------------------------------------------------
#
echo "Step 4 Start...\n" >> $LOG_FILE
#
cd $LOG_DIR
find . -name "*.csv" -mtime +30 -exec rm -f {} \;                 
cd ${LOG_DIR}                                                                  
find . -name "*.log" -mtime +30 -exec rm -f {} \;                 
#
 echo "End of Step 4\n" >> $LOG_FILE
#
exit 0

please advise thank you.

-warren
# 2  
Old 07-30-2014
You might also fuser the shell script to see what PIDs have it open.
# 3  
Old 07-30-2014
i tried this and it does not return anything

Code:
$ fuser -V sfs_pcard_load_file.ksh
sfs_pcard_load_file.ksh:

$

in the script
Code:
fopen=`fuser -V sfs_pcard_load_file.ksh`
echo "file open info: $fopen" >> $LOG_FILE

# 4  
Old 07-30-2014
What's your OS?
# 5  
Old 07-30-2014
The traditional way to avoid ps | grep | cut | sed | awk | tr | ed | head | tail | kitchen | sink is to use a PID file. Store your program's PID ($$ in shell) in a file so other things can track if you're still running or died unexpectedly.

Code:
#!/bin/sh

# Traditional location is /var/run/..., but not everything can write there
PIDFILE="/tmp/sfs_pcard.pid"

if [ -e "$PIDFILE" ]
then
        read PID < "$PIDFILE"
        if ps "$PID" >/dev/null 2>/dev/null
        then
                echo "$PIDFILE has $PID, which is still running" >&2
                exit 1
        fi
fi

# PID file either missing, or the process in it no longer exists.
echo "$$" > "$PIDFILE"
# Delete the PID file when the process exits
trap "rm -f '$PIDFILE'" EXIT

# Rest of the program's code.
...

Much less of a kludge and should work on most systems.
# 6  
Old 07-30-2014
OS is IBM-AIX Version 6.1

---------- Post updated at 04:40 PM ---------- Previous update was at 03:48 PM ----------

i run this script with these small line of codes for debugging purpose.

sfs_pcard_load_file.ksh
Code:
#!/usr/bin/ksh
ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc
 -l`
pInfo=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh`
echo "number of same process currently running: $ISRUNNING"
echo "these are the process currently running: $pInfo"

and it returned this output:
Code:
$ sh sfs_pcard_load_file.ksh
number of same process currently running:        2
these are the process currently running: grep -i sfs_pcard_load_file.ksh
sh sfs_pcard_load_file.ksh

it looks like the grep command inside the script is also being counted. how do i exclude it? please advise. thank you.

Last edited by wtolentino; 07-30-2014 at 05:48 PM..
# 7  
Old 07-30-2014
As Corona688 pointed out, try and use a PID file - It's more portable reliable and best practice.

As for your ps issue, avoid using -f and -o args these two arguments are at odds with each other -f is for full listing and -o is limiting to specific list of arguments.

One popular way to avoid counting the grep command is to use an RE that doesn't match itself for example:

Code:
ISRUNNING=`ps -e -o args | grep -i [s]fs_pcard_load_file.ksh | wc -l`


Last edited by Chubler_XL; 07-30-2014 at 06:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to strictly grep (with before and after args) for last N number of occurrences?

Here is my grep string to print only the last occurrence, with before and after lines. Note that the tail Argument is sum of B and A args + 1, so that it prints the data of only the last 1 match. Now I need to print last 2 such matches. I thought of doubling the tail arg like 5+5+1 (For -- line),... (2 Replies)
Discussion started by: samjna
2 Replies

2. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

3. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

4. Shell Programming and Scripting

AWK/GREP: grep only lines starting with integer

I have an input file 12.4 1.72849432773174e+01 -7.74784188610632e+01 12.5 9.59432114416327e-01 -7.87018212757537e+01 15.6 5.20139995965960e-01 -5.61612429666624e+01 29.3 3.76696387248366e+00 -7.42896194101892e+01 32.1 1.86899877018077e+01 -7.56508762501408e+01 35 6.98857157014640e+00... (2 Replies)
Discussion started by: chrisjorg
2 Replies

5. Shell Programming and Scripting

ps -eaf gives inconsistent result

The command ps -eaf gives inconsistent result when executed on XYZ server (the command shows 1, 2 or 3 instances of the process. Ideally it should show only 1). I have attached the script file that was executed and the corresponding output. When the same script was executed on ABC server it gave... (1 Reply)
Discussion started by: rebels
1 Replies

6. UNIX for Dummies Questions & Answers

Advanced grep'in... grep for data next to static element.

I have a directory I need to grep which consists of numbered sub directories. The sub directory names change daily. A file resides in this main directory that shows which sub directories are FULL backups or INCREMENTAL backups. My goal is to grep the directory for the word "full" and then... (2 Replies)
Discussion started by: SysAdm2
2 Replies

7. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

8. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

9. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

10. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies
Login or Register to Ask a Question