It is really easy to launch and close applications on Linux using Gambas.
This may be useful if you need to run an application when certain conditions are met, and shut it down when conditions change.
An example is my BirdBoxMonitor program that needs to load a browser and display video streams.
Linux command line
It seems to me that the command line in Linux is more widely used than in other operating systems. Maybe this is because there are thousands of useful little command line programs that return information, and that there is often not a simple gui alternative.
In my particular case, it is not a small command line program that I need to run, it is a web browser. I want to automatically load Firefox from my Gambas BirdBoxMonitor when the monitor detects that a bird box system is running, and close Firefox when there are no bird box systems running.
Using Gambas
Since you can launch Firefox from the Linux command line by simply typing:-
firefox
...you can do the same from within a Gambas program by using the Exec command:-
Exec ["firefox"]
And if you want to close Firefox you can kill the process:-
Exec ["pkill", "firefox"]
It is also necessary to check whether Firefox is running first, before to try to either launch it or kill it. We can do this by searching through all running processes using pgrep:-
Exec ["pgrep", "-l", "firefox"] Wait To strReply
...which means; list the process name (as well at process id number), Wait for the information to be returned, and put it in the Gambas string variable strReply.
Using this command on my Lubuntu laptop gives me something like this:-
1705 firefox
...where the number is simply the process id number (it could be almost any number) assigned by the system.
On a Raspberry Pi I get:-
15392 firefox-esr
...because the Pi uses a different version of Firefox.
Here is a simple Gambas code example:-
To start or continue to run Firefox, we call the routine with blnRun set to True.
To close or leave Firefox closed, we call the routine with blnRun set to False.
No comments:
Post a Comment