B4J Question How to check if a 'service' is running?

amorosik

Expert
Licensed User
I read a very interesting post that shows how to use a non-UI B4j program in Windows service
However, it could happen that the program interrupts its functioning for various reasons
The question is: how to create a second B4J program to test whether a Windows service is running and, if not, start it?
 

Peter Simpson

Expert
Licensed User
Longtime User
I read a very interesting post that shows how to use a non-UI B4j program in Windows service
However, it could happen that the program interrupts its functioning for various reasons
The question is: how to create a second B4J program to test whether a Windows service is running and, if not, start it?
A non-ui app doesn't stop on windows. I have created multiple non-ui apps for processing data or website orders for clients that runs 24/7. I usually supply my clients with a new server, something like a HPE MicroServer Gen10 Plus V2 or dell servers, all setup in RAID-1.

You don't need a second appto test if the first app is running, either on Windows Server or windows 7, 8, 10 or 11. B4J non-ui background service apps (with my experience) run for months on end without stopping. My Dell R230 server in the loft has been running for about 14 months without rebooting, and the B4J non-ui background service app has been running that long without stopping or crashing. I will admit one hung though, he apo manages it's own garbage collection and doesn't wait for the system to clear it.

Can a non-ui app stop or crash, if course it can, but mine luckily never have.


Enjoy...
 
Upvote 0

amorosik

Expert
Licensed User
"..A non-ui app doesn't stop on windows.."
"..Can a non-ui app stop or crash, if course it can.."
You wrote two perfectly opposite statements

The reality is that ANY program can crash (even yours), for multiple reasons
And since this possibility is not foreseeable, then it is better to think of something to correct the situation in case the blockage occurs
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If it is a web server, I can periodically "ping" it. For example, a client using OkHttpUtils2 to make a GET request to a very simple API on that server that returns success.
I also can create an API to stop the server if I want.

 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
"..A non-ui app doesn't stop on windows.."
"..Can a non-ui app stop or crash, if course it can.."
You wrote two perfectly opposite statements
What I was saying is that Windows unlike Android doesn't just kill programs out of the blue, so with no bugs it will not stop. Anyway you can do whatever you want as it's your program. I'm just saying that 14 months with no crashes (server has not been rebooted in all that time) and my non-ui background worker app is still running flawlessly, no bugs luckily. The app processes orders every 10 minutes, between 80 and I would say 130 per day without failure (so far) with a well known e-commerce provider through their API.

Just use the abbackgroundworkers library as it's light weight and stable.
 
Upvote 0

amorosik

Expert
Licensed User
....so with no bugs it will not stop....
It is clear that an error-free program does not crash
And it is equally clear that the question asked is precisely to allow the operational continuation of a program that contains errors

.... and my non-ui background worker app is still running flawlessly, no bugs luckily....
This makes no contribution to the question asked
 
Upvote 0

a.consorti

Member
Licensed User
Hi Amoroso,
If I understand well what you need, you need a way to understand if one of your processes, although running, stops working and consequently terminate it. Correct?
 
Upvote 0

amorosik

Expert
Licensed User
Hi Amoroso,
If I understand well what you need, you need a way to understand if one of your processes, although running, stops working and consequently terminate it. Correct?

Yes, for example if a program is a 'printer server' it need remain h24 up and running
If for any reason (user intervention, or a bug on code, etc...) the main program stops running, then there must be some system such as a second program called 'guardian' that re-starts main program
It is clear that even the 'guardian' could stop and therefore not monitor the state of the main program, but being of tiny dimensions the guardian will be much more robust and reliable than a large system
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Yes, you can call it a "watchdog".
I have implemented something like this in a PHP web application using Cron job in shared hosting.
If I remembered correctly, what I did is I have the cron job running every hour, executing a PHP script to write to a database table with a timestamp. After 5 minutes, a checking to read the timestamp whether the last entry is more than 1 hour. If yes then an email will be send to my inbox to alert me.
There is a requirement which I need to make sure it is not the same server having issue.
That time I only have very limited knowledge of using PHP and I only have 1 or 2 shared hostings.
Shared hosting is not reliable because we are sharing resources with 100 or 1000 of other users.

If I am going to do similar thing today, I can use different B4J server or better a different hosting provider.
I can automate the attempt to restart the server. Upon successful or failure, an email will be send to me telling me everything is under control or I need to take a manual action.
 
Last edited:
Upvote 0

a.consorti

Member
Licensed User
Assuming you are using Microsoft Windows, the most obvious thing that comes to mind is if you have created an application that you then run as a service then you can use jshell to test the Windows service by having it execute a command in powershell such as:
Get-Service -Name your_service_name

If you have not created a service but simply a jar running as a program then I would advise you to test its execution on the system with something like this:

Check application running:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   Dim js As Shell
   js.Initialize("js", "tasklist", Null)
   js.Run(60000)
End Sub

Sub js_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success Then
     Dim m As Matcher = Regex.Matcher("your_application_name", StdOut)
     Dim count As Int
     If m.Find
       Log("Its alive")
     Else
         Log ("Restart the software again")
     End If
    
   Else
     Log(LastException)
   End If
End Sub


If none of the above methods appeal to you then you can always write a log file with just 1 single line with last timestamp every 5 minutes while with the control program you can test the same file and where you find the old timestamp then it means the main program is stopped and you can start it again.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
other.
example.
B4X:
    Wait For (CheckService("MariaDB")) Complete (Status As Boolean)
    Log(Status)

B4X:
Public Sub CheckService(ServiceName As String) As ResumableSub
    Dim Parameter As List
    Parameter.Initialize2(Array As String("query", ServiceName))
    Dim CmdShell As Shell
    CmdShell.Initialize("CheckService", "sc", Parameter)
    CmdShell.Run(DateTime.TicksPerSecond * 60)
    Wait For CheckService_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Return StdOut.ToUpperCase.Contains("RUNNING")
End Sub
1721726312193.png
 
Last edited:
Upvote 0
Top