Sandeep T D S
Sandeep T D S

Reputation: 527

How to run multiple shell scripts from applescript in background

I am launching an a jar application from apple script.

do shell script quoted form of jvmpath & " -jar -XstartOnFirstThread -Dapple.awt.UIElement=true -Dfile.encoding=UTF8 " & quoted form of jarpath & " " & quoted form of parameters

The script keeps running till i quit my jar application.

But i am required to launch another application form shell script.

Since i am doing this in a Cocoa app i want to do this in the background.

Thus, can i launch multiple scripts in multiple instances of terminal (so that they aren't blocking one another).

Note: I tested it by running the command in two different terminal windows, works as expected.

Upvotes: 1

Views: 2414

Answers (1)

Jim Matthews
Jim Matthews

Reputation: 1191

See Technical Note TN2065, specifically the answers to the questions "I want to start a background server process; how do I make do shell script not wait until the command completes?" and "I have started a background process; how do I get its process ID so I can control it with other shell commands?".

The AppleScript code to run two commands in the background would look like this:

set pid1 to do shell script command1 & " &> /dev/null & echo $!"
set pid2 to do shell script command2 & " &> /dev/null & echo $!"

The pid1 and pid2 variables will be set to the process ids of the two commands. You can later check whether the commands are still running by calling a function like this one:

on isProcessRunning(pid)
    try
        do shell script "kill -0 " & pid
        set isRunning to true
    on error
        set isRunning to false
    end try
    return isRunning
end isProcessRunning

Upvotes: 2

Related Questions