Dear MacScripter Mac OX List folks,
Thanks to all for the very useful info posted to this list. I’m usually able to answer my questions with a quick query to the archive.
This one has me stumped, and I couldn’t find anything in the archive.
I need to be able to use the Unix ‘ps’ command to grab the pid for a specific system process from within a large, compiled AppleScript I’m running.
The calls I started with on the Unix command line are:
[n2-129-789:~] dingus% ps xa -o pid,command | grep myProcess | grep -v grep | cut -d"/" -f1
234
[n2-129-789:~] dingus%
This returns the number of the process named “myProcess” on the command line.
I can call this via ‘do shell script’, wrap it in a call to osascript and run it again on the command line with the same results:
[n2-129-789:~] dingus% osascript -e 'do shell script "ps xa -o pid,command | grep myProcess | grep -v grep | cut -d"/" -f1"'
234
[n2-129-789:~] dingus%
The only change I needed to make was to escape the quotes in the delimiter parameter passed to the Unix ‘cut’ command, put the collection of piped Unix statements in double quotes and put everything in single quotes before passing it as a parameter to the ‘osascript’ command.
Now I come to my problem.
I wanted to run this collection of piped commands from within a compiled AppleScript. Here’s the code:
set theProcName to "myProcess"
set theCmd to "ps xa -o pid,command | grep " & theProcName & " | grep -v grep | cut -d"/" -f1"
set thePID to ((do shell script theCmd) as number)
Unfortunately, when I do this, no matter how I construct the command, the ‘ps’ command is always the last to be evaluated of my 4 Unix shell commands. Obviously, this completely screws up what I’m trying to do here, as the remaining 3 commands are just there to parse the output of the ‘ps’ command and grab the PID I’m looking for.
Normally, it’s no problem running a collect piped Unix shell commands in this manner and get them to run in the proper order. I’ve done it several times in scripts I use on a regular basis. This seems to be particular to the ‘ps’ command - or at least I’ve only discovered it to be the case so far for the ‘ps’ command.
I’ve tried several other ‘do shell script’ methods for achieving the same means - like running the same commands from within PERL or RUBY scripts. These all work from the command line and all give the same error from within my script.
Any help you can offer, I’d greatly appreciate.
Many thanks!
Cheers,
Bill Bug