Pgm at boot time

Hello,

I need to start few process at boot time.
I did this :

1- Create the script with start/restart/…
2- add :

description: xx

chkconfig: 345 99 0

3- add this script in /etc/init.d/
4- chkconfig --add my script
5- chkconfig --level 3 myscript on

Ok when I do >service myscript start it’s work.

Now my pbm is that at reboot time, it is ROOT user who launch the script and I don’t want to do that.

What could be the best solution to launch this script at boot time under an other user than root. This script has to be launch in his directory because it looks for other files in his specific dir.

I Thought do a SU - user1 -c /user/rep/thecommand
But it’s not enough as, the directory of the command in the script is not under ~/ of the user but under a subdirectory.

I also thought to create a symlink from the /home/user/dir1/command (with chown user1:user1) to /etc/init.d/command (with chown root:root).
But I’m not sure it will work ?

For example here is my script. It has to run in /home/user1/dir1/ and in /home/user1/dir1/src/pgm
I’m not sur it will work at boot time because it looks for file in different locations

#!/bin/sh

description: script pgm1 carat hosting

chkconfig: 345 99 0

PID_FILE="/home/user1/dir1/pgm1.pid"
PID_BACKUP="/home/user1/dir1/pgm1.pid.bak"
if [ “$1” = “start” ] ; then
echo "Starting "
if [ -r $PID_FILE ] ; then
mv -f $PID_FILE $PID_BACKUP
fi
su - user1 -c /home/user1/dir1/src/pgm1
sleep 1
if [ ! -r $PID_FILE ] ; then
echo “Possible error encountered (not started ?)”
echo “=====================================================”
echo "Check above for possible errors, and this output of "
echo “pgm1.log” echo “=====================================================”
tail -n 5 /home/user1/dir1/pgm1.log
if [ -r $PID_BACKUP ] ; then
su - user1 -c mv -f $PID_BACKUP $PID_FILE
fi
fi
elif [ “$1” = “stop” ] ; then
echo “Stopping pgm”
kill -9 cat $PID_FILE
elif [ “$1” = “rehash” ] ; then
echo “Rehashing pgm”
kill -1 cat $PID_FILE
elif [ “$1” = “restart” ] ; then
echo “Restarting pgm”
kill -2 cat $PID_FILE
elif [ “$1” = “mkpasswd” ] ; then
su - user1 -c /home/user1/dir1/src/pgm -P $2 $3
elif [ “$1” = “version” ] ; then
su - user1 -c /home/user1/dir1/src/pgm -v
else
echo “Usage: pgm start|stop|rehash|restart|mkpasswd|version”
fi

Is there a better solution to do that than SU - user1 -c ?
Is a symlink from my pgm1 to /init.d/ --> /rc3.d/ will work and do what I’d like (I don’t want files created as pgm.pid are with root owner !! but user1) ?

All your comments suggestions are well welcomed :slight_smile:

Thanks
Squalito

well it seems to work :slight_smile:

squalito, you can also use the installed program “setuidgid” which will run a program as a given user/group.

Chris

Ok chris, but it will not go under the good directory

So I have to do a “setuidgid” and then “cd /to/thegood/dir”

Thanks

Pascal

So I have to do a “setuidgid” and then “cd /to/thegood/dir”

yes:

cd ~user
setuidgid user command

Chris

cd ~user

Note exactly as the “execution” directory is in a sub directory :slight_smile:
It’s ok Chris I have tested it and everyting is fine

Thanks This solution is much better than my su -c

:slight_smile: