Any way to start systemd process from a PHP script?

Hi there everyone!

I’m wondering if there’s any way to start a systemd service from a PHP script. I have a web panel that multiple admins use and each of them need the convenient ability to start/stop/restart a systemd service.

I’ve done a ton of googling and I can find hundreds of topics about starting a PHP script from a systemd service but nothing in the other direction.

Any help or suggestion would be most welcome!

Have you thought about replacing systemd with a small “microserver” build in Golang (or other language) and just call this server and let this server do the task? I e make your own systemd?

exec('systemctl start foobar');
exec('systemctl stop foobar');

That does however mean that PHP must be running as root, which is not a good idea.

Instead you could create a wrapper around systemctl, like my-systemctl:

systemctl $@

Make it executable with chmod +x my-systemctl

and then from PHP call it with sudo:

exec('sudo my-systemctl start foobar');
exec('sudo my-systemctl stop foobar');

And then allow the user that is running PHP to call that script without requiring a password. See

1 Like

If you mean synchronal: exec(), system(), shell_exec(), backtick operator(``). Asynchronal: popen().

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.