0

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 "messenger-worker@.service" 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 messenger-worker@1.service 2>&1");
dd($output)

The $output contains the following error message:

Failed to stop messenger-worker@1.service: Interactive authentication required. See system logs and 'systemctl status messenger-worker@1.service' 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 messenger-worker@1.service

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

schwaluck
  • 115
  • 9
  • 1
    why do you use -u www-data? you are the www-data and you dont have permissions to use systemctl. you set the sudoers to allow your www-data to execute the systemctl as root.. so then execute it as root. (the service itself should fallback to the desired users) `sudo /usr/bin/system....` – Rufinus Oct 30 '22 at 09:41

1 Answers1

0

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 messenger-worker@1.service");
schwaluck
  • 115
  • 9