I'm just wondering if it's possible to execute a command inside a bash script as a specified user and supply the password in one string.

At the moment I have


su -c postgres $COMMAND

But this is asking me for a password which I won't be able to enter all the time as this script is going to be running in a cron every hour

  • Run it as root or use expect
    – Jeff Schaller
    Jan 29, 2016 at 12:32

2 Answers 2


Several possibilities:


If you can configure it in a way that is both secure and flexible enough for your calls then you can use sudo to allow the script user to run the commands as user postgres without being asked for a password.

sudo with wrapper script

You can write a wrapper script which is writable by postgres or root only and checks that the call (script parameters) is correct. sudo can allow the script user to call the wrapper script with random parameters as postgres.


If there was only one command line to be executed then you could use SSH. You can bind a certain key to a command i.e. this key cannot be used for anything else.


We can use pipe for applying password and I have used it and it work perfectly well.


su -c postgres $COMMAND | passwd "enter your password here"

You must log in to answer this question.

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