Reload environment variables PATH from chef recipes client

a 夏天 提交于 2019-12-11 18:42:46

问题


is it possible to reload $PATH from a chef recipe?

Im intrsted in the response about process signals given in the following thread:

How to have Chef reload global PATH

I dont understand very well that example that the omribahumi user gives. I would like a clearer example with chef-client / recipe to understand, with that he explains, seems it is possible with that workaround.

Thanks.


回答1:


Well I see two reasons for this request:

  • add something to the path for immediate execution => easy, just update the ENV['PATH'] variable within the chef run.

  • Extend the PATH system wide to include something just installed.

For the 2, you may update /etc/environment file (for ubuntu) or add a file to /etc/profiled.d (better idea to keep control over it), But obviously the new PATH variable won't be available to actually running processes (including your actual shell), it will work for process launched after the file update.

To explain a little more the link you gave what is done is:

  1. create a file with export commands to set env variables

    echo 'export MYVAR="my value"' > ~/my_environment
    
  2. create a bash function loading env vars from a file

    function reload_environment { source ~/my_environment; }
    
  3. set a trap in bash to do something on a signal, here run the function when bash receive SIGHUP

    trap reload_environment SIGHUP
    
  4. Launch the function for a first sourcing of the env file, there's two way:

    • easy one: launch the function

      reload_environment
      
    • complex one: Get the pid of your actual shell and send it a SIGHUP signal

      kill -HUP `echo $$`
      

All of this is only for the current shell until you set this in your .bash_rc

Not exactly what you were asking for indeed, but I hope you'll understand there's no way to update context of an already running process.

The best you can do is: update the PATH with whatever method you wish (something in /etc/profile.d for exemple) and execute a wall (if chef run as root) to tell users to reload their envs

echo 'reload your shell env by executing: source /etc/profile' | wall

Once again, it could work for humans, not for other process already running, those will have to be restarted.



来源:https://stackoverflow.com/questions/25295439/reload-environment-variables-path-from-chef-recipes-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!