如何从php运行.sh文件? [英] how to run a .sh file from php?

查看:835
本文介绍了如何从php运行.sh文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php运行shell脚本

I am trying to run a shell script using php

shell脚本(/home/scripts/fix-perm.sh)在同一服务器上

shell script ( /home/scripts/fix-perm.sh ) is in the same server

这是我正在尝试的代码

<?php
echo shell_exec('/home/scripts/fix-perm.sh');
?>

上面的代码不起作用

使用Linux服务器

有人可以帮助我吗?

推荐答案

Shell exec接受一个字符串,该字符串必须是实际命令.您现在正在向它传递一个文件路径.这不解释为在此路径上执行文件".您可以做几件事.

Shell exec takes a string which needs to be an actual command. You are now passing it a filepath. This is not interpreted as "execute the file at this path". You could do several things.

您需要做的是使用程序调用文件.按照评论中的建议用bash或sh调用它:

What you need to do is call the file with a program. Call it with bash or sh as suggested in the comment:

echo shell_exec('sh /home/scripts/fix-perm.sh');

另一个选择可能是:

$contents = file_get_contents('/home/scripts/fix-perm.sh');
echo shell_exec($contents);

但是我认为第一种选择会更好.

I think the first option would be better however.

请务必注意,用于执行外部程序的所有命令均应使用实际命令,而不是文件路径或其他内容.这适用于 shell_exec

It is important to note that all commands for executing external programs expect actual commands and not a filepath or something else. This goes for shell_exec, exec, passthru and others.

这篇关于如何从php运行.sh文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆