Re: Is it possible to multi-thread do shell script?
Re: Is it possible to multi-thread do shell script?
- Subject: Re: Is it possible to multi-thread do shell script?
- From: Axel Luttgens <email@hidden>
- Date: Tue, 8 Dec 2009 18:04:09 +0100
Le 8 déc. 2009 à 04:39, Patrik a écrit :
> [...]
>
> Hi Axel,
>
> Thanks for your help. I am not too familiar with shell scripting - so I am not sure what "&>/tmp/pdftk.log &" does. As I had to combine pdfs. I did the tests with another command line tool called pdfmeld today which I wanted to also multi-thread.
>
> Does "&" at the end of a statement not do the trick of detaching the process?
The ending "&" just ask not to wait for the preceding command to finish, and thus to immediately skip to the execution of the next command, if any.
No more.
So, let's have a look at following script:
do shell script "/bin/sleep 10 &"
beep 1
It appears that it doesn't immediately proceed with the beep, but instead waits for the end of the sleep command.
That's because another *general* mechanism is at work: do shell script has provided the command (here, the sleep command) with standard communication channels, so as to be able to get some feedback from that command. And do shell script will wait until those channels are closed on the "other side" - in this case when sleep terminates.
To avoid this, one has to explicitly disable that mechanism, for example by making use of regular files:
do shell script "/bin/sleep 10 >/tmp/sleep_stdout 2>/tmp/sleep_stderr &"
beep 1
Here, do shell script "knows" it doesn't have to worry about the sleep command anymore -because it has been told not to wait for the command's completion nor to watch its possible output- and the script immediately continues with the next line.
If it doesn't matter to have both outputs intermingled, the above may be simplified as:
do shell script "/bin/sleep 10 &>/tmp/sleep_stdboth &"
beep 1
and if no useful output is to be expected at all, one may even make use of the null device instead of creating un-needed files:
do shell script "/bin/sleep 10 &>/dev/null &"
beep 1
As a result, this one will launch two sleep processes in the background and beep immediately:
do shell script "/bin/sleep 10 &>/dev/null &"
do shell script "/bin/sleep 10 &>/dev/null &"
beep 1
as well as this one:
do shell script "/bin/sleep 10 &>/dev/null & /bin/sleep 10 &>/dev/null &"
beep 1
or even this one:
do shell script "(/bin/sleep 10 & /bin/sleep 10 &) &>/dev/null"
beep 1
So, if pdfmeld (or pdftk) behaves in a "standard" way, it is likely that it fits the general framework described above; in which case you just need to ensure to redirect stdout and stderr, and to make use of the "&" control operator.
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden