如何在cron中运行ruby脚本并让它加载所有'require'宝石?我正在将这个ruby脚本作为python中的子进程启动:
!#/usr/bin/env python
...
def ruby_importdb(import_this):
if formatinput == 'nessus':
print '[+] importing the report to the database'
subprocess.Popen(["/usr/local/rvm/rubies/default/bin/ruby /scripts/nessus-xml2db.rb " + import_this],
shell=True, stdout=subprocess.PIPE, universal_newlines=True)
cronjob如下:
51 11 * * * /scripts/nessus-export-scans-nes.py 2>&1 | logger
当python脚本尝试执行ruby子进程函数时,/ var / log / messages中的事件显示以下错误:
root: /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- mysql (LoadError)
root: #011from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require'
root: #011from /scripts/nessus-report-database/nessus-xml2db.rb:3:in `<main>'
如果我从我的用户帐户或root用户运行此脚本,代码就可以找到。
答案 0 :(得分:0)
将以下行添加到crontab解决了这个问题:
SHELL=/bin/bash
BASH_ENV=/root/.bashrc
并将以下行添加到.bashrc:
source /usr/local/rvm/environments/default
包含以下环境变量:
export PATH="/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1@global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:$PATH"
export GEM_HOME='/usr/local/rvm/gems/ruby-2.2.1'
export GEM_PATH='/usr/local/rvm/gems/ruby-2.2.1:/usr/local/rvm/gems/ruby-2.2.1@global'
export MY_RUBY_HOME='/usr/local/rvm/rubies/ruby-2.2.1'
export IRBRC='/usr/local/rvm/rubies/ruby-2.2.1/.irbrc'
unset MAGLEV_HOME
unset RBXOPT
export RUBY_VERSION='ruby-2.2.1'