2

I have written a script which will run a script for a backup before:

# cat backup-before-shutdown
#Scritp to take backup before shutdown
#Author Ashish Karpe

echo "Taking backup to s3"
su ubuntu /bin/sh /home/ubuntu/backup.sh
/bin/sh /home/ubuntu/backup-to-s3.sh

Normally when I switch the user from root to ubuntu it doesn't ask password as its switching from root to ubuntu but it's asking while running the script. So what should I do to avoid the password in my script?

2

If the script is running as the root user, this sudo command string should execute the command without requiring a password.

sudo -H -u ubuntu /bin/sh /home/ubuntu/backup.sh
| improve this answer | |
  • 1
    +1, and if the script is not runing as the root user, a sudo rule must be added for the user <user> to run as user ubuntu with the flag NOPASSWD: <user> ALL=(ubuntu) NOPASSWD:/bin/sh /home/ubuntu/backup.sh – dave_alcarin Nov 6 '15 at 17:37

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.