Re: Starting a background process and reporting failure if applicable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Can you give an example, please?

I'm trying this, for example:

<?php

function execute_shell($command_in, $in_background = false) {
  $command = "$command_in > /dev/null &";
  $process = proc_open("$command", [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'a']], $pipes);
  error_log('execute_shell: ' . var_export(['command' => $command_in, 'process' => $process, 'pipes' => $pipes], true));
}

execute_shell('php sleeps.php', true);
execute_shell('php sleeps_nonexist.php', true);

Where script `sleeps.php` sleeps for 3 seconds and writes its progress into a file (error log), and file `sleeps_nonexist.php` does not exist.

I don't see how I can detect whether the background script was launched successfully or not.

[18-Aug-2020 14:34:17 Europe/Helsinki] execute_shell: array (
  'command' => 'php sleeps.php',
  'process' => NULL,
  'pipes' =>
  array (
    0 => NULL,
    1 => NULL,
    2 => NULL,
  ),
)
[18-Aug-2020 14:34:17 Europe/Helsinki] execute_shell: array (
  'command' => 'php sleeps_nonexist.php',
  'process' => NULL,
  'pipes' =>
  array (
    0 => NULL,
    1 => NULL,
    2 => NULL,
  ),
)
[18-Aug-2020 14:34:17 Europe/Helsinki] sleep to erorr log1
[18-Aug-2020 14:34:20 Europe/Helsinki] sleep to erorr log2

I would also be satisfied if the error is written to a file, but pointing the stderr in `proc_open` to the error log file gave no output either.

Regards,
Janis


pirmd., 2020. g. 17. aug., plkst. 16:30 — lietotājs Aziz Saleh (<azizsaleh@xxxxxxxxx>) rakstīja:
It would be better to use proc_open imo which will allow you to get the output/state built in php functionality:

https://www.php.net/manual/en/function.proc-open.php

On Mon, Aug 17, 2020 at 9:15 AM Jānis Elmeris <janis.elmeris@xxxxx> wrote:
Hello!

I would like to start a PHP script in the background from within another PHP script. I would like to monitor all error cases properly.

Is there an easy way to see if that background script was started successfully? The background script would write its errors into an error log that I'm monitoring. But what about starting the script itself?

I'm trying to start a background script like this:

exec('nohup php sleeps.php > /tmp/tt 2>&1 &', $output, $status);
error_log('execute_shell: ' . var_export(['status' => $status, 'output' => $output], true));
exec('nohup php sleeps_nonexist.php > /tmp/tt 2>&1 &', $output, $status);
error_log('execute_shell: ' . var_export(['status' => $status, 'output' => $output], true));

The first `exec` runs all right in the background (asynchronously).
The second one behaves in exactly the same way (`$status === 0`), even though file "sleeps_nonexist.php" does not exist.

Running in the command line manually, this would give status `0`:
nohup php sleeps.php > /tmp/tt 2>&1
echo $?

This would give status `1`:
nohup php sleeps_nonexist.php > /tmp/tt 2>&1
echo $?

Regards,
Janis

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux