Home > Enterprise >  Symfony 6 - stop and start Message Messenger systemd worker as www-data user via PHP
Symfony 6 - stop and start Message Messenger systemd worker as www-data user via PHP

Time:11-07

I am currently working on a Symfony 6 project. Now I have the situation that I want to stop and start a systemd service which is used to consume the messages from the Symfony Messenger message queue. The service is called "[email protected]" and is located under /etc/systemd/system

Now in my PHP script I run the following:

$output = shell_exec("sudo -u www-data /usr/bin/systemctl stop [email protected] 2>&1");
dd($output)

The $output contains the following error message:

Failed to stop [email protected]: Interactive authentication required. See system logs and 'systemctl status [email protected]' for details.

Under /etc/sudoers.d I already created a file called "www-data". With the following code:

%www-data ALL=(ALL) NOPASSWD: /usr/bin/systemctl stop [email protected]

Does anyone have an idea what I am doing wrong here?

CodePudding user response:

As @Rufinius noted in the comments. It does not make sence to try to execute the command as -u www-data. Furthermore I realized that I had to remove "2>&1" at the end of the command in order to make it work properly. I think it's because I didn't include the part ("2>&1") in my sudoers.d file in the command, but I don't need it now anyway. So I adjusted it as the following:

shell_exec("sudo /usr/bin/systemctl stop [email protected]");
  • Related