How can I run a command as a different user while Fedora boots up?

0

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?

Navaneeth K N

Posted 2013-10-06T15:34:10.543

Reputation: 123

Why don't you just have it start when you log in? Run your script in your ~/bash_login or ~/.Xsession files. – terdon – 2013-10-06T15:45:07.720

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. – Navaneeth K N – 2013-10-06T16:08:55.747

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 – 2013-10-06T16:34:22.327

Answers

2

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"

JoshOfAllTrades

Posted 2013-10-06T15:34:10.543

Reputation: 136