如何在gem中通过crontab设置PATH变量

时间:2014-05-23 16:30:49

标签: ruby-on-rails-3 cron whenever whenever-capistrano

是否可以通过每当schedule.rb文件在crontab中设置PATH或SHELL变量?

# here I want to set the PATH and SHELL variable somehow

every 3.hours do
  # some cronjob
end

在我的capistrano部署之后,我想在crontab中输出这个输出:

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11
# some cronjobs

2 个答案:

答案 0 :(得分:2)

好的,似乎我找到了解决方案。我在这里找到了它:https://gist.github.com/jjb/950975

我在测试后会更新这个答案

我必须把它放到我的schedule.rb

# If your ruby binary isn't in a standard place (for example if it's in /usr/local/bin,
# because you installed it yourself from source, or from a thid-party package like REE),
# this tells whenever (or really, the rails runner) where to find it.
env :PATH, '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'

答案 1 :(得分:0)

在设置DISPLAYLANG等时运行zenity时,您已经在执行此操作。

如果要设置shell,请使用/home/username/script/script1.sh#!/bin/bash的第一行设置它。

如果要设置路径,一种方法是在运行命令之前设置它:

5    9-20 * * * PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11 /home/username/script/script1.sh > /dev/null

另一种/更好的方法是创建一个简单的包装器脚本,如下所示:

#!/bin/bash
export PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11

# Absolute path to this script
SCRIPT=`readlink -f $0`
# Absolute directory this script is in
SCRIPTPATH=`dirname $SCRIPT`

#make sure we are in the same directory as the script1.sh - this is useful in case the script assumes it is running from the same directory it's in and makes relative directory/file references
cd $SCRIPTPATH



##run final script, and pass through all parameters that were passed to wrapper script.
/home/username/script/script1.sh "$@"