如何创建一个运行ruby脚本的cron作业?

时间:2023-01-31 01:08:32

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

我想创建一个cron作业来运行ruby脚本。这就是我在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.

但它没有运行。我认为当cron以root身份运行时,环境会出现问题。

Please help.

4 个解决方案

#1


8  

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

如果你的ruby在非标准路径中,那么我个人喜欢在shell脚本中包装我的ruby调用,从而确保正确设置我的ruby程序所需的所有路径等,并在crontab中安排脚本。做点什么

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

你的/ home / mark / project / ruby​​_wrapper_sh应该是这样的

#!/bin/bash

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

#2


3  

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

检查每当(https://github.com/javan/whenever)gem在Rails中使用cron作业

#3


1  

If you are using RVM, you can simply do:

如果您使用的是RVM,则可以执行以下操作:

rvm cron setup 

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

#4


0  

working on centos

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

在你的终端执行#哪个ruby找到你的ruby路径

example output

/usr/bin/ruby

Then you can edit your cronjob, using crontab -e

然后你可以使用crontab -e编辑你的cronjob

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

* * * * * / 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

并保存,这只是在我的centos服务器上工作。在编辑cronjob之前,可以先使用此命令测试代码

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

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

它应该先工作,然后你可以穿上你的crontab

#1


8  

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

如果你的ruby在非标准路径中,那么我个人喜欢在shell脚本中包装我的ruby调用,从而确保正确设置我的ruby程序所需的所有路径等,并在crontab中安排脚本。做点什么

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

你的/ home / mark / project / ruby​​_wrapper_sh应该是这样的

#!/bin/bash

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

#2


3  

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

检查每当(https://github.com/javan/whenever)gem在Rails中使用cron作业

#3


1  

If you are using RVM, you can simply do:

如果您使用的是RVM,则可以执行以下操作:

rvm cron setup 

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

#4


0  

working on centos

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

在你的终端执行#哪个ruby找到你的ruby路径

example output

/usr/bin/ruby

Then you can edit your cronjob, using crontab -e

然后你可以使用crontab -e编辑你的cronjob

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

* * * * * / 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

并保存,这只是在我的centos服务器上工作。在编辑cronjob之前,可以先使用此命令测试代码

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

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

它应该先工作,然后你可以穿上你的crontab