In a script we are checking if any other instance of the same script is running to make sure that we don't run multiple threads, and if there is only one then we are proceeding otherwise coming out of it ..
To do this we check as below,
ps -ef | grep -i 'scriptname' | grep -v grep | wc -l
this at times returns 2, but when I try to display
ps -ef | grep -i 'scriptname' | grep -v grep
it shows only the current instance that is getting started ..
Can anyone please help me in figuring out the problem.
scripts usually run external programs. This means they must fork() and then exec(). If you catch it after the fork and prior to the exec, you will see two processes. Also be aware that a pipeline like this:
one | two | three
will involve multiple forks.
Sorry, forgot to mention about one important thing though ..
this script works fine if we run it normally, but when the script is called after a server reboot (automatically called as part of unix start up) , it says it can see 2 processes with the name 'scriptname' but lists only one , that is the one which its correctly starting ..
Thanks Perdarabo ..
Looks like I understand what you are saying ..
So if my script is called runMonitor.sh, then are you saying that if i give
ps -ef | grep -i 'runMonitor.sh' | grep -v grep | wc -l
will invoke 1 fork for each command that has the name 'runMonitor.sh' ?
sorry I posted this query in advanced column , but I am quite comfortable in unix commands but not in the way they are executed..
So please help