2

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."

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
MHEMBINT
  • 105
  • 1
  • 10
  • Does this answer your question? [Start a process with a name](https://stackoverflow.com/questions/11130229/start-a-process-with-a-name) – Peter Feb 03 '21 at 12:12
  • 1
    FTR: This has nothing to do with PHP and depends only on OS facilities to change the process title. – Peter Feb 03 '21 at 12:14
  • 1
    @Peter, I am using php function to run this command it is from the code. – MHEMBINT Feb 03 '21 at 13:11
  • Add `&& echo $! ` at the end of your command. This will output the pid on the last line. You might have to trim() the last line. – NVRM Feb 04 '21 at 00:53

2 Answers2

1

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);
xhienne
  • 5,738
  • 1
  • 15
  • 34
0

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.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • 1
    i want to name it from what is in $cmd which i want to run in shell_exec(). The answer you told i already knows that. – MHEMBINT Feb 04 '21 at 04:29