Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why this:

rsync -avz -e ssh /home/userneme/folder

works from cronjob and this:

exec("rsync -avz -e ssh /home/userneme/folder");

doesn't work.

I know exec is working because i have a few places in my appp that do convercion from pdf to jpg with ImageMagick (exec).


exec is working OK it was a permission issue on remote server. "Local" server is shared reseller account and remote server is my first VPS Ubuntu 10.10 LAMP box. If only I had a system administrator since i'm just a software developer forced to do this and i stink at it :) Thank You all!

share|improve this question
Do you use ssh keys with rsync? Your apache have access to this keys ? – Sacx Mar 18 '11 at 13:06
yes passwordless ssh public. They are working fine with cronjob. For apache i don't know because "local" server is shared (Hostgator) – mojeime Mar 18 '11 at 13:08
up vote 4 down vote accepted

Your ssh keys should be also on apache home folder. If not, the rsync will not work.


share|improve this answer
or specified with -i, iirc – 3molo Mar 18 '11 at 13:13
You mean apacheuser/.ssh folder. If this is true that i am stuck becouse "local" is hostgator reseller account – mojeime Mar 18 '11 at 13:20
Yes. Sorry. try to don't use sshkeys and pipe the password directly to rsync. – Sacx Mar 18 '11 at 13:26
After adding: -i /home/user/.ssh/ output of exec changed to this: array(1) { [0]=> string(27) "building file list ... done" }, but file is not transfered. – mojeime Mar 18 '11 at 14:27
Do you have enough rights to that folders ? – Sacx Mar 18 '11 at 14:38

This is probably PATH related. try execing rsync with a full path e.g.

exec("/usr/bin/rsync -avz -e ssh /home/userneme/folder");
share|improve this answer
note: bin.rsync should read bin/rsync. I can't edit. :) – egorgry Mar 18 '11 at 13:08
@egorgry: thanks - you should be able to edit though - anyone can edit - some need approval though. – Iain Mar 18 '11 at 13:11
@lain: The system told me I don't have permission to edit. I make that same typo at least once a day. – egorgry Mar 18 '11 at 13:16
It doesn't work exec outputs array(0) { } – mojeime Mar 18 '11 at 13:16
@mojeime: I've always created a wrapper script in bash to call php script when using cron. I've had better success with that method. – egorgry Mar 18 '11 at 13:18

Does your host have safe_mode on?

Run php -i |grep -i safe. If safe mode is on, then look at safe_mode_exec_dir. If rsync isn't in one of the listed folders, then PHP won't exec it.

You can ask your host to put a symlink to rsync in the exec_dir path, and then exec it from that location; but it depends on their policy as to whether they'll allow it.

share|improve this answer

You should be able to do this by giving ssh a specific key file to use in the -e option to rsync. Since you're using a shared host, what you need to do is create the ssh key somewhere safe, then put the keys onto the host in some directory as safe as you can get (should only be readable by you and apache, but if anyone else knows about it they can use a PHP script or something else to have apache send your key to them. If your host uses suexec for scripts so they all run under your username, you can make it readable only by you.)

Then you would use

rsync -avz -e "ssh -i /some/id_?sa_key" /home/userneme/folder

Since this isn't very secure, you should set up the remote side's authorized keys file to force a command that will, at the very minimum, verify that the command being executed is "rsync".

More on automating rsync (including a script to verify the ssh command) here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.