Re: Starting a background process and reporting failure if applicable
| From: | Jānis Elmeris | Date: | Tue, 18 Aug 2020 10:54:09 +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-327298@lists.php.net to get a copy of this message | ||
Thank you for the idea of user's browser to initiate the additional
request! In this case, I would prefer it to be done in the background (not
affecting the response to the browser) and not depending on the
user/browser.
Janis
pirmd., 2020. g. 17. aug., plkst. 21:52 — lietotājs Tedd Sperling (<
tedd.sperling@gmail.com>) rakstīja:
>
>
> > On Aug 17, 2020, at 9:14 AM, 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 �úûq¼F
> > HmÈ ÁY), 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
>
>
> Janis:
>
> Look at header — it’s much simpler.
>
> For example, I often use it to start another script from within a script,
> like so:
>
> if ($auth == 0)
> {
> // send user to log on page
> header('Location:log-on.php');
> exit();
> }
>
> See:
>
> https://www.php.net/manual/en/function.header.php
>
> Cheers,
>
> Tedd
>
> Tedd Sperling
> tedd.sperling@gmail.com