Grafana/Telegraf/Ruby/RVM : telegraf 用户无法找到 ruby 路径
Grafana/Telegraf/Ruby/RVM : telegraf user isn't able to find ruby path
默认telegraf服务运行s在用户telegraf:telegraf
下
[esantos@localhost telegraf]$ systemctl status telegraf
● telegraf.service - The plugin-driven server agent for reporting metrics into InfluxDB
Loaded: loaded (/usr/lib/systemd/system/telegraf.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-11-15 06:01:30 EST; 3h 16min ago
Docs: https://github.com/influxdata/telegraf
Main PID: 114362 (telegraf)
CGroup: /system.slice/telegraf.service
└─114362 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d
Nov 15 06:01:30 localhost.localdomain systemd[1]: Started The plugin-driven server agent for reporting metrics into InfluxDB.
Nov 15 06:01:30 localhost.localdomain systemd[1]: Starting The plugin-driven server agent for reporting metrics into InfluxDB...
telegraf 用户没有shell
[esantos@localhost telegraf]$ cat /etc/passwd | grep telegraf
telegraf:x:996:994::/etc/telegraf:/bin/false
现在的问题...配置告诉 telegraf 运行 一个 ruby 脚本,它将 return 一个 JSON 对象
[esantos@localhost telegraf]$ cat /etc/telegraf/telegraf.d/groove.conf
[[inputs.exec]]
commands = ["ruby /etc/telegraf/pm_collector.rb"]
所有 运行 对像 esantos 这样的用户都很好 - SUCCESS
不幸的是,作为 telegraf 用户,运行无法使用 telegraf 服务,returning "ruby": executable file not found in $PATH"
- 失败
[esantos@localhost telegraf]$ sudo -u telegraf telegraf --config /etc/telegraf/telegraf.d/groove.conf --test
2018-11-15T14:19:08Z E! Error in plugin [inputs.exec]: exec: exec: "ruby": executable file not found in $PATH for command 'ruby /etc/telegraf/pm_collector.rb'
以 telegraf 用户身份打开 shell 也可以 - SUCCESS
[esantos@localhost telegraf]$ sudo -u telegraf /bin/bash
bash-4.2$ cat .bashrc
source /usr/local/rvm/scripts/rvm
bash-4.2$ cat .bash_profile
source /usr/local/rvm/scripts/rvm
bash-4.2$
bash-4.2$ rvm -v
rvm 1.29.4 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
bash-4.2$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
bash-4.2$ gem -v
2.7.8
bash-4.2$ echo $PATH
/usr/local/rvm/gems/ruby-2.5.1/bin:/usr/local/rvm/gems/ruby-2.5.1@global/bin:/usr/local/rvm/rubies/ruby-2.5.1/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rvm/bin
bash-4.2$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.7.8
- RUBY VERSION: 2.5.1 (2018-03-29 patchlevel 57) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/local/rvm/gems/ruby-2.5.1
- USER INSTALLATION DIRECTORY: /etc/telegraf/.gem/ruby/2.5.0
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-2.5.1/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/rvm/gems/ruby-2.5.1/bin
- SPEC CACHE DIRECTORY: /etc/telegraf/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /usr/local/rvm/rubies/ruby-2.5.1/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/local/rvm/gems/ruby-2.5.1
- /usr/local/rvm/gems/ruby-2.5.1@global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/local/rvm/gems/ruby-2.5.1/bin
- /usr/local/rvm/gems/ruby-2.5.1@global/bin
- /usr/local/rvm/rubies/ruby-2.5.1/bin
- /sbin
- /bin
- /usr/sbin
- /usr/bin
- /usr/local/rvm/bin
这是否与登录与非登录有关 shell?如何克服这个?
我们可以在更广泛的范围内看待这个问题。如何使用没有 shell 的用户在 RVM 多用户安装中 运行 ruby 脚本?
这里的技巧是在我们希望 运行 的命令前加上 /bin/bash --login
。这样就打开了登录 shell 并调用了初始化脚本。
默认telegraf服务运行s在用户telegraf:telegraf
下[esantos@localhost telegraf]$ systemctl status telegraf
● telegraf.service - The plugin-driven server agent for reporting metrics into InfluxDB
Loaded: loaded (/usr/lib/systemd/system/telegraf.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2018-11-15 06:01:30 EST; 3h 16min ago
Docs: https://github.com/influxdata/telegraf
Main PID: 114362 (telegraf)
CGroup: /system.slice/telegraf.service
└─114362 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d
Nov 15 06:01:30 localhost.localdomain systemd[1]: Started The plugin-driven server agent for reporting metrics into InfluxDB.
Nov 15 06:01:30 localhost.localdomain systemd[1]: Starting The plugin-driven server agent for reporting metrics into InfluxDB...
telegraf 用户没有shell
[esantos@localhost telegraf]$ cat /etc/passwd | grep telegraf
telegraf:x:996:994::/etc/telegraf:/bin/false
现在的问题...配置告诉 telegraf 运行 一个 ruby 脚本,它将 return 一个 JSON 对象
[esantos@localhost telegraf]$ cat /etc/telegraf/telegraf.d/groove.conf
[[inputs.exec]]
commands = ["ruby /etc/telegraf/pm_collector.rb"]
所有 运行 对像 esantos 这样的用户都很好 - SUCCESS
不幸的是,作为 telegraf 用户,运行无法使用 telegraf 服务,returning "ruby": executable file not found in $PATH"
- 失败
[esantos@localhost telegraf]$ sudo -u telegraf telegraf --config /etc/telegraf/telegraf.d/groove.conf --test
2018-11-15T14:19:08Z E! Error in plugin [inputs.exec]: exec: exec: "ruby": executable file not found in $PATH for command 'ruby /etc/telegraf/pm_collector.rb'
以 telegraf 用户身份打开 shell 也可以 - SUCCESS
[esantos@localhost telegraf]$ sudo -u telegraf /bin/bash
bash-4.2$ cat .bashrc
source /usr/local/rvm/scripts/rvm
bash-4.2$ cat .bash_profile
source /usr/local/rvm/scripts/rvm
bash-4.2$
bash-4.2$ rvm -v
rvm 1.29.4 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
bash-4.2$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
bash-4.2$ gem -v
2.7.8
bash-4.2$ echo $PATH
/usr/local/rvm/gems/ruby-2.5.1/bin:/usr/local/rvm/gems/ruby-2.5.1@global/bin:/usr/local/rvm/rubies/ruby-2.5.1/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rvm/bin
bash-4.2$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.7.8
- RUBY VERSION: 2.5.1 (2018-03-29 patchlevel 57) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/local/rvm/gems/ruby-2.5.1
- USER INSTALLATION DIRECTORY: /etc/telegraf/.gem/ruby/2.5.0
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-2.5.1/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/rvm/gems/ruby-2.5.1/bin
- SPEC CACHE DIRECTORY: /etc/telegraf/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /usr/local/rvm/rubies/ruby-2.5.1/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/local/rvm/gems/ruby-2.5.1
- /usr/local/rvm/gems/ruby-2.5.1@global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/local/rvm/gems/ruby-2.5.1/bin
- /usr/local/rvm/gems/ruby-2.5.1@global/bin
- /usr/local/rvm/rubies/ruby-2.5.1/bin
- /sbin
- /bin
- /usr/sbin
- /usr/bin
- /usr/local/rvm/bin
这是否与登录与非登录有关 shell?如何克服这个? 我们可以在更广泛的范围内看待这个问题。如何使用没有 shell 的用户在 RVM 多用户安装中 运行 ruby 脚本?
这里的技巧是在我们希望 运行 的命令前加上 /bin/bash --login
。这样就打开了登录 shell 并调用了初始化脚本。