bash #!/usr/bin/env ruby 在 cron 中找不到
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15962573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
#!/usr/bin/env ruby is not found in cron
提问by ohho
I have a simple ruby script, hello.rb:
我有一个简单的 ruby 脚本,hello.rb:
#!/usr/bin/env ruby
puts 'hello'
It runs ok at the command line:
它在命令行运行正常:
# /usr/local/src/hello/hello.rb
hello
However, if I put it in cron:
但是,如果我把它放进去cron:
* * * * * /usr/local/src/hello/hello.rb >> /usr/local/src/hello/hello.log 2>&1
There are errors in the log file:
日志文件中有错误:
/usr/bin/env: ruby: No such file or directory
/usr/bin/env: ruby: No such file or directory
...
/usr/bin/env: ruby: No such file or directory
/usr/bin/env rubyruns ok at command line though:
/usr/bin/env ruby虽然在命令行运行正常:
# /usr/bin/env ruby -v
ruby 1.8.7 (2012-10-12 patchlevel 371) [i686-linux]
How to fix the env error for cron?
如何修复 env 错误cron?
回答by the Tin Man
The problem is that the environment isn't what you expect.
问题是环境不是你所期望的。
You don't say whether the cron is running as your user, or as root, but, in either case, you can test to see what the environment looks like by adding another cron entry of:
您没有说明 cron 是以您的用户身份运行还是以 root 身份运行,但是,无论哪种情况,您都可以通过添加另一个 cron 条目来测试环境的外观:
* * * * * /usr/bin/env > /path/to/your/home/directory/env.txt
Let that run once, then pull it out, and look at the file.
让它运行一次,然后将其拉出,然后查看文件。
Instead of using /usr/bin/envto try to find a Ruby to run your code, define the Ruby explicitly:
与其/usr/bin/env尝试寻找 Ruby 来运行您的代码,不如明确定义 Ruby:
* * * * * /path/to/the/ruby/you/want /usr/local/src/hello/hello.rb >> /usr/local/src/hello/hello.log 2>&1
You can figure out which Ruby you want by using:
您可以使用以下命令找出您想要的 Ruby:
which ruby
Alternately, instead of relying on /usr/bin/envin your #! line, define your Ruby there.
或者,不要依赖/usr/bin/env于您的 #! 行,在那里定义您的 Ruby。
Using /usr/bin/env rubyin your code is a convenience when you're using something like RVM or rbenv, and switching between versions of Ruby. It's not a good choice when you're putting something into "production", whether it's on your machine in your own account, or on a production host running as root.
/usr/bin/env ruby当您使用 RVM 或 rbenv 之类的东西并在 Ruby 版本之间切换时,在代码中使用会很方便。当您将某些东西投入“生产”时,无论是在您自己帐户中的机器上,还是在以 root 身份运行的生产主机上,这都不是一个好的选择。
If you are on Linux or Mac OS, try man 5 crontabfor more information. Also, "Where can I set environment variables that crontab will use?" should be very useful.
如果您使用的是 Linux 或 Mac OS,请尝试man 5 crontab获取更多信息。另外,“在哪里可以设置 crontab 将使用的环境变量?”应该非常有用。
回答by jim mcnamara
env searches only in the existing PATH variable. crond creates the process that is run as your user name. So the PATH is minimal. You have to set up your environment variables in the script itself
env 仅在现有的 PATH 变量中搜索。crond 创建以您的用户名运行的进程。所以 PATH 是最小的。您必须在脚本本身中设置环境变量

