func () {
local arrayOfParams=$@
local CMD="ps aux | grep -v grep"
for item in ${arrayOfParams[@]};
do
local CMD="$CMD | grep -e $item"
done
echo "Current process PID : $$"
echo "`date`: CMD is -> $CMD"
local isProcRunning=`eval ${CMD}`
if [[ 0 -eq `echo "$isProcRunning" | wc -l` ]];
then
echo "`date`: Following process is running: "
echo "$isProcRunning"
echo ""
if [[ "$FORCE_RUN" == true ]];
then
echo "`date`: User forced the run of current process ..."
# we extract the PIDs of proces mathcing our search criteria
PIDS=(`echo $isProcRunning | awk '{print \$2}'`)
for PID in "${PIDS[@]}";
do
# we extract the command running on said PID so that we know what we kill
local PIDCMD=`ps -o cmd -p ${PID} --no-headers`
echo "`date`: Killing PID: $PID with CMD -> $PIDCMD"
kill -9 $PID
done
else
echo "`date`: Current process will exit!!!"
fi
exit 0
fi
}
The above code is meant to check if current process is already running .
I use ps -ef
and grep based on some params which should be found under CMD
I remove the line coresponding to the current PID
and then I expect the wc -l
to return 0, but it's returning 1.
I run that line outside of the script and I get 0 indeed.
Any idea why this is happening ?