LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2009, 02:40 AM   #1
bobgosling
LQ Newbie
 
Registered: Jul 2009
Location: Singapore
Posts: 6

Rep: Reputation: 1
Question /bin/sh what am I doing wrong - trying to check if already running


I am trying to check ( in a generic way ) that the shell that has just been started is not already running. I am getting bizarre counts of 3 or even 4 for the number of processes already running. I expect to see only 1 if it is just this instance or 2 if already running. In fact since adding the "grep -v ${thispid}" I expected to see 0 or 1 !!!

The sleep is so I can launch a test with "&" to background it then try to launch again. I think it has something to do with being in a sub-shell because of using pipes within "`" pre-execution quotes but I'm losing vast amounts of hair trying to figure out what I've done wrong. Any bright ideas ? Or any suggestions for a better/easier way of doing this ?

#!/bin/sh
#
# Test how to check whether already running
#
export thispid="$$"
echo "This PID = ${thispid}"
export thisshell="$0"
export thisproc="`basename ${thisshell}`"
echo "This proc = ${thisproc}"
export thisuser="`whoami`"
echo "This user = ${thisuser}"
txt="`ps -w -u ${thisuser} -O args | grep ${thisproc} | grep -v ${thispid} | grep -v grep`"
echo "${txt}"
counter="`ps -w -u ${thisuser} -O args | grep ${thisproc} | grep -v ${thispid} | grep -v grep | wc -l`"
echo "Count = ${counter}"
sleep 10m
exit
 
Old 08-18-2009, 02:55 AM   #2
bobgosling
LQ Newbie
 
Registered: Jul 2009
Location: Singapore
Posts: 6

Original Poster
Rep: Reputation: 1
sorry, example shell no use without example output

Here's some annotated example output from my shell.

bobs_test.sh
This PID = 31994
This proc = bobs_test.sh
This user = martini
31997 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
Count = 1
<<<<<<<<<<<< I CTL-Ced it here. Output as expected ( though why 1 instead of 0 ) >>>>>>>>>
[martini@dbvdev4108 Bin]$
[martini@dbvdev4108 Bin]$ !! &
bobs_test.sh &
[1] 32024
[martini@dbvdev4108 Bin]$ This PID = 32024
This proc = bobs_test.sh
This user = martini
32027 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
Count = 1

<<<<<<<<<<<< Backgrounded, again output as expected >>>>>>>>>

[martini@dbvdev4108 Bin]$ bobs_test.sh
This PID = 32039
This proc = bobs_test.sh
This user = martini
32024 /bin/sh ./bobs_t S pts/8 00:00:00 /bin/sh ./bobs_test.sh
32042 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
32045 [bobs_test.sh] R pts/8 00:00:00 [bobs_test.sh]
32046 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
Count = 3

<<<<<<<<<<<< WTF?!?! Previous still running + this one should = 2 >>>>>>>

[martini@dbvdev4108 Bin]$ <<<<<<<<< CTRL-C and try again >>>>>>>>>>>>
[martini@dbvdev4108 Bin]$ !!
bobs_test.sh
This PID = 32069
This proc = bobs_test.sh
This user = martini
32024 /bin/sh ./bobs_t S pts/8 00:00:00 /bin/sh ./bobs_test.sh
32072 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
Count = 2

<<<<<< Whaaaa ! Why 2 when it was 3 last run ? >>>>>>>>>>>

[martini@dbvdev4108 Bin]$ <<<<< CRTL-C a few more times and see if it happens again >>>>>>>>>>>
[martini@dbvdev4108 Bin]$ !!
bobs_test.sh
This PID = 32084
This proc = bobs_test.sh
This user = martini
32024 /bin/sh ./bobs_t S pts/8 00:00:00 /bin/sh ./bobs_test.sh
32087 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
Count = 2

[martini@dbvdev4108 Bin]$
[martini@dbvdev4108 Bin]$ !!
bobs_test.sh
This PID = 32099
This proc = bobs_test.sh
This user = martini
32024 /bin/sh ./bobs_t S pts/8 00:00:00 /bin/sh ./bobs_test.sh
32102 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
Count = 2

[martini@dbvdev4108 Bin]$
[martini@dbvdev4108 Bin]$ !!
bobs_test.sh
This PID = 32114
This proc = bobs_test.sh
This user = martini
32024 /bin/sh ./bobs_t S pts/8 00:00:00 /bin/sh ./bobs_test.sh
32117 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
32121 /bin/sh ./bobs_t R pts/8 00:00:00 /bin/sh ./bobs_test.sh
Count = 3

<<<<<<<<<<<<< No !!! Not again ? My Ox is flummed and my ghast is flabbered >>>>>>>>

N.B. I did check the obvious : Nobody else is logged in trying my test script .
 
Old 08-19-2009, 02:16 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

I think it's a problem of the variuos subshells (forks) you are creating be using "`...`" command substitution and pipes (/bin/sh is bash on your system?). The "-w" output format of ps does not show the PPID (parent process id), so you don't filter children out with "grep -v ${thispid}". Try the "-f" option instead.

You should check, if the pidof command is available on your system, it makes your work much easier ;-)



Jan
 
Old 08-19-2009, 11:46 PM   #4
bobgosling
LQ Newbie
 
Registered: Jul 2009
Location: Singapore
Posts: 6

Original Poster
Rep: Reputation: 1
Thanks

Danke !

pidof isn't available but grepping out child processes solved my problem.

( /bin/sh was an attempt to see if it still went wrong in another shell )
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
check bin or package dependencies? ldeveloperxl Linux - Software 4 11-10-2007 03:08 AM
please check for me the error and tell me~~ thx! i know is wrong posting here but.... anzdyy Slackware 1 09-11-2007 12:52 PM
Fedora check sume is wrong procfs Linux - General 4 05-30-2006 11:05 PM
path in services wrong for clamav updated frm 0.75 to 0.80 usr/bin vs usr/local/bin Emmanuel_uk Linux - Newbie 3 04-22-2005 01:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration