1

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>
3
  • 3
    A hint: what user runs your script? Commented Mar 5, 2014 at 22:04
  • 1
    since you get the error when running via terminial, its nothing to do with phhp Commented Mar 5, 2014 at 22:05
  • Root user runs it in terminal. @Dragon If you read the full post you will note that I said if you remove "-O" from rsync it runs fine in terminal but has more errors running through php. Commented Mar 5, 2014 at 22:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Exactly. To determine user that php uses to call script just try shell_exec('whoami');
@Scony Where can I find the output of shell_exec('whoami'); Trying to search the logs for it.
Try hitting it from from your browser var_dump(shell_exec('whoami'));
@quickshiftin when I run the script with sudo -u daemon I get the same error as through php. Now the issue is how do I allow the script to run as root. I know people will say don't give root access but this is running on a private network.
I don't think you want to run it as root. Rather you want to open up permissions in other places with a special group or even allowances for daemon specifically (though group permissions would be cleaner IMO). One other option I would suggest in lieu of that would be creating a sudo entry for your specific command (saved as a BASH script) for the daemon user. Then daemon could run that command via sudo.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.