me and a project partner are running a Minecraft server off of a RPI and connecting via its wireless feature. we wanted to create a python script which would control an LED and toggle it on/off based on whether the server was running. Whilst toggling an LED isnt very difficult in python for an RPI i was curious if there was any way to use python to check if the RPI was running a particular process and what that may look like?
With systemd you can check the state of a service, e.g. with:
rpi ~$ systemctl is-active minecraft.service inactive
Of course, I don't have Minecraft installed on my RasPi. You can call this command within your Python3 script. But this will waste resources because you have to poll the state in an endless loop. I would suggest to let the service itself toggle the LED. This can simply be done by creating or modifying a Minecraft service. I assume with the installation of Minecraft on the RasPi you already have a service to manage it. Then you can just add maybe two simple scripts
led-off.sh or any python script you want. Just edit the service with:
rpi ~$ sudo systemctl edit minecraft.service
In the empty editor insert these statements, save them and quit the editor:
[Service] ExecStartPost=/absolute/path/to/led-on.sh ExecStopPost=-/absolute/path/to/led-off.sh