donglian1982 2019-08-08 18:09
浏览 336

如何在PHP中使用shell_exec运行带参数的.sh文件? [重复]

This question already has an answer here:

attempting to run a shell script using PHP. the shell script contains: 'cd /data/nfsshare; python /data/nfsshare/kron_hull.py> /data/nfsshare/log.txt' that is running a python script named kron_hull.py

the php code im using is:

if ($_POST['visual']) {
  shell_exec("sh test.sh");
  echo "working! woot woot";
} else {
  echo "not working";
}

i am using the echo's as a way to tell if its running the if statement correctly, and i am able to successfully echo "working! woot woot", but the shell file does not run, because the script is meant to create a new file on the server, which it does not.

the shell file works when you put it directly in the linux terminal as "sh test.sh" so the issue is not with the shell file.

i also ran shell_exec('whoami'); and shell_exec('ls -lart'); and was able to echo both of those, so i don't believe it is a problem with the shell_exec() command.

my problem is: i dont believe the shell script is even running when put into the shell_exec() command, and I dont know a better way to check besides looking for the new file its supposed to create, which i get none.

any help is appreciated, if you need me to do anything for testing, or need more information, lmk and ill be glad to answer.

i checked the related questions and they did not help my case

</div>
  • 写回答

1条回答 默认 最新

  • dongsi2317 2019-08-08 18:16
    关注

    One way to check whether the shell command executed correctly is to use the exec() function instead. exec() gives you the ability to see the return value and the output in a variable.

    You can build a error checking execution like this:

    if ($_POST['visual']) {
        exec("sh test.sh", $output, $return);
    
        // Return will return non-zero upon an error
        if (!$return) {
            echo "working! woot woot";
        } else {
            echo "Error with shell script";
        }
    }
    

    To see error codes you can look here.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度