The idea would be for the background task to set a flag in shared memory showing it had started successfully, and the parent process could check for that flag to confirm the background task/s had started, if it times out after a second then the background failed to start. Obviously once it’s running it can set values as well to show progress of the background task/s.
From: Jānis Elmeris [mailto:janis.elmeris@xxxxx]
Sent: Wednesday, 19 August 2020 10:14
To: arno@xxxxxxxxxxxxxx
Cc: php-general
Subject: Re: Starting a background process and reporting failure if applicable
Thank you for the idea!
I understand that some proper background service is an option, that could take its input from a database and do its work independently from the initiator script (I don't really need any response back from the job to act upon, just to know that it was executed without errors).
I just hoped that there is some quicker, dirtier way of just spawning a separate process to not hold the main one, while still being able to monitor if any error has occurred.
Janis
otrd., 2020. g. 18. aug., plkst. 18:16 — lietotājs Arno Kuhl (<arno@xxxxxxxxxxxxxx>) rakstīja:
Would shared memory work to communicate between the scripts?
https://www.php.net/manual/en/book.apcu.php
From: Jānis Elmeris [mailto:janis.elmeris@xxxxx]
Sent: Monday, 17 August 2020 15:15
To: php-general
Subject: Starting a background process and reporting failure if applicable
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
I didn't want for the main script to wait 1 s to determine whether the script was launched...
But, yes, thank you for the comment!
Janis
trešd., 2020. g. 19. aug., plkst. 14:42 — lietotājs Arno Kuhl (<arno@xxxxxxxxxxxxxx>) rakstīja: