-   Linux - Newbie (
-   -   Starting processes as a specific user? (

Loonyjuice 01-03-2007 08:54 AM

Starting processes as a specific user?
I've got a script which needs to be run on system startup, as a specific user. As I understand, to start something automatically I can add it to /etc/rc.d/rc.local, but this will start up as root, correct?

Thanks in advance


acid_kewpie 01-03-2007 09:10 AM

it will by default, but you can do something like "su -l myotherusername -c command" and it should run. of course, if it's a program which does not fork nicely (i.e. a daemon) then it'll hang the boot at that stage while it's running.

b0uncer 01-03-2007 09:11 AM

You could just as well create a script which you run from ("call from") rc.local; the script would be like

su preferred_username
commands, other stuff that is to be run as the other user

I guess that should work. su is normally used to become the super user (root), but it can be used to become another user too, but then the password for that user is needed or you will need to be root. I haven't tested if it works creating that kind of script and then make it executable and make rc.local run it (at boot), but logically it should. Try out; write a script like the above, where in the beginning you use su to become another user and then the command(s) you want to run, then make the script file executable using chmod +x scriptfilename and type the full path to the script in /etc/rc.local.

acid_kewpie 01-03-2007 09:19 AM

no, that won't work, the su command spawns a login shell, and just sits there. it won't process the other commands as the new user. only when that su terminates is the rest of the script executed:


[root@server scripts]# su ac-phillipc; whoami
[guest@server scripts]$ exit
[root@server scripts]#

timmeke 01-03-2007 11:30 AM

Another solution would be to write the commands (to be executed as the non-root user) in a script
and then to:

chown your_user:some_group the_script
chmod u+s the_script

Call that as from rc.local, and it should run under the specified user (see chown).

acid_kewpie 01-03-2007 02:49 PM

hmm yes, that's an idea. depends how many comamnds you need, wouldn't be worth it for a single lline, but anythign lareger and it could easily be useful.

timmeke 01-04-2007 03:17 AM

For the one-liner, the -c option to su can easily be used, no?

All times are GMT -5. The time now is 01:07 PM.