Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a server which runs Fedora 19. When the server boots up, I need to run bunch of commands. I do this in /etc/rc.d/rc.local file. This works well. Now I need to start my nodejs server from this script.

I don't want to start the server program as root. So I tried something like the following in the rc.local file.

su myuser -c /home/myuser/project/path/prod_start.sh

This executes prod_start.sh as myuser. But in prod_start.sh, I am changing the directory to the applications root directory. It also expects some environment variables to be set before running. But since these environment variables are defined in bash_profile for user myuser, none of them took effect and the the script failed.

I'm wondering how I can run a command as a different user with all the environment variables required set for him? Should I set all these environment variables at the system level so that it is available to all users?

share|improve this question
    
Why don't you just have it start when you log in? Run your script in your ~/bash_login or ~/.Xsession files. – terdon Oct 6 '13 at 15:45
    
It's a headless server. So XSession won't be there. bash_login is also not an option as it gets triggered when logging via ssh. – Appu Oct 6 '13 at 16:08
    
Well, you can't have it all. You could set your variables in the script itself. You could have the script start from ~/.bash_profile. You might be able to get it to work with su myuser bash -c "script.sh" since that would probably read the ~/.bash_profile. – terdon Oct 6 '13 at 16:34

If you add the '-' switch it will load myuser's profile. In other words you can have profile environment variables loaded while headless.

su - myuser -c "cmd_to_execute"
share|improve this answer

You must log in to answer this question.

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