24

I have a script that exports an environment variable and starts some subscripts.

export LOGLEVEL="1"
/home/myuser/bin/myscript1.sh
/home/myuser/bin/myscript2.sh

LOGLEVEL is available to the processes started from the subscripts. How can I change the environment variable LOGLEVEL?

I have tried to set the variable with export LOGLEVEL="5" but that`s not working.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
  • possible duplicate of [Is there a way to change another process's environment variables?](http://stackoverflow.com/questions/205064/is-there-a-way-to-change-another-processs-environment-variables) – thiton Dec 21 '11 at 11:42
  • Where do you want the LOGLEVEL changed and for what process - as you state it has changed for the two myscript processes. – mmmmmm Dec 21 '11 at 11:46
  • @Mark: It doesn't matter if LOGLEVEL is changed for the processes of both subscripts, or if I can change it for individual processes. Essentially I'm looking for any working way talking to a process, that it should print more information into it's logfile. – Christian Ammer Dec 21 '11 at 11:56
  • @ChristianAmmer - then you need to show us how you use LOGLEVEL in the scripts – mmmmmm Dec 21 '11 at 11:58
  • @Mark: Sorry that I wasn't accurate enough, but LOGLEVEL is only exported in the script, it is used from the processes through `getenv()`. – Christian Ammer Dec 21 '11 at 12:03
  • So changing it as you have done affects what the scripts print as they use getenv - what exactly do you want? – mmmmmm Dec 21 '11 at 12:05
  • @Mark: The script starts some processes after system has started. The processes are printing some information into their logfiles depending on the environment variable LOGLEVEL. Now I want to change the behavior of the processes without restarting them. – Christian Ammer Dec 21 '11 at 12:10
  • @ChristianAmmer: I just added a suggestion to my answer below. – ibid Dec 21 '11 at 12:23

1 Answers1

29

In general, you can only influence a process's environment variables at the time the process starts up. If you need to communicate a change to a running process, the environment isn't the right tool.

However, this question has some answers that suggest ways to overcome this limitation.

Edited to add in light of discussion in the question's comments: A fairly good way of communicating occasionally changing setup to a running process is to designate a configuration file where the LOGLEVEL value is set, send a SIGHUP to the process, and have the process reread the configuration file upon receipt of SIGHUP.

Community
  • 1
  • 1
ibid
  • 3,891
  • 22
  • 17
  • 2
    Thank you for the link and the clue how to manage this in the right way – I have read more about SIGHUP and found on Wikipedia: _Daemon programs sometimes use SIGHUP as a signal to restart themselves, the most common reason for this being to re-read a configuration file which has been changed_ – Christian Ammer Dec 21 '11 at 12:31