Question

I've written an application in C that is supposed to be reading environmental variables and handling those changes appropriately. When the application starts, I've set it up to go ahead and initialize the variable (to prevent any null pointers from returning) via setenv("MYVARIABLE", "TEST", 1).

This application loops often and, during those loops, one of its jobs is to check that global variable via getenv("MYVARIABLE").

The plan then was to have either a shell script or a python script change these environmental variables. The C application is full screen, so I have no way of testing this process without another terminal entry. In my other terminal (c2) I run commands such as:

MYVARIABLE="My New Value"

or

export MYVARIABLE="My New Value"

My application doesn't seem to catch the environmental update, though. Instead it continues to insist that MYVARIABLE is "test," and not "My New Value." I'm not sure why these two environments are separate, but I need them to work together. Does anyone know what I'm doing wrong?

My system is running Archlinux for anyone who is interested

Was it helpful?

Solution

The problem you're facing right now is that you've only set your variable in the local shell session's scope. EX:

(Assuming bash) When you set a variable such as:

MYVARIABLE="My New Value"

it only effects the current shell, when you set it as:

export MYVARIABLE="My New Value"

it effects the current shell and all processes started from the current shell.

If you set it in your .bashrc file it will set it permanently for all future sessions, but you'll need to source that file for it to work in your current session.


The best solution is to fork off a process. For example if your program is called a.out you can execute:

> ./a.out &

This will allow you to continue to work in the shell session while the program is running, then you can set variables in that same session.


The only other option I've ever seen is to force your shell session to "auto" source things as they come in:

1) In your first session enter:

trap 'source ~/.bashrc' DEBUG

2) Then start your program in the same session:

./a.out

3) In a second window edit your .bashrc file to add the new env var

4) in the second window source the new version of the file:

source ~/.bashrc

Now the first window running your program has the new var set to its session. I've used that before and I know it works, but I've never tried it on an applications that was already spawned.. but I suspect it should work for you.

OTHER TIPS

Your process environment is not dynamically changeable!!!

Remember this main() prototype...

int main (int argc, char *argv[], char *envp[])
{
    char *path;

    /*
    Searches in this process envp[] only,
    There is no way it can access changes happening in Shell command prompt...
    User shell does not communicate with this process, if not piped.
    This will always return PATH, that was set at the time of starting.
    */
    while (1)
    {
        path = getenv("PATH");
        sleep (5);
        printf("PATH = %s\n", path);
        free(path);
    };
}

Your understanding of getenv() library function is WRONG. To achieve your expectation, You need to use any form of IPC.

setenv() works with calling process own environment. That will be passed on to it own child process through fork(), exec() syscalls. This will will not do anything in the parent process, your shell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top