Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a service that runs at boot, and in that service it calls a bash script in the background that exports some environment variables. The problem I'm having is that those environment variables are not being sent to the parent of the background process so as soon as my script is done executing, they are gone.

In addition, after the script is run the service then calls another script that starts an application that I have. This application needs access to those environment variables.

The RHEL system I run it on is meant to never be logged in to by the user, it only boots up and starts the application. I know that environment variables for a parent process/shell can't really be set by a child background process shell though.

I need a way to do this through a script that gets called by my service (not necessarily in the background though), not by adding them in my service (which didn't work either for me) and not by storing them in an /etc/environment or .profile or anything such as that.

In my service I tried adding the environment variables (not what I want to do though):

    export TEST=192.168.1.1

I also tried this in my service:

    TEST=192.168.1.1
    export TEST=${TEST}

I tried changing how my service calls the bash script:

    /bin/asdf/script &

I also tried sourcing the script so that it runs in the same shell (which I got from this):

    . ./bin/asdf/script
    #I'm very confused why this didn't work

I also found this which looked interesting but it didn't really pan out in my case.

share|improve this question
add comment

3 Answers

You can try putting a script to gather the variables in /etc/profile.d/

Example:

/etc/profile.d/somescript.sh

#!/bin/bash
TEST=$(cat /var/somefile)
export $TEST

/etc/profile does a call that will run any script in /etc/profile.d/, and this applies to all users on the system including root.

share|improve this answer
    
You may be on to something here. I don't want to do exactly that though since the script cannot be located in the /etc directory for IA reasons. But I think I can create a symlink in there that points to my script. But to complicate things, some of the environment variables that are set by the script are coming from environment variables set by the service. So this might not work, since the variables have to already be set before the service script gets to the end, but not too early or the environment variables needed won't have been created by the service yet. –  sqenixs Dec 7 '11 at 2:09
    
I guess I can get rid of the dependency on the variables set in the service. In this case, I think it will work, as long as the script gets run before the service starts up. Do you know when the login shell is started up? Or can I control when the login shell starts up? I don't have a service for it in the rcX.d directory for my run level . –  sqenixs Dec 7 '11 at 2:15
add comment

There is no way for a process to influence the environment of another existing process. Processes only influence the environment of their child processes.

So you need to set these environment variables in an ancestor of the application that needs them. Instead of having your service separately invoke the environment-setting bash script and the application, have your service invoke a bash script that sets the environment variables then launches the application.

#!/bin/bash
. /path/to/environment/variable/setter.bash
exec /path/to/application
share|improve this answer
    
From what I have read online, there are hacks (something having to do with eval on a source script, or using gdb) to get it to work though. I'm not overly interested in it being in the background, if I can run the commands in the current "shell" (are you in a shell during boot when your service is executing?) then that would be fine too. –  sqenixs Dec 6 '11 at 2:40
    
Unfortunately, I cannot start the application from the service, as there are many different processes that are started and things that are configured from the application start script, and they must be kept separate from the service for IA. –  sqenixs Dec 6 '11 at 3:38
    
@sqenixs A shell is a process like any other. There is no such thing as “being in a shell”. When you speak of “eval on a source script”, that's a protocol where a program (which may of may not be a shell script) prints out definitions in shell syntax, and a shell script interprets them. Setting environment variables with gdb could work, or it could have no effect, or it could crash your application; as you might imagine, using a debugger in production is not recommended (and, again, for good reason). –  Gilles Dec 6 '11 at 8:33
    
There's probably a solution to your problem, but you need to be more precise with your requirements. In your question, you write “after the script is run the service then calls another script that starts an application”. So you seem to have a solution: set the environment variables inside that other script. But then in a comment you write that you “cannot start the application from the service”. So, which is it? –  Gilles Dec 6 '11 at 8:35
    
perhaps, make or configure the environment of /sbin/init at the boot time. Since every process is a child of init anyways, child process can acquire the environment. Just a thought/guess. –  Nikhil Mulley Dec 6 '11 at 15:22
show 1 more comment

It seems I was mistaken. The . /bin/asdf/script did the trick for setting my environment variables in the environment before login. However, as soon as I logged in those environment variables would no longer exist for some reason. The solution for that was to add a call . /bin/asdf/script inside my /etc/profile file. I had also tried putting a symlink that pointed to my /bin/asdf/script inside the /etc/profile.d directory but that did not work. I am still unsatisfied with this solution however since I have to call my script twice: Once before I log in, and again afterward. Is there a way to persist environment variables across a login to the system, or do they always get cleared away?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.