I am struggling with getting a php file to run in the background with PHP's exec(). As a first test, I tried :
exec("ls -l > logfile.txt 2> errfile.txt &");
That works fine. logfile.txt gets filled with a directory listing.
Per instructions in the php documentation, since the exec kicks off a process that runs in the background, standard out (and standard error) are redirected to a file.
Now, I try
exec("/usr/bin/php -f /path/to/my.php > logfile.txt 2> errorfile.txt &");
It appears nothing happens.
Here are test files that I'm trying:
alpha.php
<?php
$version="a";
// Go do something we do not need to wait for.
exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &");
?>
<html>
<head><title>Test</title></head>
<body>
<p>This is Alpha version <?php echo $version; ?></p>
</body>
</html>
beta.php
<?php
if (!($fp = fopen('/home/johnst12/public_html/workshops/admin/betadata.txt', 'w'))) { exit;}
fprintf($fp, "Proof that Beta executed.");
fclose($fp);
?>
If I run beta.php directly, it works fine. Betadata.txt gets the message.
If I run alpha.php to launch beta.php, betadata.txt is not created. logfile.txt and errorfile.txt remain empty (expected).
I am sure that the path to php, and the path to my php file are correct.
Googling for clarification has not been fruitful. A couple of common themes seem to be (a) running out of resources? (b) lack of permission on the target php file? Out of resources seems unlikely. The permission on the script is global read 644 (rw-r--r--). I tried adding execute (755) just in case it would help. It made no difference.
PHP version 5.3.21
Linux/Apache system.
safe_mode Off
What am I missing? Thanks.
First of all : Have you verified that /usr/bin/php is the correct path to PHP?
Php doesn't like running like that. Something to do with stdin. Try with nohup:
exec("nohup /usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &");
With -f anything else that looks like a flag will go to PHP, so if you wanted to pass a "-x" option to your script then you'd have to
/usr/bin/php -f /path/to/beta.php -- -x
Without, options before the filename go to PHP and after go to the script.
/usr/bin/php /path/to/beta.php -x
I assume you've already looked at the two files in case they have output or errors?
A few other things to check:
Delete the two files. Are they recreated each time this code runs?
exec("nohup /usr/bin/php -v > logfile.txt &");
should output version information to that log file.
exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt");
should run the script properly (but not in the background).
Related
I want to start Hadoop daemons with PHP instead of terminal.
When i do start-dfs.sh, it works well.
The PHP code I'm using is
<?php
echo shell_exec('/usr/local/hadoop3/sbin/stop-dfs.sh');
?>
the output of the above code is
Starting namenodes on [localhost] starting datanodes. Starting
secondary namenodes [chbpc-VirtualBox] .
but when i type the url http://localhost:9870/dfshealth.html#tab-overview in the browser (to check the status of my Hadoop), it actually did not start despite having the output above.
Answer
<?php
echo shell_exec('/usr/local/hadoop3/sbin/stop-dfs.sh > /dev/null &');
?>
Description
When you run the daemon process with shell_exec, you should detach from this process for finished your shell_exec command, but you should stay daemon process in progress state.
& - provide doing this.
Also instead /dev/null you may be using your custom log file and all info from stop-dfs.sh will redirect to this file.
My test for this solution
Copy this snippet to your bash console:
cat <<EOT > daemon.sh
while true; do date; sleep 1; done
EOT
chmod +x daemon.sh
cat <<EOT > daemon-runner.php
<?php
echo shell_exec(__DIR__ . '/daemon.sh > daemon-log &');
EOT
touch daemon-log
php daemon-runner.php
tail -f daemon-log
PS
You using stop-dfs.sh script name for start your hadoop, also maybe you mixed up script name with start.sh?
I need to run a python script, compiled with pyinstaller via a PHP generated webpage.
I tried shell_exec(), exec() and system() without success.
I regularly run the script from terminal in background using:
temperature_sensor_code > /dev/null 2>&1
I've added www-data user to sudoers. I know it's not a good way but I need it in order to send killall temperature_sensor_code command (this is works).
This is my situation:
<?php
$run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
shell_exec($run);
header("Refresh: 0; URL=index.php");
?>
I've made a symlink in /usr/bin, also tried with the full path of the script with no luck.
UPDATE: to make it simpler, i've created a simple sh script run.sh and put in /var/www and make it RUN with
shell_exec("/var/www/run.sh");
this is working for me. So I put my script temperature_sensor_code in /var/www but this is not working. If I add var_dump(exec("/var/www/temperature_sensor_code/temperature_sensor_code"));
gives me: string(0) ""
I think there are problems with the compiled python script because the PHP side seems to be OK.
escapeshellcmd() does this:
Escape shell metacharacters
$run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
var_dump($run);
string(43) "temperature_sensor_code \> /dev/null 2\>\&1"
But you have shell metacharacters that you do want to behave as shell metacharacters:
temperature_sensor_code > /dev/null 2>&1
^ ^^^^
You're also doing no troubleshooting at all:
You discard all command output (that's what sending it to /dev/null does)
You don't get the return code
I suggest to:
Switch to exec() and make sure you use all its arguments and not just the mandatory ones
Get rid of > /dev/null until you diagnose the issue
It's quite simple, and I'm out of ideas. I'm sure there is a quick workaround.
exec('echo 123 &> /var/log/123.log');
I'm sure it's not about the permissions, because the file 123.log is created, but it's just- empty. I've also tried shell_exec, but it doesn't create the file at all.
Also tried all variants of redirection, i.e. 1> 2> >.
Using PHP to capture the output is not the option, as the output in production is huge, and I don't want to run into memory issues.
Any ideas appreciated.
Btw, I'm using Ubuntu 12.04 LAMP.
Debian and Debian based Linux distributions like Ubuntu are using dash and not bash as /bin/sh by now.
&> is a bash extension, the dash does not know about.
The correct posix-compatible way to write cmd &> file is cmd > file 2>&1
cmd > file 2>&1 works in all posix-compatible shells: dash, bash, ksh, zsh, ash ...
So you need to change your code to:
exec('echo 123 > /var/log/123.log 2>&1');
Try shell_exec without &:
echo shell_exec("echo 123 > /var/log/123.log");
Only thing that did help was to create a shell script with exec permissions, e.g. test.sh:
#!/bin/bash
echo 123 &>> /var/log/123.log
and execute it like this:
shell_exec('[full path to]/test.sh');
So, redirection operator is not important, but everything else is (#! directive, shell_exec).
Is there any way to execute a makefile in a php file? I have tried:
exec('cmd /c "C:\\Program Files\\Microsoft Visual Studio\\VC98\\Bin\\nmake.exe" -f E:\\dev\\temp.mak > process.out 2> process.err < /dev/null &');
But I donot think this way makefile gets to run.
Why not doing :
$make = escapeshellarg("C:\Program Files\Microsoft Visual Studio\VC98\Bin\nmake.exe");
$path = escapeshellarg("E:\dev\temp.mak");
exec("start /B {$make} -f {$path} > process.out 2> process.err");
start /B will execute your program in background
> process.out will redirect standard output to "process.out" file
2> process.err will redirect error output to "process.err" file
In this example, process.out and process.err will be erased each time make is run. To avoid this behaviour, just replace > symbols by >>, and files will be appended.
Try different methods of doing this, Create a windows batch file in the same place as your PHP directory;
cd C:\"Program Files"\"Microsoft Visual Studio"\VC98\Bin
nmake.exe -f E:\dev\temp.mak > process.out 2> process.err
*Incorporating Zids Comment into this: *
How would you execute it from the command line, if you were not using PHP? – rid
If the above method doesn't work. Search online for usage of that exe by running it from windows command prompt, then change the .batch file accordingly.*
Save this as a .batch file, then from your PHP try running
exec ("filename.bat", $output);
then
View the output performed by the exec command in a simple foreach loop
foreach ($output AS $OutputStr)
{
echo $OutputStr."<br>";
}
There should be some output, from the output I would work with that.
I'm trying to trigger a PHP script to run in the background using the exec() function but I cannot get it to work. I've read countless posts on stack overflow and other forums and tried many variations to no avail.
Server Info:
Operating System: Linux
PHP: 5.2.17
Apache Version: 2.2.23
Home Directory: /home1/username
I'm currently using the code:
exec("/home1/username/php /home1/username/public_html/myscript.php > /dev/null &");
When I run the above script I get no error_log and no error in my cPanel error log, however the script definitely doesn't execute. When I browse to http://www.mydomain.com/myscript.php it runs and e-mails me instantly. Any idea why this isn't working / how I can find out what error is being produced?
Update cPanel Process Manager Output
exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &");
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 &
Is that normal? Script appears to hang around for a long time even though it should execute very quickly.
Couldn't get the exec working with php. Even when I got shell access to the server the command just hung. I decided to use wget instead which accomplishes the same thing. Works great :)
exec("wget http://www.mydomain.com/myscript.php > /dev/null &");
Have you tried invoking the php CLI directly?
exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &");
You will not need the #!, which would output to the browser if called through Apache.
EDIT.
It looks like your script is working, but your PHP script executing in the background is hanging (not exiting). Try this variation:
exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null 2>&1 &");
What does “> /dev/null 2>&1″ mean?
since you want to run the myscript from your command line, wy not do this:
exec('(/home1/username/public_html/myscript.php) > /dev/null &',$r,$s);
And write this as a first line in the myscript.php:
#!/home1/username/php -n
<?php
//script goes here
?>
That should work. The hashbang tells the system what programme to use to run the script that follows, so you don't need to add that to your exec call. Also, it's safer (and therefore better) to put brackets around the full script call, just so PHP knows what output has to be redirected to what stream, to avoid any issues that might occur. Especially when libs or packages like PHP-GTK are installed on the server (hence the -n option).