Wednesday, January 19, 2022

#1 2021-12-03 08:51:38 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 76
Website

Need a "do shell script" which includes a pipe to run in background

I have a "do shell script" call which is to run in background and which involves piping output from one command to another.  I also need the call to return the PiD.  The first command outputs text while the second, a Perl script called "ts", adds timestamps to each line then saves in a file.  The call is in this form:

Applescript:

set output_file to "~/output.txt"
set shell_pid to do shell script " <command> 2>&1 | /usr/local/bin/ts &> " & output_file & " & echo $!"

where <command> is a Python script that can take a long time to run.  While <command> is running, my AppleScript uses the PiD to monitor and report on progress.

But, I can't get it to run in background.  The call always waits for the <command> to finish before continuing with the AppleScript.  If I remove the pipe to "ts", the call works properly by running the <command> in background and continuing on with the AppleScript.  But, then, I lose the timestamps.

Is there a way to run a "do shell script" call in background when it contains a "|" pipe ?

In fact, also, I can't get the call to run in background if it contains a list using ";" separators e.g.:

Applescript:

set output_file to "~/output.txt"
set shell_pid to do shell script " sleep 5 ; ls > " & output_file & " & echo $!"

AppleScript always waits the 5 seconds or so before returning the PiD.

Last edited by Neophyte (2021-12-03 08:52:05 pm)


Public servants are people too

iMac (late 2009), 27", 2.66GHz i5, 12GB, 1TB SSD, HD4850
macOS Catalina, 10.15.7


Filed under: shell

Offline

 

#2 2021-12-04 01:08:28 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 172

Re: Need a "do shell script" which includes a pipe to run in background

Is there a way to run a "do shell script" call in background when it contains a "|" pipe ?



Dunno if this exactly matches your case but per TN2065 under 'My script will produce output over a long time. How do I read the results as they come in?':

In Unix terms, it cannot be used to create a pipe.




https://developer.apple.com/library/arc … Y_COME_IN_

Offline

 

#3 2021-12-04 08:05:13 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: Need a "do shell script" which includes a pipe to run in background

Well, yes, it is obvious that if you block the road with a cement block (2>&1) and after the block you put a huge billboard (pipe) with the inscription "Welcome! We are so hospitable...", the traveler will see this billboard, but will not be able to pass.

In your case, to solve the problem, the timestamp should be transferred not from the previous command, but by reading in an endless repeat  the next timestamp from a file into which the first command writes all the time. Exit the repeat loop - as soon as the dies stop coming in.

That is, direct the timestamps output of your first command to some temporary file, and with the second command read timestamps from it.

Applescript:

do shell script "<command> &> " & tempFile_path & " &|/usr/local/bin/ts &> " & output_file & " & echo $!"

Last edited by KniazidisR (2021-12-04 08:36:57 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2021-12-10 06:01:27 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 76
Website

Re: Need a "do shell script" which includes a pipe to run in background

KniazidisR, many thanks.  I'll give that a try.


Public servants are people too

iMac (late 2009), 27", 2.66GHz i5, 12GB, 1TB SSD, HD4850
macOS Catalina, 10.15.7

Offline

 

#5 2021-12-18 12:44:36 am

technomorph
Member
Registered: 2017-12-14
Posts: 242

Re: Need a "do shell script" which includes a pipe to run in background

If you want the results as a variable check out Shane’s script

https://forum.latenightsw.com/t/applesc … cript/2069

Offline

 

#6 2021-12-30 06:08:44 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 76
Website

Re: Need a "do shell script" which includes a pipe to run in background

KniazidisR wrote:

do shell script "<command> &> " & tempFile_path & " &|/usr/local/bin/ts &> " & output_file & " & echo $!

I can't get that suggestion to work.  I got a syntax error until I deleted the "&" from before the "|".  This is my code:

Applescript:

set shell_pid to do shell script "ls &> ~/temp.txt | /usr/local/bin/ts &> ~/output.txt & echo $!"

The code runs but the output.txt file is empty.

As a test, I ran the code in Terminal:

ls &> ~/temp.txt | /usr/local/bin/ts &> ~/output.txt & echo $!

It worked in Terminal i.e. "zsh", but when I ran it in sh, I got an empty output.txt file. I guess that means that sh handles pipes differently.

UPDATE: I'm looking at alternatives which don't require piping e.g. ets.

Last edited by Neophyte (2021-12-30 08:24:34 pm)


Public servants are people too

iMac (late 2009), 27", 2.66GHz i5, 12GB, 1TB SSD, HD4850
macOS Catalina, 10.15.7

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)