E
E
entermix2019-01-20 21:15:40
linux
entermix, 2019-01-20 21:15:40

What is the correct way to run an external script in the background?

There is PHP 7.2 (CGI), for example, we add 2 simple scripts:
sleep.php:

<?php
sleep(30);

background.php:
<?php
exec("su -s /bin/sh - user -c cd /var/www/user/data/www/example.com/ && /opt/php72/bin/php -q /var/www/user/data/www/example.com/sleep.php > /dev/null 2>&1 &");

Console:
cd /var/www/user/data/www/example.com/ && /opt/php72/bin/php -q /var/www/user/data/www/example.com/background.php

In the background.php file, we run the command in the background, but the script freezes for 30 seconds, why is this happening?
The documentation says:
Note:
If you are going to use this function in a program running as a daemon, make sure that the standard output of the function is directed to a file or other stream, otherwise PHP will hang until the end of the program.

Environment problem? How is it configured? What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bkosun, 2019-01-20
@entermix

background.php:

$command = "(su -s /bin/sh - user -c cd /var/www/user/data/www/example.com/ && /opt/php72/bin/php -q /var/www/user/data/www/example.com/sleep.php) >/dev/null 2>&1 &";

exec($command);

https://florian.ec/articles/running-background-pro...
https://stackoverflow.com/questions/14541741/how-d...
https://unix.stackexchange.com/questions/3886/ diff...

B
Boris Syomov, 2019-01-20
@kotomyava

Everything works as it should, you just expect something strange.
The call to exec blocks until execution is complete.
The chain of calls via && is executed sequentially.
To spawn individual child processes, you can use pcntl_fork and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question