The Little Guy Posted December 26, 2008 Share Posted December 26, 2008 I was reading about system in the manual, and it seems like it allows for running programs in the background, this way I can run 10 versions of the same program (if I wanted). Note: If a program is started with this function' date=' 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.[/quote'] So my questions are: 1. How do you output to a file? 2. Is there a better way to do this? 3. Does system actually do what I am thinking it does? Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/ Share on other sites More sharing options...
The Little Guy Posted December 26, 2008 Author Share Posted December 26, 2008 Anyone know? Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724044 Share on other sites More sharing options...
hobeau Posted December 26, 2008 Share Posted December 26, 2008 hi 'The Little Guy', I believe the function you are looking for is 'popen'. If you want to run a process in the background here is a snippet of code that may help: <?php function callTool ($path,$file) { chdir($path); $call = $path.$file; pclose(popen('start /b '.$call.'', 'r')); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724127 Share on other sites More sharing options...
The Little Guy Posted December 27, 2008 Author Share Posted December 27, 2008 not sure, I want to run ffmpeg. will that work? Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724364 Share on other sites More sharing options...
hobeau Posted December 27, 2008 Share Posted December 27, 2008 the function I just gave you will start a program and run it indefinitely in the background processes. It doesn't look like that's what you want to do with ffmpeg. I'm guessing that you want to run that program to compress video etc. In the case of popen, this allows you to run a program indefinitely, thats not what ffmpeg will be doing. Since ffmeg has a command line interface you could use 'system()' to run the program and give you the return value to verify that it worked correctly. For instance: // $last_line will return the very last line of the return response $last_line = system('dir', $retval); echo ' </pre> <hr />Last line of the output: ' . $last_line . ' <hr />Return value: ' . $retval; This script on a windows server will give you a list of files and folders in the current directory. You could probably use this same code with ffmpeg, just replace the dir with the ffmpeg commands, and read the $retval to make sure everything executed successfully. I've not tested this with ffmpeg myself, but i know that this code works with other command line executables so this should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724432 Share on other sites More sharing options...
The Little Guy Posted December 27, 2008 Author Share Posted December 27, 2008 shell_exec("/usr/bin/ffmpeg -i '$f' -vcodec flv -f flv -r 29.97 -s {$resX}x{$resY} -aspect 4:3 -padtop {$padTop}px -padbottom {$padBot}px -maxrate {$bitr}k -g 160 -cmp 2 -subcmp 2 -mbd 2 -flags +aic+cbp+mv0+mv4+trell -ac 1 -ar 22050 -ab 56k '/home/ryannaddy/flv.dudeel.com/flv/{$fileName}.flv'"); Here is my current command. What would I do with this to run this command simultaneously on 2+ files? Sorry, I'm having confusion... Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724647 Share on other sites More sharing options...
laffin Posted December 27, 2008 Share Posted December 27, 2008 if yer using *nix use the & doohickie, if yer using windows, ya need another app to start the app in the background. php will not run apps in the background, that for the OS to do. Shell/exec return after the command is finished, reason ya use the & to put the app in the background in *nix machines. Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724648 Share on other sites More sharing options...
The Little Guy Posted December 27, 2008 Author Share Posted December 27, 2008 if yer using *nix use the & doohickie, if yer using windows, ya need another app to start the app in the background. php will not run apps in the background, that for the OS to do. This is a Linux Server, and according to PHP.net php can tell the OS to run something in the background. Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724673 Share on other sites More sharing options...
hobeau Posted December 28, 2008 Share Posted December 28, 2008 if yer using *nix use the & doohickie, if yer using windows, ya need another app to start the app in the background. php will not run apps in the background, that for the OS to do. Shell/exec return after the command is finished, reason ya use the & to put the app in the background in *nix machines. Sorry laffin, I've definitely run applications in the background using php. Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-724717 Share on other sites More sharing options...
The Little Guy Posted December 29, 2008 Author Share Posted December 29, 2008 OK, I don't think my question has been answered... If I run system, will it be able run two+ ffmpeg commands at the same time, without having to wait for the other command to finish? Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-725158 Share on other sites More sharing options...
The Little Guy Posted December 29, 2008 Author Share Posted December 29, 2008 I got it! (I think) at the end of the shell_exec() command, I just add this: >/dev/null 1>/dev/null 2>/dev/null & and it works! Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-725173 Share on other sites More sharing options...
The Little Guy Posted December 29, 2008 Author Share Posted December 29, 2008 OK, now that I got that, another question... how can I get the code to stop after the foreach loop and wait for the files to convert? Example: $fls = array(); $fls[] = '/home/ryannaddy/DudeelVideo/[email protected]/iBfH0qsrowWuS.mp4'; $fls[] = '/home/ryannaddy/DudeelVideo/[email protected]/xxxb3RFoljeN7.mpeg'; foreach($fls as $f){ shell_exec("/usr/bin/ffmpeg -i '$f' ... 'hq_{$fileName}.flv' >/dev/null 1>/dev/null 2>/dev/null &"); } // Wait for files to convert to flv when done continue... foreach($fls as $f){ // run a command to make images (current command works). } Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-725183 Share on other sites More sharing options...
corbin Posted December 29, 2008 Share Posted December 29, 2008 There isn't a way to do that. You could keep checking for the processes using ps, but unless this will only run 1 script at a time, that won't work. Other wise, the only way I can think of is to check if the output file exists. ffmpeg prob creates the output file immediately, but you could use a tmp name with a rename cmd then check if the rename has happened. Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-725196 Share on other sites More sharing options...
The Little Guy Posted December 29, 2008 Author Share Posted December 29, 2008 OK, thanks! that sounds like a good idea! Quote Link to comment https://forums.phpfreaks.com/topic/138439-exec-system/#findComment-725353 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.