Laravel run systemctl --user using process

Viewed 30

I'm trying to run some services on a ubuntu server using process() (from Symfony\Component\Process\Process)

$process = new Process(['systemctl', '--user', 'start', $serviceName]);
     try {
         $process->mustRun();
     } 
     catch (ProcessFailedException $ex) {
         Log::alert($ex->getMessage());
     }

but I'm getting the following error:

The command "'systemctl' '--user' 'start' 'some_service_name.service'" failed.

Exit Code: 1(General error)

Working directory: /

Output:


Error Output:

Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)

Any help would be much appreciated!

1 Answers

I figured it out, for anyone facing the same problem the argument "--machine=username@.host" was missing.

so the end result should be something like this:

$process = new Process(['systemctl', '--machine=USER_HERE@.host', '--user', 'start', $serviceName]);
 try {
     $process->mustRun();
 } 
 catch (ProcessFailedException $ex) {
     Log::alert($ex->getMessage());
 }

(replace the USER_HERE with your user)

Related