php中判断shell_exec执行结果

   在做第三方登录的时候,注册时头像下载比较耗时,所以改为了异步 shell_exec 执行wget 

   不过shell_exec执行成功无输出  和  执行失败 返回的都是null 在记录错误日志的时候区分就成问题了。


   在php手册的评论中找到了区分的方法,在此记录一下。

   

$shell = "wget -O despath sourcepath &&  echo 'success' ";
$shellExec = shell_exec($shell);
var_dump($shellExec);

  这样 当前面的执行成功的时候 会执行echo 执行结果就是success 而不是空了 

  执行失败不再继续执行echo  执行结果仍旧是null 

  这样就区分除了执行结果,记录对应日志就OK了。

  补充一下,大概是这样的。。。

 

 $shellExec = shell_exec("cd ../");
 var_dump($shellExec);//NULL
  
 $shellExec = shell_exec("cd ../ && echo 'suc'");
 var_dump($shellExec);//string(4) "suc "

发布了12 篇原创文章 · 获赞 0 · 访问量 3万+
展开阅读全文

Php执行脚本后读取日志内容

09-02

<div class="post-text" itemprop="text"> <p>I have a php script that creates a shell script which is run after making it from the same php file, the shell script generates a registry file that I need to read after the script is executed, again from the same php. The php reads the file, but I think it does it before the file is filled or created, if I go back at the browser and execute the php again, then there is content at the textarea. I have tried to solve it adding sleep(), exit() functions and some other strategies but no success. Here are some of the things I've tried:</p> <pre><code>// Creation of the shell script: Corpus alignment target to origin ...... $cmd = "cwb-align-encode -r $REGDIR -D $CORPUSLOCATION$corpusname/$corpusname"._."$lang_tg.align "; file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX); // Run the corpus indexation script $cmd = "/bin/bash $scriptfile > /dev/null 2>&1 &"; shell_exec($cmd); </code></pre> <p>Read the registry file from the same php:</p> <pre><code>// 1st try: no content at the textarea echo "<textarea id='txtArea'>".htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>"; // 2nd try: no content at the textarea echo "<textarea id='txtArea'>".sleep(10); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>"; // 3rd try: no content at the textarea echo "<textarea id='txtArea'>".exit(); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>"; // 4th try: no content at the textarea echo "<textarea id='txtArea'>".if(filesize($REGDIR.$corpusname) != 0) { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); } else { exit(0); sleep(10); htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); }."</textarea>"; </code></pre> </div> 问答

如何格式化shell_exec返回的值

01-25

最近通过shell_exec执行zipinfo命令,得到对应的zip压缩包里面的文件列表信息,返回的是字符串,如何格式化输出返回值为数组形式? Archive: 222.zip Zip file size: 1358592 bytes, number of entries: 69 -rw---- 6.3 fat 33897 bx defN 12-Jul-09 23:47 PEAR.php -rw---- 6.3 fat 34308 bx defN 12-Jul-09 23:47 chmod.define -rw---- 6.3 fat 2102 bx defN 12-Jul-09 23:47 chmod.php -rw---- 6.3 fat 32 bx stor 12-Jul-09 23:47 config.php -rw---- 6.3 fat 2356 bx defN 12-Oct-17 04:02 copy.php -rw---- 6.3 fat 1991 bx defN 12-Jul-09 23:47 delete.php -rw---- 6.3 fat 1659 bx defN 12-Jul-09 23:47 dirinfo.php -rw---- 6.3 fat 371 bx defN 12-Jul-09 23:47 download.php -rw---- 6.3 fat 1807 bx defN 12-Jul-09 23:47 editor.php -rw---- 6.3 fat 609 bx defN 12-Jul-09 23:47 exit.php -rw---- 6.3 fat 2788 bx defN 12-Sep-23 10:50 fileinfo.php -rw---- 6.3 fat 3525 bx defN 12-Oct-17 03:46 function.php drwx--- 6.3 fat 0 bx stor 16-Jan-25 09:54 geshi/ -rw---- 6.3 fat 16993 bx defN 12-Jul-09 23:47 geshi/apache.php -rw---- 6.3 fat 6038 bx defN 12-Jul-09 23:47 geshi/asp.php -rw---- 6.3 fat 10457 bx defN 12-Jul-09 23:47 geshi/c.php -rw---- 6.3 fat 10103 bx defN 12-Jul-09 23:47 geshi/cpp.php -rw---- 6.3 fat 9900 bx defN 12-Jul-09 23:47 geshi/css.php -rw---- 6.3 fat 3016 bx defN 12-Jul-09 23:47 geshi/gettext.php -rw---- 6.3 fat 6824 bx defN 12-Jul-09 23:47 geshi/html4strict.php -rw---- 6.3 fat 7975 bx defN 12-Jul-09 23:47 geshi/html5.php -rw---- 6.3 fat 50326 bx defN 12-Jul-09 23:47 geshi/java.php 22 files, 3552318 bytes uncompressed, 1349142 bytes compressed: 62.0% 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览