Viewing bash/php scripts in real time

Post your Gambas programming questions here.
Online
rj71
Posts: 40
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Viewing bash/php scripts in real time

Post by rj71 »

Hi All,
I have a project that I've been working for awhile that consists of a main bash script that does several things in order including calling a few php scripts (database stuff) and a few Linux commands (find and comm) and then the script ends and I have a nice chunk of data to either ignore or insert into a mysql DB. What I'm trying to do is build a nice little Gambas GUI app that somehow can show me the progress of all this stuff that's happening in that bash script including the php scripts and Linux commands as well as launch the script itself. I'm aware of exec and shell and have used both but in those cases I was never looking to get any output from the scripts. So far my experiments with Shell and "To string" are not working as most times I don't see anything until the scripts is done or I just get nothing back at all or it crashes...etc. Is there a way to see what's going on in in the bash script real time and send to a gambas component like a textarea box? The process is generating a lot of text data so I assume that is a problem somehow.

BTW, I have experimented with just completely writing this project in pure Gambas and eliminating bash, php and Linux commands. The amount of text data generated that I tried getting into textarea components quickly crashed the app so I the experiment didn't last long as that is a show stopper.
User avatar
BruceSteers
Posts: 1623
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Viewing bash/php scripts in real time

Post by BruceSteers »

rj71 wrote: Friday 24th May 2024 7:20pm Hi All,
I have a project that I've been working for awhile that consists of a main bash script that does several things in order including calling a few php scripts (database stuff) and a few Linux commands (find and comm) and then the script ends and I have a nice chunk of data to either ignore or insert into a mysql DB. What I'm trying to do is build a nice little Gambas GUI app that somehow can show me the progress of all this stuff that's happening in that bash script including the php scripts and Linux commands as well as launch the script itself. I'm aware of exec and shell and have used both but in those cases I was never looking to get any output from the scripts. So far my experiments with Shell and "To string" are not working as most times I don't see anything until the scripts is done or I just get nothing back at all or it crashes...etc. Is there a way to see what's going on in in the bash script real time and send to a gambas component like a textarea box? The process is generating a lot of text data so I assume that is a problem somehow.

BTW, I have experimented with just completely writing this project in pure Gambas and eliminating bash, php and Linux commands. The amount of text data generated that I tried getting into textarea components quickly crashed the app so I the experiment didn't last long as that is a show stopper.
Using "To String" with shell implies Wait and the command will not return any results till it is finished.

Use a Process handler with your Shell or Exec command
Then read the output in the Process_Read() event

Something like this... (edited)

Private hProc As Process

Public Sub RunScript()

  hProc = Shell "/path/to/bash_script" For Input As "PROC"

End

Public Sub PROC_Read()

  Dim sOutput As String
  sOutput = Read #Last, Lof(Last)
  Print sOutput

End

If at first you don't succeed , try doing something differently.
BruceS
Online
rj71
Posts: 40
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Re: Viewing bash/php scripts in real time

Post by rj71 »

BruceSteers wrote: Friday 24th May 2024 7:50pm
rj71 wrote: Friday 24th May 2024 7:20pm Hi All,
I have a project that I've been working for awhile that consists of a main bash script that does several things in order including calling a few php scripts (database stuff) and a few Linux commands (find and comm) and then the script ends and I have a nice chunk of data to either ignore or insert into a mysql DB. What I'm trying to do is build a nice little Gambas GUI app that somehow can show me the progress of all this stuff that's happening in that bash script including the php scripts and Linux commands as well as launch the script itself. I'm aware of exec and shell and have used both but in those cases I was never looking to get any output from the scripts. So far my experiments with Shell and "To string" are not working as most times I don't see anything until the scripts is done or I just get nothing back at all or it crashes...etc. Is there a way to see what's going on in in the bash script real time and send to a gambas component like a textarea box? The process is generating a lot of text data so I assume that is a problem somehow.

BTW, I have experimented with just completely writing this project in pure Gambas and eliminating bash, php and Linux commands. The amount of text data generated that I tried getting into textarea components quickly crashed the app so I the experiment didn't last long as that is a show stopper.
Using "To String" with shell implies Wait and the command will not return any results till it is finished.

Use a Process handler with your Shell or Exec command
Then read the output in the Process_Read() event

Something like this...

Private hProc As Process

Public Sub RunScript()

  hProc = Shell "/path/to/bash_script" For Output As "PROC"

End

Public Sub PROC_Read()

  Dim sOutput As String
  sOutput = Read #Last, Lof(Last)
  Print sOutput

End

Thanks Bruce! That's what I'm looking for although I think I may have done a poor job of asking my question. I'm looking for what your code does but printing it to a component (textarea...label?) on the form instead of the IDE console and seeing that data zipping by in real time. Maybe I'm approaching this the wrong way.
Online
rj71
Posts: 40
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Re: Viewing bash/php scripts in real time

Post by rj71 »

I just found some of Bruce's examples in another thread. I'm going to check those out now.
User avatar
BruceSteers
Posts: 1623
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Viewing bash/php scripts in real time

Post by BruceSteers »

Okay (edited)


Private hProc As Process
 
Public Sub RunScript()
 
  hProc = Shell "/path/to/bash_script" For Input As "PROC"
 
End
 
Public Sub PROC_Read()
 
  Dim sOutput As String
  sOutput = Read #Last, Lof(Last)
  TextArea1.Insert(sOutput)
  TextArea1.EnsureVisible
 
End

If at first you don't succeed , try doing something differently.
BruceS
Online
rj71
Posts: 40
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Re: Viewing bash/php scripts in real time

Post by rj71 »

BruceSteers wrote: Friday 24th May 2024 8:17pm Okay


Private hProc As Process
 
Public Sub RunScript()
 
  hProc = Shell "/path/to/bash_script" For Output As "PROC"
 
End
 
Public Sub PROC_Read()
 
  Dim sOutput As String
  sOutput = Read #Last, Lof(Last)
  TextArea1.Insert(sOutput)
  TextArea1.EnsureVisible
 
End

This is strange. I tried your new code and it's still printing to console even though "Print sOutput" is commented out. Nothing is populating the textarea component. I'm not sure what I'm doing wrong. I've been working on this project awhile and the whole thing has been a gigantic headache :lol:
User avatar
BruceSteers
Posts: 1623
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Viewing bash/php scripts in real time

Post by BruceSteers »

Oops sorry use Input not Output

hProc = Shell "/path/to/bash_script" For Input As "PROC"
If at first you don't succeed , try doing something differently.
BruceS
Online
rj71
Posts: 40
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Re: Viewing bash/php scripts in real time

Post by rj71 »

BruceSteers wrote: Friday 24th May 2024 8:47pm Oops sorry use Input not Output

hProc = Shell "/path/to/bash_script" For Input As "PROC"
:lol: no prob. works now. Another question though...how much data can be input into a textarea component? In some of my tests, there appears to be a clear limit as it ends up crashing the app but I don't know exactly how much it consumed before it crashed.
User avatar
BruceSteers
Posts: 1623
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Viewing bash/php scripts in real time

Post by BruceSteers »

rj71 wrote: Friday 24th May 2024 8:58pm
BruceSteers wrote: Friday 24th May 2024 8:47pm Oops sorry use Input not Output

hProc = Shell "/path/to/bash_script" For Input As "PROC"
:lol: no prob. works now. Another question though...how much data can be input into a textarea component? In some of my tests, there appears to be a clear limit as it ends up crashing the app but I don't know exactly how much it consumed before it crashed.
I didn't know there was a limit, i should think it's huge is there is.

maybe try clearing the textarea if it has grown too large.

Public Sub PROC_Read()
  
  Dim sOutput As String
  sOutput = Read #Last, Lof(Last)
  if TextArea1.Length>5000 then TextArea1.Clear
  TextArea1.Insert(sOutput)
  TextArea1.EnsureVisible
  
End
If at first you don't succeed , try doing something differently.
BruceS
Online
rj71
Posts: 40
Joined: Thursday 13th April 2023 6:39pm
Location: Vancouver WA

Re: Viewing bash/php scripts in real time

Post by rj71 »

BruceSteers wrote: Friday 24th May 2024 10:23pm
rj71 wrote: Friday 24th May 2024 8:58pm
BruceSteers wrote: Friday 24th May 2024 8:47pm Oops sorry use Input not Output

hProc = Shell "/path/to/bash_script" For Input As "PROC"
:lol: no prob. works now. Another question though...how much data can be input into a textarea component? In some of my tests, there appears to be a clear limit as it ends up crashing the app but I don't know exactly how much it consumed before it crashed.
I didn't know there was a limit, i should think it's huge is there is.

maybe try clearing the textarea if it has grown too large.

Public Sub PROC_Read()
  
  Dim sOutput As String
  sOutput = Read #Last, Lof(Last)
  if TextArea1.Length>5000 then TextArea1.Clear
  TextArea1.Insert(sOutput)
  TextArea1.EnsureVisible
  
End
Some of the text files the process is generating are huge...like 4 to 5 gig text files. In one test, I tried scanning a drive (that's one part of this project) that had several million files, it went for 5 minutes or so before I got a segmentation fault. I'll try clearing the textarea like you suggest. Thanks Bruce!
Post Reply