Shell_exec nohup with nohup.out - php

Executing this code:
shell_exec('nohup command&');
or this
shell_exec('nohup command > /path/to/nohup.out 2>&1&');
But there is no nohup.out in both cases. How can I run nohup with nohup.out via php?

shell_exec('nohup command > /path/to/nohup.out 2>&1&');
This line will work. Just make sure you have write permissions for the output folder. Consider your output folder is /usr/nohup-out
ls -l /usr/nohup-out
It should have write, read and execute permissions (rwx). If not, do this:
sudo chmod -R 777 /usr/nohup-out
Now, try to execute php file. It should create a nohup file in /usr/nohup-out folder.
Sample script and result:
1. date.php:
<?php
shell_exec('nohup date > /usr/nohup-out/nohup.out 2>&1&');
?>
2. Execute php from terminal:
php date.php
3. nohup.out content after execution
Thu Apr 23 11:30:28 IST 2015

Related

Run sh file from php with cron

I have an sh file with file-removing commands.
I run it from php like this:
shell_exec("sudo -n ./truncatefiles.sh 2>&1");
Thats works fine if I open the PHP file from browser, but doesnt work from scheduled cron tab.
PHP user: www-data
If i run whoiami from cron, returns same: www-data
I added this to my visudo:
www-data ALL=(ALL) NOPASSWD: /www/sites/..../importscript/truncatefiles.sh
Shell exec for this sh file returns (from cron):
sudo: sorry, a password is required to run sudo
Why works it dirrefent way in cron?
What should I do for get it work?
PLease try to do the following,
Try to log your output from crotab to a file,
* * myscript.php >> /var/log/myjob.log 2>&1
This way you can debug your script.
1. Also the check the user and permissions for your shell script, php file.
2. try with sudo crotab -e

shell_exec wont work from crontab

I have looked at other answers they dont fit to this case.
I am using the full path to the file. Code I copied is simplified.
run.php contains:
shell_exec("php /var/www/html/sync/chourly.php $position $quotientx > /dev/null 2>/dev/null &");
if I use manually php run.php - it works great.
here is the line on crontab -e :
05 * * * * /usr/bin/wget -O /dev/null http://sync.eeeww.com/run.php
again the file run.php starts BUT chourly.php doesn't start. I am using centOS 6
any suggestions please?
Addition: I checked the permissions I am using ec2-user to run php run.php and crontab is using the same permission. it is able to run the file but shell_exec is where the issue occurs
Is /var/www/html/sync/chourly.php using $SERVER['DOCUMENT_ROOT'] ? Since you're explicitly calling the php interpreter (not mod_php), a `$SERVER['DOCUMENT_ROOT'] call will not work as you expect.
Try manually running the cron from shell to see where it's failing.
cd /
su - your_httpd_usersame -c "/usr/bin/wget -O /dev/null http://sync.bitpine.com/run.php"

Exec to launch php as background process

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).

How to execute a makefile in a php file

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.

Run Multiple PHP files using PHP Command Line or SSH

I have 137 php files i want to run them in one command (in parallel) not by sequence.
But the problem is each file is taking 2-5 seconds.
So i have tried to make a (.sh) file and put each line as :
/usr/bin/php /files/file1.php
/usr/bin/php /files/file2.php
/usr/bin/php /files/file3.php
It will complete file1 and then run file2 and file3 by sequence.
So please what is the php or sh command to run 137 php files all in one click (parallel).
You put them in background.
for ($i=1; $i<=137; $i++) {
exec("/usr/bin/php /files/file$i.php > /dev/null 2>&1 &");
}
Run the scripts in the background by adding 'nohup' and '&'
nohup /usr/bin/php /files/file1.php &
nohup /usr/bin/php /files/file2.php &
nohup /usr/bin/php /files/file3.php &
You can use pcntl lib , that enables threads in php, you can use that and create a php-master file that will call other , and then you can master file from command file

Categories