Process count problem in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Process count problem in script
# 1  
Old 07-04-2013
Hammer & Screwdriver Process count problem in script

Hi,

I am trying to make a script which should execute if the same is not getting executed already.
To implement this check, I wrote a fragment of code as below

Code:
$ cat abc.sh
#!/bin/bash

check=0

#grepping "sh -x abc.sh" because if i check only for "abc.sh" and the file is open in say vim, it would increase the count unnecessarily 
check=`ps -ef|grep "sh -x abc.sh"|grep -v grep|wc -l`

echo "$check"

sleep 180

On execution the output is as below [using -x for verbose purpose]
Code:
$ sh -x abc.sh
+ check=0
++ ps -ef
++ grep 'sh -x abc.sh'
++ grep -v grep
++ wc -l
+ check=2
+ echo 2
2
+ sleep 180

However during this sleep time if I am checking the process count from another terminal it gives me check=1

Code:
$ check=`ps -ef | grep "sh -x abc.sh" | grep -v grep | wc -l`; echo $check
1

Unable to understand why this difference, please help Smilie

Thanks
Sam
# 2  
Old 07-04-2013
I guess its because..

say when you run the ps -ef under backquotes it will run it under subshell and command would be fired and captured by same ps -ef so it will give 2 as output

I may not be 100% correct here
# 3  
Old 07-04-2013
The problem may be that sometimes the grep process is spotted too, which you are trying to handle. Could you consider using the -c flag of grep and neatening your code a bit?
Code:
check=`ps -ef|grep -c "sh -x abc.s[h]"`

The square brackets mean that the contents are a range of characters, in this case only h which might seem a little odd, but it means that your grep will not find itself. The count is a function of grep

Can you have a go with the above streamlined code and if it's still confusing, take the -c flag off and show us the output.



Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 07-04-2013
@Robin, didn't work Smilie
still getting check=2 inside the program and 1 on command line

Code:
$ cat abc.sh
#!/bin/bash

check=0

check=`ps -ef|grep -c "sh -x abc.s[h]"`

echo "$check"

sleep 180

o/p
Code:
$ sh -x abc.sh
+ check=0
++ ps -ef
++ grep -c 'sh -x abc.s[h]'
+ check=2
+ echo 2
2
+ sleep 180

command line
Code:
$ ps -ef | grep "sh -x abc.s[h]"
sam     1234  5678  0 07:25 pts/13   00:00:00 sh -x abc.sh

---------- Post updated at 05:03 PM ---------- Previous update was at 04:59 PM ----------

@vidyadhar
I tried removing the shebang line and executing, still giving check=2
so may not be problem of changing shells

Code:
$ sh -x abc.sh
+ check=0
++ ps -ef
++ grep -c 'sh -x abc.s[h]'
+ check=2
+ echo 2
2
+ sleep 180

# 5  
Old 07-04-2013
Can you insert the lines:-
Code:
ps -fp $$
ps -ef|grep 'sh -x abc.s[h]'

above the active check=`ps .... and post the output. I'd like to see what is 'us' and what is not. There is no need to set the value of check to zero before you start. If grep -c does not match anything, it will return the value zero.



I'm a bit confused, I must admit. Smilie On AIX I get:-
Code:
RBATTE1>sh -x abc.sh
+ ps -fp 17864
UID        PID  PPID  C STIME TTY          TIME CMD
RBATTE1  17864 17757  0 12:54 pts/10   00:00:00 sh -x abc.sh
+ ps -ef
+ grep 'sh -x abc.s[h]'
RBATTE1  17864 17757  0 12:54 pts/10   00:00:00 sh -x abc.sh
++ ps -ef
++ grep -c 'sh -x abc.s[h]'
+ check=2
+ echo 2
2
+ sleep 180

.....yet on RHEL I get:-
Code:
RBATTE1> sh -x abc.sh          
+ ps -fp 2273350
     UID     PID    PPID   C    STIME    TTY  TIME CMD
 RBATTE1 2273350 1884412   0 13:01:50  pts/6  0:00 sh -x abc.sh 
+ grep sh -x abc.s[h]
+ ps -ef
 RBATTE1 2273350 1884412   0 13:01:50  pts/6  0:00 sh -x abc.sh 
+ + ps -ef
+ grep -c sh -x abc.s[h]
check=1
+ echo 1
1
+ sleep 180

.... and on HP-UX it's the same:-
Code:
RBATTE1> sh -x abc.sh
+ ps -fp 24145
     UID   PID  PPID  C    STIME TTY       TIME COMMAND
 RBATTE1 24145 24072  1 13:10:13 pts/tk    0:00 sh -x abc.sh
+ ps -ef
+ grep sh -x abc.s[h]
 RBATTE1 24145 24072  1 13:10:13 pts/tk    0:00 sh -x abc.sh
+ + ps -ef
+ grep -c sh -x abc.s[h]
check=1
+ echo 1
1
+ sleep 180


What OS are you using.



Thanks again,
Robin
# 6  
Old 07-04-2013
I am using

Code:
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.3 Beta (Tikanga)

below is the o/p you requested after inserting those two lines

Code:
$ sh -x abc.sh
+ ps -fp 18853
UID        PID  PPID  C STIME TTY          TIME CMD
sam     18853  5608  0 08:12 pts/13   00:00:00 sh -x abc.sh
+ ps -ef
+ grep 'sh -x abc.s[h]'
sam     18853  5608  0 08:12 pts/13   00:00:00 sh -x abc.sh
++ ps -ef
++ grep -wc 'sh -x abc.s[h]'
+ check=2
+ echo 2
2
+ sleep 180

# 7  
Old 07-04-2013
Funny enough - I placed a tee into the pipe, and - there are two processes! See:
Code:
check=$(ps -ef|tee xx|grep -c "[s][h] -x abc.sh")
less xx
...
userx   6061  4983  0 17:44 pts/2    00:00:00 bash -x abc.sh
userx   6062  6061  0 17:44 pts/2    00:00:00 bash -x abc.sh

one being an immediate subprocess of the other. Strange!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Telnet and get process count

Hi guys, I'm writing a script on a Solaris 10 server (server A) that needs to telnet 2 servers (server B & server C) and get a certain process count from these 2 servers. Then on server A, I check if both counts are greater than 17, I do a sendmail to concerned people. For the telnet part,... (7 Replies)
Discussion started by: frum
7 Replies

2. Shell Programming and Scripting

Process a file for line count using for loop in awk

Hi, I have a file with contents So what I have to do is In short, break the file after every 6 lines and then truncate new line to tab for these 6 lines. I am not able to execute the for loop in awk properly. The idea is something like this: less file| awk '{for .... {if ((nr>=1)... (7 Replies)
Discussion started by: rossi
7 Replies

3. AIX

Check process count

Hi, We have scheduled few jobs/scripts in cron and it runs on daily basis at a specified time. We have a cron jobs has various parameters for main scripts like /home/test/mainscript.ksh main_param when the above jobs triggers, mainscript.ksh has below code and it refers ... (3 Replies)
Discussion started by: Selva_2507
3 Replies

4. 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

5. Shell Programming and Scripting

Using backspace in echo to show process count

i have something like this tablesName="abc def hij akn ... etc etc" count=0 for i in $tablesName do echo -en "\b\b\b\b\b\b\b\b\b\b\b\b\bTableCount: $count" count=`expr $count + 1` done the above is just a description wha i need is when the loop executes the... (1 Reply)
Discussion started by: vivek d r
1 Replies

6. UNIX for Dummies Questions & Answers

Thread count for a process id

Hi, I want to know a command/program to get thread count for a process id in unix box. Please help me in this regard. (1 Reply)
Discussion started by: manaac
1 Replies

7. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

8. Shell Programming and Scripting

facing problem in starting a process in background using shell script.

hey all, i am working on sun solaris machine and i want to start a process in background using shell script (actually i wanna start tomcat server using shell script). please dont tell me that append a & at last because this is not working in the shell script. i have also used nohup and... (8 Replies)
Discussion started by: dtomar
8 Replies

9. HP-UX

how to see the threads count of a process in hp unix?

hi,all: how to see the threads count of a process in hp unix? thanks (2 Replies)
Discussion started by: bugbugbug
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