Friday, October 31, 2014

#1 2009-06-05 04:53:54 am

MRoig
Member
Registered: 2008-04-27
Posts: 111

Running multiple instances of shell script?

Is it possible to run multiple instances of a shell script simultaneously from within one applescript? I know applescript is essentially not multi-threaded. When you run a shell script from within an applescript, the applescript waits for the shell-script to finish before continuing. But I was wondering if there is some sort of hack around this.

Offline

 

#2 2009-06-07 12:28:45 am

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: Running multiple instances of shell script?

You can do it like the following, but with this approach you won't directly get any output from the command. To get output you would have to write it to a file. Then in your script you could check for the PID of the process. WHen you can no longer find the PID you know that the process is finished then could could read the contents of the file to get the output.

Applescript:

-- AppleScript's do shell script holds the process' stdout and stderr hooks waiting for the output of the command. That's why it appears to hang - it's doing what it's designed to do.

-- The solution is simple - you redirect stderr and stdout to some other location and send the shell process to the background
-- the > /dev/null redirects the process' stdout to /dev/null (you can use some other file path if you want to capture its output). 2>&1 redirects stderr to the same place as stdout (in this case, /dev/null) and the trailing & sends the process to the background.

-- You'll find that this effectively returns control back to your script while the shell process runs in the background

-- add "echo $!" to the end of the command to have the PID of the spawned process returned to applescript

do shell script "/bin/sleep 5 > /dev/null 2>&1 &"
beep

-- to get the pid of the process use "echo $1"
do shell script "/bin/sleep 5 > /dev/null 2>&1 & echo $!"
set the_pid to the result

Offline

 

#3 2009-06-07 05:30:29 pm

MRoig
Member
Registered: 2008-04-27
Posts: 111

Re: Running multiple instances of shell script?

Thanks, that seems to work.

How would you normally check for the PID? I tried with system events, but without luck.

A shell script that someone posted here doesn't work that well. Rather than returning "false" if the process isn't running anymore it creates an error. So for now, I'm just using a try-block.

Applescript:

try
   set processExists to ((count of paragraphs in (do shell script "ps -p " & the_PID)) > 1)
on error
   set processExists to false
end try


Filed under: shell, process, ID

Offline

 

#4 2009-06-07 05:46:35 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: Running multiple instances of shell script?

You're welcome. I use this code...

Applescript:

set thePID to do shell script "/bin/sleep 2 > /dev/null 2>&1 & echo $!"
set pidSearch to do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"

-- If pidSearch is "" then the process is finished.
-- If pidSearch is the pid number then the process is still running.
if pidSearch is "" then
   return "process is finished"
else if pidSearch is thePID then
   return "process is still running"
else
   return "error"
end if

Offline

 

#5 2009-06-08 03:36:50 pm

MRoig
Member
Registered: 2008-04-27
Posts: 111

Re: Running multiple instances of shell script?

Thanks. That works a treat. No idea why though, one of these days I will have to learn shell scripting.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.034 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)