Thank you for the library pointer!
Regarding the exit code as the output.
1) I tried something like that, but I got `0` always. By your example, I see how the parentheses could be used to get `1` for the failed command. Which is good, but...
2) If I read the output of the command, it won't execute asynchronously. I tried various combinations, but I couldn't get an approach that would both run asynchronously (and independently, so I can quit the caller script) and would tell me the status of whether the process was started successfully.
Janis
otrd., 2020. g. 18. aug., plkst. 06:40 — lietotājs Stefan A. (<acid24@xxxxxxxxx>) rakstīja:
Additionally you can also look at libraries such symfony/process https://symfony.com/doc/current/components/process.html#running-processes-asynchronouslyOn Tue, Aug 18, 2020 at 5:28 AM Stefan A. <acid24@xxxxxxxxx> wrote:A somewhat hacky way to do it, would be like thisexec('(nohup php sleeps_nonexist.php > /tmp/tt 2>&1; echo $?) &', $output, $status);
then instead of checking the $status variable, you would check the $output which in this case would have the value '1' at index 0 or the value '0' if the command executed successfully.
I don't know how reliable this is.On Mon, Aug 17, 2020 at 9:23 PM Paul M Foster <paulf@xxxxxxxxxxxxxxxxx> wrote:On Mon, Aug 17, 2020 at 02:51:58PM -0400, Tedd Sperling wrote:
>
>
> > On Aug 17, 2020, at 9:14 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
>
>
> 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();
> }
>
Yeah, but header doesn't run asynchronously does it? Plus, even if the
"header" script returns, that exit() call will abort the parent script.
Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com