Operating System - Linux
1819866 Members
2631 Online
109607 Solutions
New Discussion юеВ

Running startup scripts under different user

 
SOLVED
Go to solution
tpfraz
Advisor

Running startup scripts under different user

Hello, I'm running RedHat 8.
Does anyone know of a way to run a startup script in /etc/rc.d/init.d/ under a user other than root?
I don't think that there is any setting in the program itself or the config files for it.
To run it normally I just installed it using user "user1" and su to user1 to call the startup script. This way the process is owned by user1.
I tried creating a chkconfig compatible script in /etc/rc.d/init.d/ to start it on startup, but then it starts as root.
Any suggestions?
Thanks...

-Travis
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Running startup scripts under different user

First you would have to allow execute permissions to users other than root.

This is a potential security hazard.

To change the permissions chmod o+x /etc/init.d/

You couild change ownership on the script, but this too is a security hazard.

Some daemon's are quite unhappy with being run by non-root users. Some won't run at all.

Probably the issue with what you have done thus far is permissions and ownership of the script.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dave Falloon
Trusted Contributor
Solution

Re: Running startup scripts under different user

As root you can run anything you want as any user you want. Here is one way:

su - -c ""

heres a runnable example:

su - flexlm -c "lmgrd -c license.dat"

I actually use this to run my flexlm license daemons so that the process is running as the flexlm user, ie. no shell.

I hope that helps,

Dave
Clothes make the man, Naked people have little to no effect on society
tpfraz
Advisor

Re: Running startup scripts under different user

It's not a matter of permissions or ownership because I can run the daemon just fine under the user that I want to. It's just getting it to start automatically under that user upon startup.

Dave, so it sounds like I should be able to just enter the command
su user -c "command" for the program I want started into the rc.local file and it should run correctly.
Does that sound right?
I tried starting it on the command line that way and it works just fine.

-Travis
Dave Falloon
Trusted Contributor

Re: Running startup scripts under different user

Yup that is exactly what it should do. I do it all the time.

remember the - after the su is very important otherwise you are that user but you have not sourced that users enviroment scripts which can be bad news, ie. path will be totally on crack.

su dave != su - dave

Dave
Clothes make the man, Naked people have little to no effect on society
Dave Falloon
Trusted Contributor

Re: Running startup scripts under different user

Oh wait don't do it in rc.local thats a pain in the butt. Its pretty easy to write an init script and put it in /etc/init.d/ that way you can start and stop the daemon simply.

Then make some links to it in /etc/rc2.d/

called S99<scriptname> ( S is for start )

and one in rc5.d/ called

K99<scriptname> ( the k is for stop which is kind of dumb but whatever )

The scripts in the rcN.d directorys are executed with the argument start or stop depending on their name. At least that has been my experience. I will attach a really simple script that I use to start my flexlm servers so you have a kind of template to work with.

I hope that helps,

Dave
Clothes make the man, Naked people have little to no effect on society
tpfraz
Advisor

Re: Running startup scripts under different user

Okay, thanks Dave.
I'm pretty sure I have it working, I'll know next time I restart.
I already had a script in /etc/rc.d/init.d/ that would start the program but under root.
All that script did was call the program's respective _start or _stop scripts.
So I just added the "su - user" in front of each call to the start/stop scripts.
It works by doing
/etc/rc.d/init.d/program start|stop manually
and runs under the appropriate user so I'm pretty sure that it will works on startup.
I also configured the script appropriately with the S and K links.
Thanks a lot...

-Travis