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 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服
  • ¥15 心理学eprime编程
  • ¥15 arduino esp8266开发
  • ¥15 stm32单片机通过485发送命令给驱动器控制电机转动,同样的代码f103可以控制电机转动,换到f407不能动了,但是用串口助手调试407显示发送的命令都是正确的,卡了好久了这是发送规则