Re: Starting a background process and reporting failure if applicable
| From: | Jānis Elmeris | Date: | Wed, 19 Aug 2020 15:59:57 +0000 |
| Subject: | Re: Starting a background process and reporting failure if applicable | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-327314@lists.php.net to get a copy of this message | ||
Thank you for the pointer!
I wanted to not manage it in that good way. :) And the parent script does
not need to know the result of the other process.
I just wanted to being able to monitor the errors. In case of some errors
in the other process script, it would write them to an error log that I
would monitor.
But the errors I cannot find how to monitor easily, would happen upon
launching the script (e.g., the script file does not exist, is not
accessible or something like that).
For now, I just do without monitoring the sub-process starting error
monitoring.
Regards,
Janis
trešd., 2020. g. 19. aug., plkst. 18:51 — lietotājs Alexandru Pătrănescu (<
drealecs@gmail.com>) rakstīja:
> Hi,
>
> I would recommend using a library that manages this in a good way:
>
>
> https://symfony.com/doc/current/components/process.html#running-processes-asynchronously
> This way you can start a process and wait for it to finish and obtain
> it's exit code once it's done. In the meantime you can even feed it
> with input and fetch it's output.
>
> There are other libraries that do that, not only symfony process but
> this one is usually my first choice as it works in all environments
> windows/linux and it's well tested and used a lot.
>
> Regards,
> Alex
>
> On Mon, Aug 17, 2020 at 4:15 PM Jānis Elmeris <janis.elmeris@lv.lv> 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
>