how to create a cron job to run a ruby script?

≯℡__Kan透↙ 提交于 2019-12-10 01:58:42

问题


I want to create a cron job to run a ruby script. this is what i have put in the crontab.

2 * * * * ruby /home/mark/project/script.rb >> /home/mark/cronOutput.txt

But its not running. I think there's some problem with the environment getting loaded up when the cron runs as root.

Please help.


回答1:


If your ruby is in non standard paths then personally I like to wrap my ruby calls in a shell script, thereby ensuring that all the paths etc. my ruby program needs are set correctly, and schedule the script in crontab. Do something like

2 * * * * /home/mark/project/ruby_wrapper_sh >> /home/mark/cronOutput.txt 2>&1

and your /home/mark/project/ruby_wrapper_sh should read something like

#!/bin/bash

. ~mark/.bash_profile 
`ruby /home/mark/project/script.rb`



回答2:


If you are using RVM, you can simply do:

rvm cron setup 

Reference: https://coderwall.com/p/vhv8aw/getting-ruby-scripts-working-with-bundler-rvm-and-cron




回答3:


Check whenever(https://github.com/javan/whenever) gem to use cron jobs in Rails




回答4:


working on centos

in your terminal execute # which ruby which is find your ruby path

example output

/usr/bin/ruby

Then you can edit your cronjob, using crontab -e

* * * * * /usr/bin/ruby /home/mark/project/script.rb

and save, this simply working on my centos server. You can test the code first using this command before you edit your cronjob

#/usr/bin/ruby /home/mark/project/script.rb

it should be working first, then you can put on your crontab



来源:https://stackoverflow.com/questions/12066489/how-to-create-a-cron-job-to-run-a-ruby-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!