The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   Applescript and Unix commands (check if process exists) (http://hintsforums.macworld.com/showthread.php?t=34109)

atFault 01-31-2005 07:10 PM

Now to recapture my hijacked thread and post my solution.

I took the "grep -v grep" off of the set "cmdOutput to do shell script "ps ax | grep server-bin"" line so that I would get both the grep of the grep and the grep of the server-bin thereby eliminating the NULL condition.

Code:

24931  p1  R+    27:40.28 ./server-bin global sfcsar.aao
25159 std  R+    0:00.00 grep server-bin

I then modified 'if cmdOutput does not contain "sfcsar" then" to query for a different, but unique return from the "ps ax | grep server-bin" that would not be present if "server-bin" wasn't running. It turns out the NULL condition was the cause of my 'A type 1 error occured" message and this solved the problem.

My final script is this:
Code:

on idle
        set cmdOutput to do shell script "ps ax | grep server-bin"
        if cmdOutput does not contain "sfcsar" then
                tell application "Terminal"
                        activate
                        do script with command "cd //Applications/Army\\ Operations\\ 2.1.0.app/System; ./server-bin global sfcsar.aao"
                end tell
        end if
        return 60
end idle

So I figured it out on my own, but there is no way I would have put it all together without you guys, thanks for the pointers and info. It even sounds like the hijackers got something from my little problem. I don't know how 'clean' my script is or how appropriate it is to make it work like this, but it does work and is making many people happy.

Ciao, and thanks again

atFault

jkenney 02-01-2005 12:33 AM

Just a helpfull pointer...

ps -axc to take out the path name of the application

for example:

Code:

[:~] jkenney% ps -ax | grep init
    1  ??  Ss    0:00.01 /sbin/init -v
    2  ??  Ss    0:02.16 /sbin/mach_init -v
20570 std  R+    0:00.00 grep init
[:~] jkenney% ps -axc | grep init
    1  ??  Ss    0:00.01 init
    2  ??  Ss    0:02.16 mach_init
[:~] jkenney%

See the difference... type man ps to find more ps fun :)

bcjenkins 05-26-2008 09:59 AM

Thought I would dust this thread off and post my solution in case it is helpful to anyone else.

Code:

set isRunningComskip to do shell script "ps ax | grep comskip.exe | grep -v grep | wc -l | cut -d ' ' -f8"
The wc command will return 0 if it is not running. And in my case, I want to allow more than one instance running of comskip.exe under wine, so this serves two purposes. It tells me if it is running, and how many instances.

B

Hal Itosis 05-27-2008 03:00 AM

As jkenney pointed out (though perhaps not emphatically enough),
using the 'c' option with ps not only trims off the pathname... but,
ALSO totally obviates the need for the extra |grep -v grep !

If someone can explain *why* that actually works, I'm all ears.
[seems like grep should still find the same string it did before?]

--

Another trick -- which works without the 'c' option -- is to prefix a [/]
bracket expression onto the searched name. So grep must then match
a leading / forward slash, right before the string. Thus -- for example,
to see if Safari is running -- this will do the trick:
Code:

if [[ $(ps ax |grep [/]Safari) ]]
then
        # Safari is running...

else
        # Safari NOT running...

fi

§

Quote:

Originally Posted by bcjenkins (Post 472182)
Thought I would dust this thread off and post my solution in case it is helpful to anyone else.
Code:

set isRunningComskip to do shell script "ps ax |
grep comskip.exe | grep -v grep | wc -l | cut -d ' ' -f8"

The wc command will return 0 if it is not running. And in my case, I want to allow more than one instance running of comskip.exe under wine, so this serves two purposes. It tells me if it is running, and how many instances.

I will also briefly mention that that final pipe into "|cut -d ' ' -f8" is pretty much not needed.
Yes... wc does stick a bunch of spaces in there, for some odd reason.
But the shell sees spaces as nothing but separation: IFS=$' /t/n'
So... it can still do math, and "if" comparisons, etc., just fine.
For most purposes, simply don't quote the (integer) variable... and the spaces go away!
E.g.:
if [ $isRunningComskip -gt 0 ]
or
(( $isRunningComskip+=1 ))
or
echo $isRunningComskip
all work fine. (the spaces are simply ignored/discarded)

-HI-

hayne 05-27-2008 10:18 AM

Quote:

Originally Posted by Hal Itosis (Post 472379)
using the 'c' option with ps not only trims off the pathname... but,
ALSO totally obviates the need for the extra |grep -v grep !

If someone can explain *why* that actually works, I'm all ears.
[seems like grep should still find the same string it did before?]

Here's an example that should lead you to the path of understanding:
Code:

% ps -ax | grep grep
19539 ttys000    0:00.00 grep grep

% ps -axc | grep grep
19541 ttys000    0:00.00 grep


bcjenkins 05-27-2008 03:18 PM

Quote:

Originally Posted by Hal Itosis (Post 472379)
As jkenney pointed out (though perhaps not emphatically enough),
using the 'c' option with ps not only trims off the pathname... but,
ALSO totally obviates the need for the extra |grep -v grep !

If someone can explain *why* that actually works, I'm all ears.
[seems like grep should still find the same string it did before?]

--

Another trick -- which works without the 'c' option -- is to prefix a [/]
bracket expression onto the searched name. So grep must then match
a leading / forward slash, right before the string. Thus -- for example,
to see if Safari is running -- this will do the trick:
Code:

if [[ $(ps ax |grep [/]Safari) ]]
then
        # Safari is running...

else
        # Safari NOT running...

fi

§



I will also briefly mention that that final pipe into "|cut -d ' ' -f8" is pretty much not needed.
Yes... wc does stick a bunch of spaces in there, for some odd reason.
But the shell sees spaces as nothing but separation: IFS=$' /t/n'
So... it can still do math, and "if" comparisons, etc., just fine.
For most purposes, simply don't quote the (integer) variable... and the spaces go away!
E.g.:
if [ $isRunningComskip -gt 0 ]
or
(( $isRunningComskip+=1 ))
or
echo $isRunningComskip
all work fine. (the spaces are simply ignored/discarded)

-HI-

Ahh, thanks for that tip on cut. I may give that a whirl, although I wrapped my package already. For the record I had to use
Code:

set isRunningComskip to do shell script "ps ax |
grep comskip.exe | grep -v grep | wc -l | cut -d ' ' -f8"

When using c, it captured I was running bash, but not the name of the bash script.

B

Hal Itosis 05-27-2008 11:14 PM

Quote:

Originally Posted by hayne (Post 472456)
Here's an example that should lead you to the path of understanding:
Code:

% ps -ax | grep grep
19539 ttys000    0:00.00 grep grep

% ps -axc | grep grep
19541 ttys000    0:00.00 grep


Not really. In fact... that makes it more mysterious. :D
[i can see it does work... but not how/why it works.]

Try this:
$ ps -ax | grep Safari
88880 ?? 0:39.45 /Applications/Safari.app/Contents/MacOS/Safari -psn_0_454767
94274 ttys000 0:00.00 grep Safari

$ ps -acx | grep Safari
88880 ?? 0:39.45 Safari

Now... why doesn't the 2nd grep *also* see itself (grepping Safari)?
The text being matched is simply "Safari"... so, i don't get it.

Hal Itosis 05-27-2008 11:24 PM

Quote:

Originally Posted by bcjenkins (Post 472515)
Ahh, thanks for that tip on cut. I may give that a whirl, although I wrapped my package already. For the record I had to use
Code:

set isRunningComskip to do shell script "ps ax |
grep comskip.exe | grep -v grep | wc -l | cut -d ' ' -f8"

When using c, it captured I was running bash, but not the name of the bash script.

Perhaps I should have emphasized that those behaviors apply to Bash, and not AppleScript.

However, most of the examples in this thread seem to use AppleScript only to
either talk to the shell (do shell script) or Terminal (tell application "Terminal").
So --unless there's some use for it [not shown] -- a shell script should suffice.

But yes, to have AppleScript display that number as text, we'd need to strip
the spaces (if we cared about it looking perfect). I much prefer sed over cut:
Code:


--begin applescript
set theApp to "Safari"
set isRunning to do shell script "ps ax |
        grep [/]" & theApp & " |wc -l |sed 's/ //g'"

if isRunning > 0 then -- !!! need to have removed wc's spaces, or that ">" won't work
        display dialog "the count is " & isRunning
end if


hayne 05-28-2008 12:42 AM

Quote:

Originally Posted by Hal Itosis (Post 472648)
Now... why doesn't the 2nd grep *also* see itself (grepping Safari)?
The text being matched is simply "Safari"... so, i don't get it.

Look again at the 'grep grep' example I showed above.
The output from the 'ps -axc' case shows that the line in the output from the 'ps' command with those options is simply "19541 ttys000 0:00.00 grep"
- there is no "grep grep" in the 'ps' output.

Similarly when you 'grep Safari', there is no "Safari" in the line from 'ps' for that process since the "-c" option does not include the command's arguments in the output.

Or here's another way to understand:
Run a shell script that loops, running a command like 'grep foo /etc/passwd'
and then look at the full output from 'ps -xc' with something like:
ps -axc | more

Hal Itosis 05-28-2008 12:55 AM

Quote:

Originally Posted by hayne (Post 472662)
Similarly when you 'grep Safari', there is no "Safari" in the line from 'ps' for that process since the "-c" option does not include the command's arguments in the output.

No arguments!
*NOW* I get it.

[duh :o ]

Thanks C.H.

bcjenkins 06-04-2008 09:14 AM

Quote:

Originally Posted by Hal Itosis (Post 472649)
Perhaps I should have emphasized that those behaviors apply to Bash, and not AppleScript.

However, most of the examples in this thread seem to use AppleScript only to
either talk to the shell (do shell script) or Terminal (tell application "Terminal").
So --unless there's some use for it [not shown] -- a shell script should suffice.

But yes, to have AppleScript display that number as text, we'd need to strip
the spaces (if we cared about it looking perfect). I much prefer sed over cut:
Code:


--begin applescript
set theApp to "Safari"
set isRunning to do shell script "ps ax |
        grep [/]" & theApp & " |wc -l |sed 's/ //g'"

if isRunning > 0 then -- !!! need to have removed wc's spaces, or that ">" won't work
        display dialog "the count is " & isRunning
end if


I had a friend explain sed to me the other day. It is much cleaner and doesn't suffer from the limitation I found in cut. If the # of processes is 10 or higher, the field changes to 7 from 8.

B


All times are GMT -5. The time now is 10:21 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.