B4J Question Shell.run no output

Alpay ABAY

Member
Licensed User
I tried a few different ways, I couldn't make this shell output work.
I tested .jar on ubuntu, when I run same code it works.
Not raising also any exception. It is a console application.
Looks like when I make shell call, application stops working. Never reaches to Log("Operation complete")

Please if anyone knows why ?

B4X:
    Try
        Dim shl As Shell
        shl.Initialize("shl", "/usr/bin/mpstat", Array("-P","ALL"))
        shl.Run(-1)
        Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        Log("done")
        Log("Ecode:"&ExitCode)
        Log("Error:"&StdErr)
        Log("out:"&StdOut)
        Log("Status:"&Success)
    Catch
        Log("Error execution")
        Log(LastException)
    End Try 
    Log("Operation complete")
 

Attachments

  • ubuntu-preview.png
    ubuntu-preview.png
    8 KB · Views: 50

teddybear

Well-Known Member
Licensed User
I guess you are using Non-UI app, you need to do StartMessageLoop waiting for shl_ProcessCompleted .
Try this code
B4X:
Sub AppStart (Args() As String)
        Dim shl As Shell
        shl.Initialize("shl", "/usr/bin/mpstat", Array("-P","ALL"))
        shl.Run(-1)
        StartMessageLoop
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        Log("done")
        Log("Ecode:"&ExitCode)
        Log("Error:"&StdErr)
        Log("out:"&StdOut)
        Log("Status:"&Success)
        Log("Operation complete")
        StopMessageLoop
End Sub
 
Last edited:
Upvote 0

Alpay ABAY

Member
Licensed User
I guess you are using Non-UI app, you need to do StartMessageLoop waiting for shl_ProcessCompleted .
Try this code
B4X:
Sub AppStart (Args() As String)
        Dim shl As Shell
        shl.Initialize("shl", "/usr/bin/mpstat", Array("-P","ALL"))
        shl.Run(-1)
        StartMessageLoop
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        Log("done")
        Log("Ecode:"&ExitCode)
        Log("Error:"&StdErr)
        Log("out:"&StdOut)
        Log("Status:"&Success)
        Log("Operation complete")
        StopMessageLoop
End Sub
Thanks, but It is not relating to that. StartmessageLoop only wait main thread, but i call it with wait for to collect output and return, also your method with fail. I have various calls.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Thanks, but It is not relating to that. StartmessageLoop only wait main thread, but i call it with wait for to collect output and return, also your method with fail. I have various calls.
The code works here. this's the result.
result.png


I don't know why it does not work your there. you'd better upload a small project for the question.
 
Last edited:
Upvote 0

Alpay ABAY

Member
Licensed User
I will upload the code. But why wait for is not working in that code block is a mystery for me.
If somebody has an idea, so welcome! :)
 
Upvote 0

Alpay ABAY

Member
Licensed User
I do not have any special code. I could'nt make it work so I stopped before start developing related library. Simple console application with code below.
Added also StartMessageLoop with no change on result.

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Dim systemType As String
    'Dim utils As serverStatUtils
    Dim EOL As String = Chr(13)&Chr(10)
End Sub

Sub AppStart (Args() As String)
    Try
        Dim shl As Shell
        shl.Initialize("shl", "/usr/bin/mpstat", Array("-P","ALL"))
        shl.WorkingDirectory = "/"
        shl.RunWithOutputEvents(6000)
        Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        Log("done")
        Log("Ecode:"&ExitCode)
        Log("Error:"&StdErr)
        Log("out:"&StdOut)
        Log("Status:"&Success)
    Catch
        Log("Error execution")
        Log(LastException)
    End Try
    Log("Operation complete")      
    StartMessageLoop
End Sub
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Dim systemType As String
    'Dim utils As serverStatUtils
    Dim EOL As String = Chr(13)&Chr(10)
End Sub

Sub AppStart (Args() As String)
    Try
        Dim shl As Shell
        shl.Initialize("shl", "/usr/bin/mpstat", Array("-P","ALL"))
        shl.WorkingDirectory = "/"
        shl.RunWithOutputEvents(6000)
        Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        Log("done")
        Log("Ecode:"&ExitCode)
        Log("Error:"&StdErr)
        Log("out:"&StdOut)
        Log("Status:"&Success)
    Catch
        Log("Error execution")
        Log(LastException)
    End Try
    Log("Operation complete")
    StartMessageLoop
End Sub
The code you posted will not work,did you try that code I posted at post#2?
The project is attached
 

Attachments

  • jshellubunt.zip
    895 bytes · Views: 34
Last edited:
Upvote 0
Top