简体   繁体   中英

How to run Ruby script (Rbenv) on bash scipt (Crontab)

How can I run a ruby script (rbenv) in bash script for crontab?

I tried:

#!/bin/bash
process=ruby
if ps ax | grep -v grep | grep $process > /dev/null
then
    exit
else
    ruby /home/jason/ruby/project.rb &
fi

exit
enter code here

It works if I run the script as:

./{nameofscript}.sh

but it does not work when I put it on crontab. My crontab setting is like this:

*/2 * * * * root sh /home/jason/ruby/run.sh

rbenv is not loaded under cron execution session.

You can try to add them dynamically on your $PATH like this:

#!/bin/bash

PATH=$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH
process=ruby
if ps ax | grep -v grep | grep $process > /dev/null
then
    exit
else
    ruby /home/jason/ruby/project.rb &
fi
exit

1/ you should need to specify sh in your crontab (replace sh /home/jason/ruby/run.sh by /home/jason/ruby/run.sh)

2/ in any case, you may consider using nohup instruction in front of the following instruction, to ensure it won't be hang up during execution

nohup ruby /home/jason/ruby/project.rb &

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM