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?

1 Answer 1


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
  • 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 Nov 6, 2015 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .