Home > Net >  How can I sudo inside of a bash script?
How can I sudo inside of a bash script?

Time:06-07

I have a post-commit hook in my subversion that will export a copy of my repo to a desired location for deployment. That part works fine, but it comes in with apache:apache. I need this to be changed to prod_user:prod_user. If I try to add a chown statement in my script, it will fail. If I try to use sudo, it will ask for a password that I cant give because this happening in a post-commit script. I'd like this to be as automated as possible.

My question is: How can I make this work? I need to export the contents of my repo to the production folder and convert the users/groups to match existing production users/groups.

Is there a way to pass my password as an argument to a sudo command?

Thank you for your help!

CodePudding user response:

Is there a way to pass my password as an argument to a sudo command?

Don't do it, if at all possible. This will leak your password to anyone that can read the script.

But if you can't avoid it, use echo <password> | sudo -S <command> - -S makes sudo read from stdin so you can give it the password from there

CodePudding user response:

Don't do any of sudo, chown, chgrp. It is not the responsibility of the uploader to fix permissions on the remote server.

Have the server administrator properly setup these, so that pushing production files from the repository works straight without messing with sudo permission at the server.

If you are the one same person, then take the time to fix the server side to avoid having a remote user elevate its privileges (even temporarily with sudo) for the sake of fixing uploaded files permissions.

CodePudding user response:

Use crontab -e as root user, then you can change ownership without escalation of privileges.
Or run as prod_user and make it check out the code ...then it is already the owner of the files.
Keeping a file with the last deployment timestamp can be used to compare to HEAD timestamp.

  • Related