PHP exec with nohup - php

I currently use nohup to run a long php script and redirect the live results to a file using this command
nohup php long_file.php >logs 2>&1 &
so i just go and visit logs file continuously to see the results
Now i want to do the exact same thing using another php file to execute the above command
i tried the above command with php exec and the redirect output doesn't seem to be working,
I know i can just retrive the output using php and store it using any file write function but the thing is .. the output is too long thats why i keep it running on server's background
a similar question :
Shell_exec php with nohup, but it had no answer
any solution ?

Please try with -q
nohup php -q long_file.php >logs 2>&1 &
http://ubuntuforums.org/showthread.php?t=977332

Did you try passthru instead of exec?

You are redirecting STDOUT to a file by using >
This will truncate the file each time the script is run. If two scripts simultaneously redirect their output to the same file, the one started last will truncate the output from the first script.
If you really want to properly append to a log file with multiple concurrent running scripts, consider using >> to avoid having the log file truncated.
Side effect however is that the log file never truncates and keeps expanding, so if the file gets really large you can consider including it in a logrotate scheme.

Related

Execute a windows shortcut from php

I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Please refer this link for your version of PHP: manual

Running shell_exec() in background AND getting its output afterwards

First of all, I would like to point out that this is NOT a duplicate to this or this and other related questions. I am asking how to get the output as well and my question is mostly related to that.
I need to run a bash script from PHP and get its output in PHP. However, doing:
echo shell_exec('script');
dies out on the 60th second due to Apache's FcgidIOTimeout. If I use the following to run it in the background, I won't be able to get the output:
shell_exec('<myscript> > /dev/null 2>/dev/null &');
I cannot:
Modify the bash script;
The code and all the functionality needs to be in a single file (unless I use a temp file);
If I were to write the output of the shell_exec() function to a temp file, I would also need to find a way to verify whether the
process has finished;
Using a database is an overkill which I cannot afford;
My current and only idea is to create a tmp file using PHP and write the output to it, then using top -c | grep <myscript> (with a refresh button) and if it returns true then it is still ongoing. However, I suspect that would not be practical and efficient in most of the time.
It is important that I use a temp file and not actually creating a "permanent" file to write to.
Solution for a similar problem: A few years ago I had a similar problem. I had to upload a file to a FTP server. I wondered how to communicate to the FTP server that the file was uploaded completely, so that the FTP server can perform some tasks on it. My solution was to rename the file to something like *.completed after it was uploaded completely. Then the process on the FTP server could look for *.completed files only.
Solution adjusted to your problem: I'd suggest you to rename your temp file after it was generated by your bash script. This way you can independently find out if the script was executed successfully. shell_exec() could look like this:
shell_exec('<myscript> > tmp-file && mv tmp-file tmp-file.completed &');
Be aware that this only redirects the channel STDOUT into the the tmp-file. If you also want to redirect STDERR into the tmp-file, try this:
shell_exec('<myscript> &> tmp-file && mv tmp-file tmp-file.completed &');

Call Shell Command from PHP without considering output

I'm trying to write a cronjob which launches multiple processes that I want to run in parallel.
I'm using a foreach calling each command, but the command line waits for the output. I don't want it to put.
Was wondering if anyone ever used any library for this?
Add an ampersand after the command:
$ php task.php &
It will run that instance of php in the background and continue.
If you read the manual on passthru you'll notice it tells you how to avoid this...
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
So you can rely on UNIX fds to redirect output to something like /dev/null for example if you don't care about the output or to some file if you do want to save the output and this will avoid PHP waiting on the command to finish.
pssthru("somecommand > /some/path/to/file")

php script execution for heavy script file

i have a project which i develop in php framework named codeigniter. I have a script file in it which creates 30,0000 pdfs of db records for my client. the problem is that i have to run code in chunks. Because it gives errors, some time server time out, some time memory out of sync, I fixed all errors by changing in php.ini but in vain. Because I still have to make pdfs in chunks by using limit with 7000 ofset. IF I increase Limit offset it fails to run and gives me error. It's very hard to sit upto 30,0000 record and generate pdf on them. I just want that I run within one single chunk. Please give me solution. I really need it. Thanks in advance.
I would recommend using separate shell script for this, or if you insist on using a PHP script, use that as you would use a shell script. You can just "fire and forget" the script, so it would run on the background, without it affecting anything else and without it eating resources off your web application. That would be something like:
shell_exec('yourscript.sh > /dev/null 2>/dev/null &');
or with php script
shell_exec('php yourscript.php > /dev/null 2>/dev/null &');
Note that in the above, I'm redirecting both stdout and stderr to null. If you wan't them, you'd rather redirect them to somewhere else.

Is it possible to send FFMPEG output to a php file?

I want to send ffmpeg output to a php file so I can use a regex and update the output into a database. This will allow me to handle progress for multiple uploads. Does anyone know how to do this? Can it be done? Currently I can execute a php file with parameters after the ffmpeg command, and get ffmpeg to write to a txt file but can I send the output to the php file and execute it?
execute php file with parameters
&& php /opt/lampp/htdocs/xampp/site/update_db.php ".$parameter1." ".$parameter2.";
Write output to txt file
ffmpeg command and filepath to converted 1> /home/g/Desktop/output.txt 2>&1
Can something like this be done?
ffmpeg command and filepath to converted 1> php /opt/lampp/htdocs/xampp/site/update_db.php ".$output." 2>&1
Yes, you can read STDIN.
http://php.net/manual/en/features.commandline.io-streams.php
If it were me, I'd just execute FFMPEG from within PHP. You have a bit more flexibility that way, but I know that isn't desirable for every application.
You could use exec to call ffmpeg, then use the content of the output parameter to get returned output.
But doing so only allow you to get the output once the program execution is terminated:
If a program is started with this
function, in order for it to continue
running in the background, the output
of the program must be redirected to a
file or another output stream. Failing
to do so will cause PHP to hang until
the execution of the program ends.

Categories