Avatar of alexatsearidge
alexatsearidgeFlag for Canada 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/myprog/logfile.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.
Shell ScriptingLinux Distributions

Avatar of undefined
Last Comment
alexatsearidge

8/22/2022 - Mon
ozo

Can you put the init commands into $DIBIN
ASKER
alexatsearidge

Oops. Typo.


LOGFILE=/usr/local/code/myprog/logfile.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
ASKER CERTIFIED SOLUTION
omarfarid

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
alexatsearidge

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
alexatsearidge

Thanks!