How to give a name to a command inside shell_exec()?

Viewed 104

I am working in the Laravel framework where I am using shell_exec(). I am creating the string of command as in the following example.

 $cmd = "php artisan serve:setup";
 $resp = shell_exec($cmd);
 

How can I put a name to the above command? It shows in the system monitor as only "php"; I have multiple commands like this. I need to access the PID of that command and also the status of that command.

The command is running successfully, and in the system monitor, it shows the process name as "PHP".

I need process name with exec() or shell_exec() from PHP.

I have tried like this:

 $cmd = "bash -c exec ServerCPP -a cd $path && php artisan serve:setup 2>&1";
 $resp = shell_exec($cmd);
 dd($resp);

It gives the error: "Could not open input file: artisan."

2 Answers

Your last attempt needs to be corrected:

  • cd first and quote the path in case the $path variable contains spaces
  • quotes around the command executed with bash -c
  • exec -a ServerCPP if you want to give the name ServerCPP to your process
$cmd = "cd '$path' && bash -c 'exec -a ServerCPP php artisan serve:setup' 2>&1";
$resp = shell_exec($cmd);
dd($resp);

The best idea would be to create a command.

php artisan make:command ServeSetup

Once the file is generated, set the name of your command in app/Console/Commands.

protected $signature = 'serve:setup';

Hope this helps.

Related