[ACCEPTED]-how to create a cron job to run a ruby script?-crontab

Accepted answer
Score: 15

If your ruby is in non standard paths then 6 personally I like to wrap my ruby calls 5 in a shell script, thereby ensuring that 4 all the paths etc. my ruby program needs 3 are set correctly, and schedule the script 2 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 1 should read something like

#!/bin/bash

. ~mark/.bash_profile 
`ruby /home/mark/project/script.rb`
Score: 7

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

0

Score: 3

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

Score: 2

working on centos

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

example output

/usr/bin/ruby

Then you can 6 edit your cronjob, using crontab -e

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

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

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

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

Score: 1

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

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

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

More Related questions