Run Script in the background of Linux Server with PHP exec ()

I am trying to run a PHP script to run in the background using the exec () function, but I cannot get it to work. I read countless posts about stack overflows and other forums and tried many options to no avail.

Server Information:

Operating System: Linux PHP: 5.2.17 Apache Version: 2.2.23 Home Directory: /home1/username 

I am currently using the code:

 exec("/home1/username/php /home1/username/public_html/myscript.php > /dev/null &"); 

When I run the above script, I don't get error_log and no errors in my cPanel error log, however the script is definitely not executing. When I browse http://www.mydomain.com/myscript.php it starts up and instantly sends me an email. Any idea why this is not working / how can I find out what error is occurring?

CPanel Process Manager Output Update

 exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &"); 

It produces:

 27183 php /home1/username/php /home1/username/public_html/myscript.php 27221 [sh] 27207 php /home1/username/php /home1/username/public_html/myscript.php 27219 php /home1/username/php /home1/username/public_html/myscript.php 27222 php /home1/username/php /home1/username/public_html/myscript.php 27224 php /home1/username/php /home1/username/public_html/myscript.php 27249 sh -c php /home1/username/php /home1/username/public_html/myscript.php > /dev/null & 

This is normal? The script seems to twitch for a long time, although it should run very fast.

+4
source share
3 answers

Could not get exec to work with php. Even when I got access to the server on the server, the command just hung. I decided to use wget instead, which does the same thing. Works great :)

 exec("wget http://www.mydomain.com/myscript.php > /dev/null &"); 
+4
source

Have you tried calling the PHP-CLI directly?

 exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &"); 

You do not need # !, which will be displayed in the browser if called through Apache.

EDIT . It looks like your script is running, but your PHP script running in the background is hanging (not coming out). Try this option:

 exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null 2>&1 &"); 

What does "> / dev / null 2> & 1" mean?

+1
source

since you want to run myscript from the command line do not do this:

 exec('(/home1/username/public_html/myscript.php) > /dev/null &',$r,$s); 

And write this as the first line in myscript.php:

 #!/home1/username/php -n <?php //script goes here ?> 

That should work. The hashbang tells the system which program to use to run the script, which follows, so you do not need to add this to your exec call. In addition, it is safer (and therefore better) to put brackets around a full script call, so that PHP knows which output should be redirected to which thread, in order to avoid any problems that might arise. Especially when libraries or packages such as PHP-GTK are installed on the server (hence the -n option).

0
source

Source: https://habr.com/ru/post/1435714/


All Articles