[Gambas-user] Are DB servers installed and started?
Tobias Boege
tobs at taboege.de
Thu Jun 10 16:11:45 CEST 2021
Hello Hans,
On Thu, 10 Jun 2021, Hans Lehmann wrote:
> Hello.
>
> When I present my DB sample programs in the Gambas book, I want to make
> sure that the DB program only starts if (A) the DB server is installed and
> (B) is also started. For (A) I use both System.Find("postgresql") and
> System.Exist("postgresql"). In both cases the output is NULL. With MySQL,
> however, I get TRUE and the path '/usr/bin/mysql'. Whether the DB server -
> related to (B) - is started can be safely retrieved via its status, which
> certainly works for PostgreSQL *and* MySQL on my system. Now the question:
> Where do you see possible errors?
>
System.Find or System.Exist look for *program names*, not *package names*
(which are of course not standardized across distributions). You may have
the package "postgresql" installed, but the program you may want to look
for is the postgresql server program, called "postgres". This should work
unless your distribution decides to change the executable name.
As for question (B), I can suggest two alternatives:
(1) Before your main program starts, pop up a small input form which asks
the user for their preferred database connection string (and give the user
feedback about whether you detect a server at the given address) and maybe
store that for the future in a settings file. This is the robust solution
which annoys or expects too much knowledge from your users.
Given the myriad ways of running a database server (over TCP, which port,
or over UNIX only, or ...) it will be hard to detect if there is a specific
database server. Even default configurations may vary across systems.
(2) Rather, I would suggest you use your system's service manager. This
is less portable because some (many) systems use systemd, some the old
/etc/init.d scripts. BSDs may work yet differently. But _if_ you commit
to systemd-based distributions, you can query systemctl for the status
of any service managed by it:
$ systemctl is-active postgresql
inactive
This has the added advantage that that "mariadb" and "mysqld" are synonyms,
so you could use either to get the status of whatever implementation is
installed on the machine.
Using systemctl also allows you to solve your problem (A). The exit status
of `systemctl status` tells you whether a service is active, inactive or
unknown (which you can take as "not installed"):
$ systemctl status NetworkManager >/dev/null 2>&1; echo $?
0 # --> NetworkManager is running (and obviously installed)
$ systemctl status postgresql >/dev/null 2>&1; echo $?
3 # --> postgresql is installed but not running
$ systemctl status msaccess >/dev/null 2>&1; echo $?
4 # --> no service by the name "msaccess" on my system
This is documented in `man systemctl`.
In Gambas, you would do the following to obtain $?:
' Exec For Read Write but do not read or write anything to avoid
' cluttering the terminal. We only want the exit code.
Exec ["systemctl", "status", "postgresql"] Wait For Read Write
Print Process.LastValue
Of course, you can do something similar with /etc/init.d scripts instead
of systemd.
Best,
Tobias
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list