Viewing bash/php scripts in real time

Post

Posted
Rating:
#1 (In Topic #1249)
Regular
rj71 is in the usergroup ‘Regular’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

rj71 said

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)

Code (gambas)

  1.  
  2.  
  3. Public Sub RunScript()
  4.  
  5.   hProc = Shell "/path/to/bash_script" For Input As "PROC"
  6.  
  7.  
  8. Public Sub PROC_Read()
  9.  
  10.   Dim sOutput As String
  11.   sOutput = Read #Last, Lof(Last)
  12.   Print sOutput
  13.  
  14.  
  15.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

rj71 said

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…

Code (gambas)

  1.  
  2.  
  3. Public Sub RunScript()
  4.  
  5.   hProc = Shell "/path/to/bash_script" For Output As "PROC"
  6.  
  7.  
  8. Public Sub PROC_Read()
  9.  
  10.   Dim sOutput As String
  11.   sOutput = Read #Last, Lof(Last)
  12.   Print sOutput
  13.  
  14.  
  15.  

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 now: No Back to the top

Post

Posted
Rating:
#4
Regular
rj71 is in the usergroup ‘Regular’
 I just found some of Bruce's examples in another thread. I'm going to check those out now.
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
Okay  (edited)

Code (gambas)

  1.  
  2.  
  3. Public Sub RunScript()
  4.  
  5.   hProc = Shell "/path/to/bash_script" For Input As "PROC"
  6.  
  7.  
  8. Public Sub PROC_Read()
  9.  
  10.   Dim sOutput As String
  11.   sOutput = Read #Last, Lof(Last)
  12.   TextArea1.Insert(sOutput)
  13.   TextArea1.EnsureVisible
  14.  
  15.  
  16.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

Okay

Code (gambas)

  1.  
  2.  
  3. Public Sub RunScript()
  4.  
  5.   hProc = Shell "/path/to/bash_script" For Output As "PROC"
  6.  
  7.  
  8. Public Sub PROC_Read()
  9.  
  10.   Dim sOutput As String
  11.   sOutput = Read #Last, Lof(Last)
  12.   TextArea1.Insert(sOutput)
  13.   TextArea1.EnsureVisible
  14.  
  15.  
  16.  

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:
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
 Oops sorry use Input not Output

  hProc = Shell "/path/to/bash_script" For Input As "PROC"
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

rj71 said

BruceSteers said

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.

Code (gambas)

  1. Public Sub PROC_Read()
  2.  
  3.   Dim sOutput As String
  4.   sOutput = Read #Last, Lof(Last)
  5.   if TextArea1.Length>5000 then TextArea1.Clear
  6.   TextArea1.Insert(sOutput)
  7.   TextArea1.EnsureVisible
  8.  
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

rj71 said

BruceSteers said

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.

Code (gambas)

  1. Public Sub PROC_Read()
  2.  
  3.   Dim sOutput As String
  4.   sOutput = Read #Last, Lof(Last)
  5.   if TextArea1.Length>5000 then TextArea1.Clear
  6.   TextArea1.Insert(sOutput)
  7.   TextArea1.EnsureVisible
  8.  
  9.  

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!
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
Another option could be to use a TerminalView not a TextArea you could then just run the script in the terminalview and see the normal terminal output.

Code (gambas)

  1.  
  2. p = TerminalView1.Shell("my/command")
  3.  
  4. While p.state = Process.Running
  5.   Wait 0.1
  6.  
  7.  
Online now: No Back to the top

Post

Posted
Rating:
#12
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

Another option could be to use a TerminalView not a TextArea you could then just run the script in the terminalview and see the normal terminal output.

Code (gambas)

  1.  
  2. p = TerminalView1.Shell("my/command")
  3.  
  4. While p.state = Process.Running
  5.   Wait 0.1
  6.  
  7.  

I'll play with terminal view and see if that works. Thanks Bruce!
Online now: No Back to the top

Post

Posted
Rating:
#13
Regular
rj71 is in the usergroup ‘Regular’

rj71 said

BruceSteers said

Another option could be to use a TerminalView not a TextArea you could then just run the script in the terminalview and see the normal terminal output.

Code (gambas)

  1.  
  2. p = TerminalView1.Shell("my/command")
  3.  
  4. While p.state = Process.Running
  5.   Wait 0.1
  6.  
  7.  

I'll play with terminal view and see if that works. Thanks Bruce!

Hey Bruce, I like the terminalview and have made some changes to my back end scripts to make this work. Just one small problem: with longer running scripts, the terminalview eventually stops scrolling down and I can't tell if the script has finished without scrolling down manually. Is there a way to auto scroll to the bottom until the script finishes?
Online now: No Back to the top

Post

Posted
Rating:
#14
Regular
rj71 is in the usergroup ‘Regular’
Ah, I see the .ensurevisible now. Sweet!
Online now: No Back to the top

Post

Posted
Rating:
#15
Guru
BruceSteers is in the usergroup ‘Guru’

rj71 said

rj71 said

BruceSteers said

Another option could be to use a TerminalView not a TextArea you could then just run the script in the terminalview and see the normal terminal output.

Code (gambas)

  1.  
  2. p = TerminalView1.Shell("my/command")
  3.  
  4. While p.state = Process.Running
  5.   Wait 0.1
  6.  
  7.  

I'll play with terminal view and see if that works. Thanks Bruce!

Hey Bruce, I like the terminalview and have made some changes to my back end scripts to make this work. Just one small problem: with longer running scripts, the terminalview eventually stops scrolling down and I can't tell if the script has finished without scrolling down manually. Is there a way to auto scroll to the bottom until the script finishes?

You got it, EnsureVisible :)

I found the same with TerminalView, mostly it's fine but occasionally it misses a scroll and then stops scrolling with the output.

I do something like this…

Code (gambas)

  1.  
  2.  
  3. Public Sub RunCommand(sCommand)
  4.  
  5.   hProc = TerminalView1.Shell(sCommand)
  6.   hObs = New Observer(hProc) As "PROC"  ' Observe hProc events as PROC_EventName
  7.  
  8.  
  9. Public Sub PROC_Read()   ' Trigger EnsureVisible on each Read event.
  10.  
  11.   TerminalView1.EnsureVisible()
  12.  
  13.  
  14. ' Detect when the command process ends and kill the event observer.
  15. ' PS. TerminalView1_Kill() does the same for this but we have a process event observer so I'll use that.
  16. Public Sub PROC_Kill()
  17.  
  18.   hObs = Null
  19.  
  20.  
Online now: No Back to the top

Post

Posted
Rating:
#16
Regular
rj71 is in the usergroup ‘Regular’

BruceSteers said

rj71 said

rj71 said



I'll play with terminal view and see if that works. Thanks Bruce!

Hey Bruce, I like the terminalview and have made some changes to my back end scripts to make this work. Just one small problem: with longer running scripts, the terminalview eventually stops scrolling down and I can't tell if the script has finished without scrolling down manually. Is there a way to auto scroll to the bottom until the script finishes?

You got it, EnsureVisible :)

I found the same with TerminalView, mostly it's fine but occasionally it misses a scroll and then stops scrolling with the output.

I do something like this…

Code (gambas)

  1.  
  2.  
  3. Public Sub RunCommand(sCommand)
  4.  
  5.   hProc = TerminalView1.Shell(sCommand)
  6.   hObs = New Observer(hProc) As "PROC"  ' Observe hProc events as PROC_EventName
  7.  
  8.  
  9. Public Sub PROC_Read()   ' Trigger EnsureVisible on each Read event.
  10.  
  11.   TerminalView1.EnsureVisible()
  12.  
  13.  
  14. ' Detect when the command process ends and kill the event observer.
  15. ' PS. TerminalView1_Kill() does the same for this but we have a process event observer so I'll use that.
  16. Public Sub PROC_Kill()
  17.  
  18.   hObs = Null
  19.  
  20.  

Thanks Bruce. I'll try this bit of code because I did a test this morning and the terminalview stopped auto scrolling down on a very long running script. I'll take a look at it tomorrow because it's Indy 500 day and I got some meats to grill and some racing to watch :D
Online now: No Back to the top

Post

Posted
Rating:
#17
Regular
rj71 is in the usergroup ‘Regular’
 So I tried the code on a really long running script last night. The terminal view at some point stopped scrolling and even looked like it stop responding. I wasn't watching it so I don't know exactly how long the script ran before this happened. Not much I can do about the script, its just something that will take a long time to complete, so I guess its back to the drawing board for this project.
Online now: No Back to the top
1 guest and 0 members have just viewed this.