Operating System - HP-UX
1830899 Members
2017 Online
110017 Solutions
New Discussion

Starting a process at boot that is owned by a user, not root

 
Tomasz Wozniak
New Member

Starting a process at boot that is owned by a user, not root

I need to start a process at boot time, however the process needs to be owned by a local user and not root. I understard that I can start a process at boot from /sbin/rcX.d, however the process will be owned by root.
8 REPLIES 8
Denver Osborn
Honored Contributor

Re: Starting a process at boot that is owned by a user, not root

In your startup script you could have it su to the user to start the process.

su - user_name -c "/path/command"

I've seen some posts in the hp-ux forum for this, here's one that should work;
http://forums.itrc.hp.com/cm/QuestionAnswer/1,11866,0x58f8660142b2d5118ff10090279cd0f9,00.html

Hope this helps
-denver
Sanjay_6
Honored Contributor

Re: Starting a process at boot that is owned by a user, not root

Hi,

As Denver has already pointed out, to run a particular command as a user while booting you should put
su - user_name -c "/path_to_command/command"

in the startup file. You can put this command in /sbin/rc2.d directory with a Sxxxsome_name, where xxx is a three digit number.

Try to run this command after loggging in as root user to see that the command doesn't return asking you for an option. When you do su - user_name it will execute the profile of that user and sometimes it asks you to enter the terminal type (say hp or vt100 or hpterm). To avoid the system asking you this terminal type, export the terminal type in the user .profile and put a "#" in front of the terminal type evaluation script (eval tset ....). instead export the terminal type for that user. put "export TERM=terminal_type" in the .profile for that user.

Hope this helps.

thanks
A. Clay Stephenson
Acclaimed Contributor

Re: Starting a process at boot that is owned by a user, not root

Hi Tomasz:

The one problem you may encounter with the su - user command is the execution of the user's .profile. If you simply execute it as su user command then the user's .profile is not executed. .profiles often fail because some of the commands like stty and tabs expect a terminal device which you will not have when started from rc. If you must execute the .profile to set up the environment, modify the .profile something like this:

if [ -t 0 ]
then
stty ....
tabs
fi

This will bypass the commands when stdin (0) is not a terminal device.

Clay
If it ain't broke, I can fix that.
David Allen
Frequent Advisor

Re: Starting a process at boot that is owned by a user, not root

Hi Tomasz,

When you create your start/stop script try copying one of the existing scripts in the /sbin/init.d directory and use that as a template to creating your script (cron is a fairly simple script). Depending on the nature of the process you need to start, the script should include a start section and a stop section.

Save the script in the /sbin/init.d directory with the permissions rx for all and bin as owner and group. From the /sbin/rcX.d directories create a sym link to the /sbin/init.d script to stop and start the script.

ie, To start non-root processes I create a sym link in the /sbin/rc3.d directory, eg /sbin/rc3.d/S900oracle -> /sbin/init.d/oracle (default runlevel is 3) and to stop them I create a sym link in /sbin/rc2.d, eg /sbin/rc2.d/K001oracle -> /sbin/init.d/oracle.

Try to start your non-root processes last in the boot sequence (ie, at runlevel 3 - if that is the default runlevel on your system). Having them start before some root processes may cause you problems if the non-root process fails to start properly.

There is also a way of disabling the start/stop scripts via way of a simple text file kept in the /etc/rc.config.d directory. Again look at the cron entry as an example. Simply put, if the entry in this file is a 1 (ie, CRON=1) then allow cron to start and stop during startup and shutdown. If it is a anything else (ie, CRON=0) then cron will neither start or stop (of course, if you are rebooting the system it will stop cron anyway). This is particularly useful if you are patching or diagnosing a system and you don't want the non-root process starting up after reboots.

Cheers,
Dave
Sridhar Bhaskarla
Honored Contributor

Re: Starting a process at boot that is owned by a user, not root

Thomasz,

You need to club all the suggestions made by the above to get yours working.

1)Script in /sbin/init.d
2)Links from /etc/rc?.d/ as Kxxxscript Sxxxscript.The best choice for rc directory is the highest run level that your systems runs in finally.
3)/etc/rc.config.d/ flag script called from the script that is there in /sbin/init.d - But this is completely optional but gives better control.
4) In your script in /sbin/init.d you need to use su - user_name command to get the commands executed as the user_name.

For ex. a template of the script would look like this. You can take any script in /sbin/init.d and modify it


#!/usr/bin/sh

case $1 in
start) su - oracle -c /home/oracle/bin/dbstart
;;
stop) su - oracle -c /home/oracle/bin/dbshut
;;
*) echo "Usage: $0 start stop
;;

esac

#ln -s /sbin/init.d/oracle /sbin/rc3.d/S990oracle
#ln -s /sbin/init.d/oracle /sbin/rc3.d/K990oracle

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sachin Patel
Honored Contributor

Re: Starting a process at boot that is owned by a user, not root

Thanks all of you.
I would love to assign point here.
My problem is solved without posting any question.

Sachin
Is photography a hobby or another way to spend $
Sridhar Bhaskarla
Honored Contributor

Re: Starting a process at boot that is owned by a user, not root

Sachin,

If you loved to assign the points, post the same question, we will copy&paste the answers. Your love would be fruitful

:-{)

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sachin Patel
Honored Contributor

Re: Starting a process at boot that is owned by a user, not root

Hi Sri,
No fun, No excitement because I know what is going to be my answer.

Sachin

And who is going to answer. LOL :))

Sachin
Is photography a hobby or another way to spend $