Reload environment variables PATH from chef recipe

2019-09-16 05:09发布

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条回答
等我变得足够好
2楼-- · 2019-09-16 05:59

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.

查看更多
登录 后发表回答