简体   繁体   中英

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

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.

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`

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

检查任何时候( https://github.com/javan/whenever)gem以在 Rails 中使用 cron 作业

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

Thanks for @saihgala for his solution, but I'm little modified this way. I add #!/usr/bin/env ruby sting to the beginning of my ruby executable file.

Add permissions for this file, launch crontab file edit crontab -e .

Add */1 * * * 0-5 /path/to/executable.rb>>/path/to/output.txt and then it works for me.

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