Using open process instead of shell

Mike Bonner bonnmike at gmail.com
Sun Dec 24 21:06:08 EST 2017


IGnore the "with message" part, had a brain freeze. It would be -- open
process pdata for text read
And then do the -- send "monitorslave pData' to me in 50 millisec -- to
start the loop.

Just tested on linux.  I can start a find process, grab the output of dmesg
and cat a file and direct output to different fields.  But the processes
don't auto close on completion, so you'd have to have a way to recognize
when each finishes based on their output, and close the process. (or if you
don't care about the output, you can
open process tProcess for neither
Which should start a disconnected process that will exit when complete
(assuming its a command that exits on completion)

Another possible method that should make it easy to see when such a process
ends (if you DO want to read the output) would be to open a shell process
for text update.  Then write your command to that shell,  and read the
output.  When it returns to a prompt, its done.

Example code (not using a shell) follows:

local tstarttime
on mouseup

   put empty into field 1
   put empty into field 2
   put empty into field 3

   put the seconds into tstarttime
-- start 3 processes

   put "dmesg" into tProcess1
   put "find /" into tProcess2
   put "cat /etc/fstab" into tProcess3

   open process tProcess1 for text read
   monitorslave tProcess1

   open process tProcess2 for text read
   monitorslave tProcess2

   open process tProcess3 for text read
   monitorslave tProcess3
end mouseup

command monitorSlave pProcess

switch

   case pProcess contains "dmesg"
      read from process pProcess until empty
      put it after field 1
   break

   case pProcess contains "find"
      read from process pProcess until empty
      put it after field 2
   break

   case pProcess contains "cat"
      read from process pProcess until empty
      put it after field 3
   break

end switch

if pProcess is not among the lines of openprocesses() then
     exit "monitorslave"
else
     send "monitorslave pProcess" to me in 100 millisec
end if

-- quick and dirty method of stopping the processes after a minute
-- since this is only testing
if the seconds - tstarttime > 60 then closeProcesses

end monitorSlave

command closeprocesses

   repeat for each line tline in openprocesses()
      close process tLine
   end repeat

end closeprocesses



On Sun, Dec 24, 2017 at 5:56 PM, Mike Bonner <bonnmike at gmail.com> wrote:

> Off the top of my head, untested (no mac available)
>
> on executeProcess pData
>     put whereAmI() into a; set the defaultFolder to a
> -- I'd bipass this
> --    get "file:xprocess.bat"
> --    put pData into URL it
>
>
>     open process  "file:xprocess.bat"  for text write
> -- this part, if you want to use a shell file (bat?) you a) have to make
> sure its set to executable
> -- b) You'd want to prepend #!/bin/sh so that it knows what shell to use
> on execution (or use sh xprocess.bat as your command)
> -- c ) Don't think it will work using the URL form  It should be -- open
> process "xprocess.bat" for text read
> -- text read because you want to read the output (or update if you need to
> send additional commands interactive)
>
> --Since you want to be able to check progress you'll need to start a read
> loop too..
> -- if it were me, i'd just call openprocess with your known working
> command line
> open process pData for text read with message
>
> -- start read loop
> send "monitorslave pdata" to me in 50 millisec
>
> close executeProcess
>
>
> command monitorslave pProcess
> -- read loop.. DO what you need to check progress here..
> read from process pProcess until empty
>
> -- Somewhere in here you'll need to determine if the process is done,
> close it, and exit without looping again
> -- or update status and loop again
>
>     send monitorslave pProcess to me in 50 millisec -- loop again if not
> done
> end monitorslave
>
>
> if you want to use the same slave loop for multiple processes, you'll of
> course have to parse the url to know how to handle the output
> (Or just make a custom slave loop for each of your 3 executables)
> I also don't know if you will actually need to close the process, or if it
> will self close when done.  As mark pointed out, you can look at
> openprocesses() to see whats currently running.
>
>
>
> On Sun, Dec 24, 2017 at 4:30 PM, Mark Wieder via use-livecode <
> use-livecode at lists.runrev.com> wrote:
>
>> On 12/24/2017 02:05 PM, Stephen Barncard via use-livecode wrote:
>>
>> ​ Does someone out there have a clear example of what I am looking for,
>>> using a UNIX  executable  through *open process​ *?
>>>
>>> 1.    call the function  with parameters
>>> 2.     allow livecode scripts to continue
>>> 3.     continuously monitor and display data returned by that function
>>>
>>>
>>>   thank you all so much for any and all information,
>>>
>>
>> check out the openProcesses function to see what's still in play
>> here's an example from the 'close process' doc:
>>
>> close process myProcess
>> wait until myProcess is not among the lines of the openProcesses
>> open process myProcess
>>
>>
>> --
>>  Mark Wieder
>>  ahsoftware at gmail.com
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>
>



More information about the use-livecode mailing list