grep the number of self process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep the number of self process
# 1  
Old 04-09-2009
grep the number of self process

Dear All,

I plan to write a script and in the beginning, I will check if there's any existing previous process running, if YEs, then exit directly, if Not, then continue.

Code:
#!/bin/bash

NUM=`ps -ef | grep $0 | grep -v grep | wc -l`

ps -ef | grep $0 | grep -v grep
echo "NUM ==> $NUM"

if [ $NUM -ne 1 ]; then
   echo "previous processing running, exit"
   exit 1
fi

echo "Hello...  I got here.."


I can not understand, why running this script will return :


====================================
root 7815 29364 0 09:30 pts/0 00:00:00 /bin/bash ./2.sh
NUM ==> 2
previous processing running, exit
====================================

from "ps -ef | grep $0 | grep -v grep " command, it's obvious that only one line return, why $NUM will become 2 ??


any advice on this?

Thanks.

Last edited by Yogesh Sawant; 04-10-2009 at 08:17 AM.. Reason: added code tags
# 2  
Old 04-10-2009
look into 'man fuser'.
# 3  
Old 04-10-2009
you may want to take care of situations such as if someone is editing the same file in an editor, say using vi
# 4  
Old 04-10-2009
# 5  
Old 04-13-2009
Error

Well, I'm a little disappointed at the responses on this one... so... I'll try something a little different.

In your script, the return of 2 is happening for a very specific reason. I'll give you a clue first.

Instead of piping the ps command all the way through the word count, eliminate it and print out the return string from the ps command when you spawn it through the grave quotes.

I.e... STRING=`ps -aef | grep $0 | grep -v grep`

You will note that two lines are returned... look closely, the process ID's should give you a clue as to what the kernel is doing..

My return on a reduced example of your script is as follows:

Code:
aa@lnx2:~/bin$ 2.sh
aa        1530   537  0 18:29 pts/12   00:00:00 /bin/bash /home/aa/bin/2.sh
STRING ==> aa        1530   537  0 18:29 pts/12   00:00:00 /bin/bash /home/aa/bin/2.sh
aa        1534  1530  0 18:29 pts/12   00:00:00 /bin/bash /home/aa/bin/2.sh
NUM ==> 2

# 6  
Old 04-13-2009
a tricky problem.... the answer is . . ..

the back-ticks are creating yet another sub-shell.

But even acknowledging that, these types of checks are tricky.
And I prefer to just check the existance of a lock file.

If it's there, quit.
Else, create it and continue processing.

Code:
FLOCK=/tmp/aardvark.lock

if [ -f $FLOCK ]; then
  echo already running since `cat $FLOCK`
  exit 1
fi

date > $FLOCK

echo continuing on my merry way...


Last edited by quirkasaurus; 04-13-2009 at 05:57 PM.. Reason: oops. first effort FAILED.
# 7  
Old 04-15-2009
Error

Quote:
Originally Posted by quirkasaurus
a tricky problem.... the answer is . . ..

the back-ticks are creating yet another sub-shell.
This is the correct answer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recorder number of process per user

Hello Team, I would like to use a shell script that run each 15 minutes in order to recorder the number of process per user I request your help in order to build an awk script under Solaris from the following command or similar: ps -fea -o user | sort | uniq -c | sort -k 2 648 ... (3 Replies)
Discussion started by: csierra
3 Replies

2. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

3. Solaris

To get process id for port number

Hi All, How to get the list of port numbers and it is correspoding proceses id that are currently running on. Please suggest and it is urgent Thanks. (7 Replies)
Discussion started by: rbalaj16
7 Replies

4. Shell Programming and Scripting

grep the process id and kill all the filtered process

Hi I want to write a shell script which can find the process id's of all the process and kill them eg: ps ax | grep rv_ 3015 ? S 0:00 /home/vivek/Desktop/rv_server 3020 ? S 0:00 /home/vivek/Desktop/rv_gps 3022 ? S 0:00 /home/vivek/Desktop/rv_show ... (7 Replies)
Discussion started by: vivek_naragund
7 Replies

5. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

6. Solaris

Number of process per user session

Hi All, Do we have any option through which we can limit the number of process which can be started by single user session. Thanks (3 Replies)
Discussion started by: kumarmani
3 Replies

7. UNIX for Dummies Questions & Answers

Find what process on port number

Hi, I am on a Sun Solaris and I want to find out which process is allocated on a certain port. How can I do that? BR Andreas (4 Replies)
Discussion started by: mr_andrew
4 Replies

8. UNIX for Advanced & Expert Users

number of process

Hi, under AIX 5.2 which (environment) parameter defins the number of processes for the server ? Many thanks. (2 Replies)
Discussion started by: big123456
2 Replies

9. Shell Programming and Scripting

number of process?

how i know number of process are currently run on my user area (2 Replies)
Discussion started by: piyush_movadiya
2 Replies

10. Programming

Count Number Of Threads in a Process

I am trying to find out that how many number of threads are currently running or in any other state which is created by POSIX standard in a process. First I have defined a variable called proc_var of type proc defined in sys/proc.h.Next I open up the dir /proc and per directory wise I do an ioctl... (7 Replies)
Discussion started by: S.P.Prasad
7 Replies
Login or Register to Ask a Question