XFerrariX
XFerrariX

Reputation: 47

Bash Script through PHP

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>

Upvotes: 1

Views: 320

Answers (1)

quickshiftin
quickshiftin

Reputation: 69731

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.

Upvotes: 1

Related Questions