Saicharan S M
Saicharan S M

Reputation: 828

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.

Upvotes: 16

Views: 23060

Answers (5)

Ashen One
Ashen One

Reputation: 411

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.

Upvotes: 1

rosada
rosada

Reputation: 31

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

Upvotes: 2

muhammadn
muhammadn

Reputation: 330

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

Upvotes: 7

saihgala
saihgala

Reputation: 5774

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`

Upvotes: 15

Krishna Srihari
Krishna Srihari

Reputation: 722

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

Upvotes: 3

Related Questions