alexatsearidge
asked on
Linux init script, bash, exporting variable and accessing as another user?
I'm using Fedora 8.
I have an initscript for my app which sets some variables, and then starts the process as another user. ex:
LOGFILE=/usr/local/code/my prog/logfi le.txt
PATH=$PATH:/usr/local/code /myprog/
BIN="myprog"
export LOGFILE
export PATH
export BIN
if su - myuser -c "$DIBIN" >>$LOGFILE 2>&1 &; then
success
else
failure
fi
echo
The problem as I understand it is that my app cannot then access the variables, since they were set by root. My app, in C++, uses the getenv("LOGFILE") to check for the variable, which returns null.
I'm looking for a solution that involves the least amount of system configuration (ie., editing sudoers or bash profiles). Hopefully something can work right in the script. I'm thinking there should be a way that I can export variables for all users to read, but I don't quite understand the environment enough at this point.
Thanks.
I have an initscript for my app which sets some variables, and then starts the process as another user. ex:
LOGFILE=/usr/local/code/my
PATH=$PATH:/usr/local/code
BIN="myprog"
export LOGFILE
export PATH
export BIN
if su - myuser -c "$DIBIN" >>$LOGFILE 2>&1 &; then
success
else
failure
fi
echo
The problem as I understand it is that my app cannot then access the variables, since they were set by root. My app, in C++, uses the getenv("LOGFILE") to check for the variable, which returns null.
I'm looking for a solution that involves the least amount of system configuration (ie., editing sudoers or bash profiles). Hopefully something can work right in the script. I'm thinking there should be a way that I can export variables for all users to read, but I don't quite understand the environment enough at this point.
Thanks.
Can you put the init commands into $DIBIN
ASKER
Oops. Typo.
LOGFILE=/usr/local/code/my prog/logfi le.txt
PATH=$PATH:/usr/local/code /myprog/
BIN="myprog"
export LOGFILE
export PATH
export BIN
if su - myuser -c "$BIN" >>$LOGFILE 2>&1 &; then
success
else
failure
fi
echo
LOGFILE=/usr/local/code/my
PATH=$PATH:/usr/local/code
BIN="myprog"
export LOGFILE
export PATH
export BIN
if su - myuser -c "$BIN" >>$LOGFILE 2>&1 &; then
success
else
failure
fi
echo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent -- that worked.
A side effect that I've noticed is that if I then run the init script (the first one that calls the other one with su) NOT as root, but as the user, the script prompts for password. Example:
root> /sbin/service myProg start
//myProg starts ok
root>su user
password:******
user> /sbin/service myProg start
password:
I wonder why it asks for password as the user, but not when I'm root? My script looks like this:
#!/bin/bash
su - user -c "/opt/myprog/myprog.sh $1"
exit 0
Anyway, this problem is not a big one since I can still start/stop cleanly as root. I guess I just want to know more about bash scripting and su.
A side effect that I've noticed is that if I then run the init script (the first one that calls the other one with su) NOT as root, but as the user, the script prompts for password. Example:
root> /sbin/service myProg start
//myProg starts ok
root>su user
password:******
user> /sbin/service myProg start
password:
I wonder why it asks for password as the user, but not when I'm root? My script looks like this:
#!/bin/bash
su - user -c "/opt/myprog/myprog.sh $1"
exit 0
Anyway, this problem is not a big one since I can still start/stop cleanly as root. I guess I just want to know more about bash scripting and su.
ASKER
Thanks!