duanliushua5026 2014-03-05 22:00
浏览 61

通过PHP进行Bash脚本

I'm having problems trying to run the following script through php using the shell_exec() function.

#!/bin/bash

/usr/bin/sshpass -p 'password' /usr/bin/rsync -avzhe -O 
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p port"
--exclude '*html' --include='R*' --exclude '*' 
username@ipaddress:/location/ /location

When I run this script in Terminal or through php shell_exec() I receive the following error:

Unexpected remote arg: username@IP:/location/
rsync error: syntax or usage error (code 1) at main.c(1201) [sender=3.0.6]

If I remove the '-O' from the rsync part it works fine in terminal but through php I get the following error:

rsync: failed to set times on "/destination_location/.": Operation not permitted (1)
rsync: mkstemp "/location/.file.pSnb11" failed: Permission denied (13)
rsync: mkstemp "/location/.file.hR7VUM" failed: Permission denied (13)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at     main.c(1505) [generator=

Below is the php code.

<?php
if (isset($_POST['button']))
{
     shell_exec('/location/rsync.sh');
}
?>
<html>
<body>
<form method="post">
<p>
    <button name="button">Run Script</button>
</p>
</form>
</body>
  • 写回答

1条回答 默认 最新

  • duanjia4220 2014-03-05 23:00
    关注

    As others have suggested you are likely facing a permissions problem because when you run the code through the webserver, the webserver is running as a different user than root.

    You can simulate the php call by first determining which user your webserver runs as. On Ubuntu for example this is www-data.

    Then you can use sudo from the command line to test the call, sudo -u www-data my-bash-script. You should be able to sort out the permission problems this way and then you can try again via php and your webserver.

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题