php中popen的使用問題

來源:互聯網
上載者:User
我有程式路徑為:/usr/src/myappMamou/apps/myapp
在命令列中,首先啟動程式myapp後,可以輸入命令運行程式如:send arg1 arg2 arg3
這樣可以程式可以運行成功

現在想在PHP中完成上述功能,不需要每次在命令列中輸入命令 :send arg1 arg2 arg3 也能達到一樣的效果

PHP如下:
        $fixedcmd="send 123456063 10000 1111111";        $ph="/usr/src/myappMamou/apps/myapp";        $rs = popen(\"$ph\" \"$fixedcmd\", "r" );pclose($rs);

運行達不到效果,請教大家有什麼問題嗎?非常感謝


回複討論(解決方案)

1、php是通過命令列還是cgi執行,是否執行/usr/src/myappMamou/apps/myapp的許可權
2、 “首先啟動程式myapp後”是說這個程式單獨啟動嗎?能否給出一個send arg1 arg2 arg3 的關係的
3、執行啟動myapp時當前路徑是否必須在一定的位置下?

1、php是通過命令列還是cgi執行,是否執行/usr/src/myappMamou/apps/myapp的許可權
2、 “首先啟動程式myapp後”是說這個程式單獨啟動嗎?能否給出一個send arg1 arg2 arg3 的關係的
3、執行啟動myapp時當前路徑是否必須在一定的位置下?

1、php是放在apache中執行的,具有許可權
2、“首先啟動程式myapp後”指的是在命令列中啟動程式,如./myapp,之後進入程式中myapp>
輸入的命令如send arg1 arg2 arg3,send是命令其它三個是參數
3、執行啟動myapp時當前路徑是否必須在一定的位置下,只有到了/usr/src/myappMamou/apps下才可運行,是不是要弄成系統命令那樣才行,就是在任意位置輸入命令myapp即可啟動程式
請教了

如果我想在任意地方可以執行命令myapp,是不是只要在$PATH中加入myapp的路徑就可以了,這樣系統就知道myapp的路徑,應該就可以識別了吧,但是我試了,無效

$rs = popen(\"$ph\" \"$fixedcmd\", "r" ); ???
咋麼也得寫作
$rs = popen("$ph $fixedcmd", "r" );

你這樣做下測試呢

 array("pipe", "r"),  // stdin is a pipe that the child will read from   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to);$cwd = '/usr/src/myappMamou/apps';$process = proc_open('myapps', $descriptorspec, $pipes, $cwd);if (is_resource($process)) {    // $pipes now looks like this:    // 0 => writeable handle connected to child stdin    // 1 => readable handle connected to child stdout    // Any error output will be appended to /tmp/error-output.txt$fixedcmd="send 123456063 10000 1111111";    fwrite($pipes[0], $fixedcmd);    fclose($pipes[0]);    echo stream_get_contents($pipes[1]);    fclose($pipes[1]);    // It is important that you close any pipes before calling    // proc_close in order to avoid a deadlock    $return_value = proc_close($process);    echo "command returned $return_value\n";}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.