如何从shell脚本调用Ruby脚本?

时间:2021-08-14 14:39:30

I am trying to write a watchdog for a Ruby application. So far, I have a cron job which is successfully calling a shell script:

我正在尝试为Ruby应用程序编写监视程序。到目前为止,我有一个成功调用shell脚本的cron作业:

#!/bin/sh
if ps -ef | grep -v grep | grep adpc.rb ; then
    exit 0
else
    NOW=$(date +"%m-%d-%Y"+"%T" )
    echo "$NOW - CRITIC: ADPC service is down! Trying to initialize..." >> che.log
    cd lib
    nohup ruby adpc.rb &
    exit 0
fi

This code runs correctly from command line, but I am not able to make the shell script execute the Ruby script when called from a cron job.

此代码从命令行正确运行,但是当从cron作业调用时,我无法使shell脚本执行Ruby脚本。

Please any help would be appreciated.

请任何帮助将不胜感激。

  1. The Ruby file has +x permissions.
  2. Ruby文件具有+ x权限。

  3. The nohup.out file is empty.
  4. nohup.out文件为空。

Solution: replace bare "ruby" command with full path (which ruby output).

解决方案:用完整路径(ruby输出)替换裸“ruby”命令。

Thanks to all for the replies =)

感谢所有回复=)

1 个解决方案

#1


1  

This is usually caused by an incorrect environment. Check the Ruby output in the created nohup.out file and log the stderr of nohup itself to a file.

这通常是由不正确的环境引起的。检查创建的nohup.out文件中的Ruby输出,并将nohup本身的stderr记录到文件中。

It's frequently solved by starting the script with:

它通常通过以下方式启动脚本来解决:

#!/bin/bash
source ~/.bash_profile
source ~/.bashrc

This will ensure that you run with bash instead of sh, and that any settings like PATH you've configured in your init files will be set.

这将确保您使用bash而不是sh运行,并且将设置您在init文件中配置的任何设置,例如PATH。

#1


1  

This is usually caused by an incorrect environment. Check the Ruby output in the created nohup.out file and log the stderr of nohup itself to a file.

这通常是由不正确的环境引起的。检查创建的nohup.out文件中的Ruby输出,并将nohup本身的stderr记录到文件中。

It's frequently solved by starting the script with:

它通常通过以下方式启动脚本来解决:

#!/bin/bash
source ~/.bash_profile
source ~/.bashrc

This will ensure that you run with bash instead of sh, and that any settings like PATH you've configured in your init files will be set.

这将确保您使用bash而不是sh运行,并且将设置您在init文件中配置的任何设置,例如PATH。