0

I need to use exec() in my PHP script to call another (long-running) PHP script.

I am currently trying to create a couple of simple test scripts, but I end up with an infinite loop.

test.php is my initial script and simple.php is the script that it calls via exec().

simple.php:

<?php
error_log('simple.php called.');
echo 'OK';

simple.php works fine if I go into Terminal and execute:

php tasks/simple.php

But when I try and call it via exec() in test.php, I end up with an infinite loop. The code shows the commands I've tried, along with the result for each one.

test.php:

<?php

error_log('test.php called'); // Test code to check infinite loop

$cmd = 'dir'; // This works!

//$cmd = 'php tasks/simple.php'; // Loops forever

//$cmd = 'php tasks/simple.php >/dev/null &'; // With this, the browser doesn't get stuck in an infinite loop, but simple.php is called thousands of times

//$cmd = 'php tasks/this-script-does-not-exist.php';// Loops forever

//$cmd = "php /home/public_html/my_test/tasks/simple.php"; // Loops forever

//$cmd = 'php /home/public_html/my_test/tasks/simple.php'; // Loops forever

//$cmd = 'php-cli tasks/simple.php'; // $return_val: 127. $output:
/*
Array
(
)
*/

//$cmd = "php-cli /home/public_html/my_test/tasks/simple.php"; // $return_val: 0. $output:
/*
Array
(
    [0] => X-Powered-By: PHP/8.1.31
    [1] => Content-Encoding: gzip
    [2] => Vary: Accept-Encoding
    [3] => Content-type: text/html; charset=UTF-8
    [4] => 
    [5] => �UWuP\ק㡻a����n�c�٥�xĥ��nYB��nD@��W�]`��}��o��ǝ3w~'��~@E 80���%���'��]��`�@�,@9 �)���=6"vE����o00�*�*0�2z,-�DX����_����R�q�u��q���y��!D�",��˭����?�@�����e``��;��K������4���8��r��i��DX��e��ui�rB
    [6] => ���4!鷁3��J�Y���?�5��L�](.R�]E��VBO�_�}^���\�{f�ȼ��Ez�N:��7����fU�w�6
�NK�r��:�]�~������R�Qc}V=<�hdj�l�.:������۷��e{`�NK'�P#�p7�'y��m#B*k*��T��׏�:±&o�^��X}�v����KO�dt� ������mߤo��ĸ��3\��b�_p���    H��Et6�&`���{�H�\ޜ�C
��'\�$�X�������M��NZ;��m��6���e�M�{��8�l����+��۸�t�7�ڗ��mu��AN0��u-�1}�l��sn�Oe)��c��(h���z""r��t���c!4���E����ϗ+�e�Ԧ飓C��Թ��ɊW�˖�F�^�+��n?=E�fz\��B��ȟ�x����D뀢:��Xc@xʱ��Qñw�0,�+uP5��7~�����?��F��x��&�?8�O3�(�e�\�\eE����.T1)Cm�A��5�%w�#�7�ɴ�(�'^¥�r�����@��*��e��B��G2����r�K$��٘A����Bvd��2���N��_YD�X���L����TՅ��9_��Z�_?o���B��ќ]�M㐖(���P]�P#;ԃlbd�Q�����"k   ��(�cs��k�A"E]��N��_M<�5�u;��,�z ������,����0%�\�����-�e?Dj����>u���o�   f5��Ѿ\wQ�N�׮�za�
Eb47�����u5��s*�6}�H8U�!<�mzcVc�/��?���O�R�䎾�xg��_s��u<<�o����On/���O;�p'�q;%]��JM�B|9���W����<�����N3�6��؁�$���״�=������i�?�}���F$��e�J��d�]~�7��&l�L��k�����gAk�q&��!��H��F�$������HS���&jDNT�tk�/i�K�X��RL��tL��}�Y���~(*��L.����x��)N���bV1�ΐ�u��N;H�f%��i�!���I��L�՟�va����c�A�t�Ր��J�w�d�.=�N�SDe(����7�J��%"�7̻�Ё#x�8ld�m̟�������&:Nߑ��h*��̙�ܞV��t���H[���~�r/�v��u��o��
#����Ϳ?e\�1x��U~ud��6������d�;@T����x�dG�S�T?��缫�F��b;J%bK#b�a
�}
��s�X��`}w��;���o5�zT�2B�����U5{�'|�(R;��f��}N.7w���*\�����gT��
    [7] => �prS���&�����ID�E-ײf�\j-��"�`0���YFP��������֠D�����eAk+{ж��R7ʹj׉��w{�-l4>�L�Z�%�͌�w殿��B�`�A%6\񐄟��Sj��.�r�����삵o0?��9�1i���7�wd¯*�o���L활��W�
    [8] => �����&,�:b�np�k�`���M�Gϼ��L\4�>z,���^%�r�4��x���=f,v�   �1z+���t��рC���w�*�<��*�sU�^�x�u�8�h����1W�҂��ϧV�ԏdu����T_�%���?ix�WD� w��9˴A��C}j�BߥiBS�Bz�;;�'��뚐�^��9������1AXV��7-��%����ב�O��Rjc������v�d�&��,���fႣ�������,�  �\��V�
    [9] => >y����Z�!��=��=��=�!T%��۳Ic�4��5��PucJ�?,�D%菱�����w�^��V0�����0.�C�7���WK˳Jv�������c���X0�L�4��M���]������h���R�7�����y�#!Ѧ�~�1|�>?��3  o��3�\����,��_��<�\�ޚ�B{����j�gB��߻Ab�,��v_��ڄ�ܦa>F-j��5�R����ejԐ��l
    [10] => �Nj��=��dW(�ʔP�A~NJ��R��T[/C��o��u�օ@�,NL�o����
    [11] => ͧ�^r�ܫ��A��n""e�6č)���Ck|���Y��h59�b�n���4�6Xq�    I��'`��Z��%��b"H���<�a���QZ���^�t/�H��811��5��>��;���wxs,��������ɬ���u)qS8,��W���II�v�o'�{�E��Js��P���^��v�E3CS��x���2���F��[���m
�����
    [12] => Q~&�������?�,�I��{�����T���{�@�b�wH��S��I�4W6M�oW��D��Q��R�E�   #� (M_��LS�Z���J��!�c�Z*��k�P�)E�,7_ߐ]n-�eLI�}L�z�[����| Y.O��k�/O�\���L|"?GG��A�wu�G�+����R�(�����v���?]~�,w4� ���0��/l'��J`�pu��<�:�Kԛm�<��m���`2�����bi���0<�!�Ы{�����O��Y��?���_9��q��\o�ySo��-[p�KM·��p5K�UIh�r5��N7���8;���I�w55$����˼��3�F��-f�N�9���Ԫ�l���\���&��X1�|��^��p&�,q!#�B7o�+�W�}����6�`i]r�l���&Q�Wa]d^�����>�    �+���qb=B�<�t����O��&��F����_�wG5��G�n.ɉ�ւ��4�?���b?���Π�fY�xM�j`��ae6։oqd�
    [13] => >�Y��T�0����YIe]�z�86B���(�Ήj!��Lq�f#�~�U��<\r:Ωf,��8�4ţ����)M�j1T�7��ч|z��@&�1D(¦3��r6mվ�g�ɂ�>U��ƈ@����C�~�Y�?��
����|g����B�Y����c�H(��k�]Gp����
<�d��#��Uɺ/��Ј4��f������(��%Y ����t��8J+��*_�_�n���r9�1y#��B;�!5#I�a0n�*��U�~�H�^���o(�N��K�o����U�:�Q{���ON
    [14] => ���8.Ֆ�Y� ����*)���u1���\���ʿ�/�VD[�b��n<��e��2�4�eJ�b|�?kt�7�b(�����i5Xݯmw����pn������M+�l#�A�>�~1~<�w"՗��?�l?�k11/{L����"�,�*�G����.�S��B�飕�h��k�K���x�w}xZ`v���,��#9<��Ȉ�=����Z?oOc��:+OA��R�$� +v�M{
�Ԙ�QsI��9k�TF��U*oV}��|�F��Z��&����]R&�a��|������ŅKNS��?4r���~�M�"�G� ��S}����7D����S:���*Uk�<���;��\�!;B5F�tt�|�ʰ�!h�j$�; ��=��`[{a�%<�zyt���k�%'u�������u]D��.�n�¢Ӽ��@����?wK��~�@�
)
 */

//$cmd = "php /home/public_html/my_test/tasks/simple.php"; // Loops forever

$output = null;
$return_val = -1;
exec($cmd, $output, $return_val);
echo "<pre>Return val: " . print_r($return_val, true) . "</pre>";
echo "<pre>Output: " . print_r($output, true) . "</pre>";

This and this haven't helped. Any ideas?

9
  • 1
    Although it is completely valid to do so, is there a specific reason that you are using PHP to invoke another PHP process (using a different SAPI), instead of just running the PHP code? Commented Mar 20 at 13:21
  • When you say "loops forever", do you mean that you are being flooded with error_log messages (a true loop), or that the code just never exits? Commented Mar 20 at 13:22
  • The task script takes a long time to complete and causes the browser to timeout, so i I call it via exec() instead. Yes, the error log file is flooded with "simple.php called." - about 10 per second. Also, only when I append ` >/dev/null &` to the command does the browser complete, otherwise it is continually waiting. Commented Mar 20 at 13:49
  • php and php-cli can be defined per-user, so it might help to figure out what those means when run by the web SAPI. Try running var_dump(exec('which php')); through this to see where it points, and maybe try it with php-cli, too Commented Mar 20 at 14:08
  • 1
    From your regular CLI that is working, try running php -r "var_dump(exec('which php'));". If that is different than the web's version, you could try hard-coding the web version to run using the path from your known-working one. Commented Mar 20 at 16:19

1 Answer 1

1

Thanks to Chris's invaluable help (see comments, above), the underlying issue was that `which php` was returning different PHP paths when executed in Terminal as compared to when executed on my web page.

With the Terminal PHP version working as required, the solution was to use Terminal's PHP path in my web script code. So...

$cmd = 'php tasks/simple.php'; // Loops forever

...became...

$cmd = '/usr/local/bin/php tasks/simple.php'; // Works!
Sign up to request clarification or add additional context in comments.

1 Comment

Incidentally, I had the original problem with PHP 8.1.31 and with PHP 8.2.27 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.