[Gambas-user] Monitoring a process... Need help
nando
nando_f at ...951...
Mon Feb 4 22:33:52 CET 2008
I may suggest the following:
There are 3 parts:
PART 1:
This is to do your script in the background so your Gambas program still responds...
a) Write all the shell script lines into one script file.
This file could be created dynamically in Gambas just before the script is to run
b) Make the last line in the script is the shell command to remove the directory
that Gambas makes just before the SHELL command (see below)
PART 2:
in Gambas..
TRY MKDIR unique directory 'this is our home-make signal that the script is running
SHELL "the script file &" 'this will start running in the background
Timer1.Delay = 1000
Timer1.Enable 'use Timer1 to see if that unique directory is exists of not.
'if it still exists, then the SHELL is still running.
'When Timer1 find the directory is gone, the script is done
'delete the file (in Gambas) if you choose a dynamic script.
'then you can continue on with what you want to do.
PART 3:
Inside TIMER1 code:
PUBLIC SUB TIMER1()
IF ISDIR (unique directory that was created) = TRUE then RETURN 'shell still working.
TIMER1.ENABLE=FALSE 'shell done, turn timer off
(Code here to do something with results from shell)
END
-Fernando
---------- Original Message -----------
From: "M0E Lnx" <m0e.lnx at ...626...>
To: "mailing list for gambas users" <gambas-user at lists.sourceforge.net>
Sent: Mon, 4 Feb 2008 12:40:53 -0600
Subject: Re: [Gambas-user] Monitoring a process... Need help
> Ok.. Been playing with this some more... an I can't help wondering....
> Why should this not work?
>
> MdlShellTest.Module
>
> PUBLIC hShellProc as process
> PUBLIC iStage as integer
> PUBLIC sBuffer
>
> PUBLIC FUNCTION sSysShell(sDir as string, iStep as integer)
> DIM sShellLine as string
>
> SELECT CASE iStep
> CASE 0
> sShellLine = "cd " & sDir & " || echo \'ERROR\'"
> CASE 1
> sShellLine = "ls -al " & sDir & " || echo \'ERROR\'"
> ' OTHER CASES HERE
> END SELECT
>
> hShellProc = SHELL sShellLine to sBuffer
> iStage = iStep ' Update the publically visible stage here.
> END
>
> PUBLIC SUB Process_kill()
>
> IF Instr(MdlShellTest.sBuffer, "ERROR") > 0 then
> ' Something went wrong during one of the shell commands
> STOP EVENT ' Not sure this does anything here
> ELSE
> SELECT CASE MdlShellTest.iStage
> CASE 0
> MdlShellTest.sSysShell(sDir, 1) ' Trigger the next command
> END SELECT
> END IF
> END
>
> Is there a reason something liek that wouldn't work?
>
> SELECT CASE MdlShellTest.iStage
>
> END
>
> On Feb 4, 2008 9:52 AM, M0E Lnx <m0e.lnx at ...626...> wrote:
> > Yes I know... the code is now all messed up... I've been thinking this
> > over and over...
> > But it seems to me that I *should* be able to define one process, and
> > run a bunch of shell commands via that one process.
> >
> > About your remarks... how do I tell it to trigger something when the
> > process is done running?
> > do I use the hShellProc_kill() event?...
> > And do you mean that if I do that, I dont need a timer?
> >
> > The reason I ask is because it is not yet very clear to me... I
> > thought you use the "Kill" to actually termintate a process, and it
> > was my understanding that a process would returned a "Process.Stopped"
> > property when it was done running...
> >
> > Please correct me if I'm wrong.
> >
> >
> >
> >
> > On Feb 4, 2008 9:46 AM, Benoit Minisini <gambas at ...1...> wrote:
> > >
> > > On lundi 4 février 2008, M0E Lnx wrote:
> > > > I'm trying to write a function to execute several shell commands in a
> > > > given order. For instance, extract a compressed tarball, and then cd
> > > > to it's extracted dir, and do "ls -al"
> > > >
> > > > Basically, what I want to do is execute several shell commands.
> > > > I want to use One process for all shell commands instead of using one
> > > > process for each command.
> > > > I want to use one timer to monitor this one process... and as soon as
> > > > it reports a Process.Stopped event, do some error checking, and if
> > > > passed, execute the second command using the same Process variable i
> > > > defined.
> > > >
> > > > The module file looks like this
> > > >
> > > > MdlShell
> > > > Public hShellProc as Process
> > > > Public iMdlStep as integer
> > > > PUBLIC FUNCTION Extract(sPath as string, iFuncStep)
> > > >
> > > > DIM ShellLine as string
> > > >
> > > > ' The following commands will be triggered from a timer event in another
> > > > form
> > > >
> > > > SELECT CASE iFuncStep
> > > > CASE 0 ' First step
> > > > ShellLine = "cd /tmp && tar xf " & sPath & " || echo \'ERROR\'"
> > > >
> > > > CASE 1 ' Second step
> > > > shellLine = "cd /tmp && ls -a || echo \'ERROR\'"
> > > >
> > > > ' And the list would continue here
> > > >
> > > > END SELECT
> > > >
> > > > hShellProc = shell ShellLine for read ' execute the selected command
> > > > iMdlStep = iFunctStep ' Make the last step available publically
> > > >
> > > > On a separate form, I have a timer setup.
> > > > On this timer setup... on it's timer event I have this
> > > >
> > > > TmShellMon_timer()
> > > >
> > > >
> > > > If MdlShell.hShellProc.State = Process.Stopped then
> > > > Me.TmShellMon.Enabled = false
> > > > ' Do some error checking here
> > > > if instr(sBuffer, "ERROR") > 0 then
> > > > STOP EVENT
> > > > Else
> > > >
> > > > ' If no error is found then execute this
> > > > SELECT CASE MdlShell.iMdlStep
> > > >
> > > > CASE 0 ' First step has ran... trigger next
> > > > MdlShell.Extract(sPath, 1)
> > > > CASE 1 ' Trigger 3rd step
> > > > MdlShell.Extract(sPath, 2)
> > > > ' And so on....
> > > > END SELECT
> > > > END IF
> > > > END IF
> > > >
> > > > The problem With this is... for some reason it's not monitoring the
> > > > process right... It's triggering a lot of the lines in the wrong
> > > > sequence...
> > > >
> > > > Can anyone think of a way to fix this or a better way to do this?
> > > > Please help
> > > >
> > >
> > > As you don't give all your code, we can't guess what happens exactly.
> > >
> > > Anyway, a few remarks:
> > >
> > > - You always use one different process for each command, even if you store it
> > > in the same variable.
> > >
> > > - To know when a process has stopped, you should use the "Kill" event, not a
> > > timer.
> > >
> > > - STOP EVENT does nothing in a timer event.
> > >
> > > --
> > > Benoit Minisini
> > >
> > > -------------------------------------------------------------------------
> > > This SF.net email is sponsored by: Microsoft
> > > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > > _______________________________________________
> > > Gambas-user mailing list
> > > Gambas-user at lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > >
> >
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------
More information about the User
mailing list