The Little Guy Posted September 24, 2009 Share Posted September 24, 2009 I have a php file that uses the command line to run (cmd). When a particular area of my loop is reached, I want to use exec to run another php file in a new cmd window. My Question: is there any way I can pass variables to the cmd sort of like $_GET? These variables would then be used in that second php file. Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/ Share on other sites More sharing options...
mikesta707 Posted September 24, 2009 Share Posted September 24, 2009 There is an array called $argv that you can use to access arguments passed via the command line. For example if you did the command path/to/php/test.php chocolate 276 "killer tie, dude!" and in that page you had a line print_r($argv); it would output something like Array ( [0] => test.php [1] => chocolate [2] => 276 [3] => killer tie, dude! ) notice that the script name is the first parameter, always there is also a variable $argc which is the count of the array $argv. Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924385 Share on other sites More sharing options...
The Little Guy Posted September 24, 2009 Author Share Posted September 24, 2009 Sweet thank you! Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924389 Share on other sites More sharing options...
The Little Guy Posted September 24, 2009 Author Share Posted September 24, 2009 Is there anyone to keep the code going that ran the exec? I notice that if I have that, it waits till my other code is finished before it continues. When working with linux, I could just do this: > /dev/null 2>&1 It would then run the file, and not wait for the exec to complete it would continue execution of the current file and the other file would just finish when it is done... Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924447 Share on other sites More sharing options...
The Little Guy Posted September 25, 2009 Author Share Posted September 25, 2009 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924600 Share on other sites More sharing options...
mikesta707 Posted September 25, 2009 Share Posted September 25, 2009 I'm not sure, but i read this on the PHP manual Note: 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. system() Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924601 Share on other sites More sharing options...
The Little Guy Posted September 25, 2009 Author Share Posted September 25, 2009 anyone know how to do that on windows? Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924608 Share on other sites More sharing options...
trq Posted September 25, 2009 Share Posted September 25, 2009 cmd > out.txt Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924610 Share on other sites More sharing options...
The Little Guy Posted September 25, 2009 Author Share Posted September 25, 2009 Nope, that didn't work... Here is what I have now: system("C:\WINDOWS\system32\cmd.exe /c php C:\php_files\id3.php 525 > C:\php_files\out.txt"); Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924633 Share on other sites More sharing options...
trq Posted September 25, 2009 Share Posted September 25, 2009 Nope, that didn't work... Thats helpful. What didn't work? The file didn't get written too? Your script didn't run? Your script waits for system to finish? Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924646 Share on other sites More sharing options...
The Little Guy Posted September 25, 2009 Author Share Posted September 25, 2009 probably should have said... My script waits for system to finish. Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-924669 Share on other sites More sharing options...
The Little Guy Posted September 25, 2009 Author Share Posted September 25, 2009 Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-925014 Share on other sites More sharing options...
lemmin Posted September 25, 2009 Share Posted September 25, 2009 Have you tried using shell_exec() instead of exec()? Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-925017 Share on other sites More sharing options...
The Little Guy Posted September 25, 2009 Author Share Posted September 25, 2009 It still does the same thing, just sit and waits till its done. Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-925027 Share on other sites More sharing options...
The Little Guy Posted September 26, 2009 Author Share Posted September 26, 2009 Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-925275 Share on other sites More sharing options...
RussellReal Posted September 26, 2009 Share Posted September 26, 2009 not sure if this will work but try this: <?php $shell = new COM('WScript.Shell'); $shell->Run("cmd /c php whatever/blabla.php flags"); ?> again not sure if it will work, just giving a suggestion, I know for a fact though that the script doesn't hang whatsoever with COM objects, so if it does do what its supposed to it should be a pretty good solution. ~~~ Tested and Works Have fun dude Quote Link to comment https://forums.phpfreaks.com/topic/175415-send-variables-to-cmd-php-file/#findComment-925276 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.