I'm working on a script to sync a folder from Windows to a Linux server with Rsync and I manage to get it work with git bash and the rsync executable.

In Centso I added the syncinguser to the sudoer -> syncinguser ALL=NOPASSWD:/usr/bin/rsync to allow the execution of Rsync, how can I allow the same user to run a .sh file to complete the sync process or a php command?

I want to sync a Laravel application so I need to run the command php artisan cache:clear and php artisan config:cache after the sync has finished.

I would also like to change owner:group like chown -R user:psacln * but I'm quite happy with the fact that doesn't change the rights on the folders E.g: 0777 permission on the public folder.

This is my script in windows so far:

rsync -rt --chown=user:group (being ignored I think because user's permissions) 
--exclude ".env" --exclude "storage" --exclude "public/storage" --progress 
--rsync-path="sudo rsync" -e "ssh -p <port>" 
'/c/path/to/local/folder/' syncinguser@<ip address>:/remote/path/

how can I add something like this after the sync:

ssh yncinguser@<ip address> php artisan cache:clear
ssh syncinguser@<ip address> php artisan config:cache
// OR
ssh syncinguser@<ip address>:/remote/path/to/completesync.sh

where completesync.sh will run the 2 php artisan command and apply recursively the owner:group of all files/folders as when they come from windows they are all root:root

I cannot connect as a root because we blocked it, you need to connect as a normal user and then switch to root.

 ssh -p <ssh port> syncinguser@<IP ADDRESSED> '/path/to/script.sh'

permissions denied, same if I connect as a normal user

  • You need to write a shell script. What's the question exactly? What you've shown isn't really a script, but a single command. A script is a text file with a series of commands, with eventual flow control (conditions checks, etc). – wazoox Jun 5 '19 at 15:54
  • the final script will be rsync / php artisan cache:clear / php artisan config:cache and in the one that will runs between the 2 linux server also php artisan down / php artisan down before and after to run the sync and cleaning the cache, it's not a really complex script, just an automation of some commands. that command is saved in a .sh file in windows that i run calling it through the git bash – Sebastiano Jun 5 '19 at 15:58
  • OK, but I don't get what you're asking or what the problem is :) – wazoox Jun 5 '19 at 17:27
  • I've updated the question, maybe it's a bit clear now. – Sebastiano Jun 5 '19 at 22:01

OK, so you need two scripts: one on the source machine, and another one on the destination machine. The script on the source machine should be something like:

rsync ... ... ... syncuser@destination:/dest/path
ssh syncuser@destination "sudo /some/path/to/completesync.sh"

And on the destination machine, the script /some/path/to/completesync.sh which contains something like this:


php artisan cache:clear
php artisan config:cache

# whatever else you need to run as root

Be careful to have restricted rights on this script:

chown root:root /path/to/completesync.sh && chmod 700 /path/to/completesync.sh

Last, modify /etc/sudoers on the destination machine so that "syncuser" can run both rsync and your script as root:

syncuser ALL=NOPASSWD: /usr/bin/rsync, /path/to/completesync.sh

Now running the script on the source machine should complete the whole process in one operation.

  • I change the permission to 777 and in visudo syncuser ALL=(ALL) NOPASSWD:ALL but nothing to do, I always have permission denied. I was trying to do a simple chown -R ... it runs if I use that user on the server and i run the script manual but not from remote – Sebastiano Jun 7 '19 at 10:37
  • Uh, of course it misses the "sudo" command in the first script :) – wazoox Jun 10 '19 at 16:48
  • 1
    now it works :) thanks a lot – Sebastiano Jun 17 '19 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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