Thursday, 29 November 2018

Run command as different user in your shell script

If you have a requirement of running a shell command inside your script without switching to that user, follow this method. This can also be used in a case where 'su - user -c' works on terminal but fails in script.  

Suppose you are logged in as root on your CentOS machine and you want to run your script as root user but in your script there is a particular command which you want to run as different user, please follow this syntax -  

...

sudo - u <username> <command>
...

eg.

sudo - u mahi whoami

Output : mahi





No comments:

Post a Comment