# Ruby script under cron

**URL:** <https://www.ruby-forum.com/t/ruby-script-under-cron/69926>\
**Category:** Rails\
**Created:** [September 25, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/ruby-script-under-cron/69926 "2006-09-25T17:04:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Long](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47774/32.png) [@Long](https://www.ruby-forum.com/u/Long)\
**Post date:** [September 25, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/ruby-script-under-cron/69926/1 "2006-09-25T17:04:50Z")

</div>

I have a number of custom scripts that run at the console. Now I want  
cron  
to take over and run them on a daily basis. If I /usr/bin/run-parts  
/etc/cron.daily  
at the console all jobs run fine. However, if I leave it to cron and  
check next day  
there was no indication the scripts had run at all. No change in output  
to log.

I wonder if ruby is not able to run under the cron shell (is it that  
different than sh?).

Has anyone had to deal with this or can offer a suggestion?

Thanks,

Long

---

<div class="post-metadata">

**Author:** ![Long](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47774/32.png) [@Long](https://www.ruby-forum.com/u/Long)\
**Post date:** [September 25, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/ruby-script-under-cron/69926/2 "2006-09-25T17:35:00Z")

</div>

I have several rails scripts that run

```
ruby /dir/to/app/script/runner 'Actions'

```

and they run just fine. This is on Arch Linux. If you can run them  
through the cron command manually, I don’t see why it wouldn’t work from  
crond. Could it be some sort of permissions issue?

Jason

---

<div class="post-metadata">

**Author:** ![Long](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47774/32.png) [@Long](https://www.ruby-forum.com/u/Long)\
**Post date:** [September 25, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/ruby-script-under-cron/69926/3 "2006-09-25T18:05:04Z")

</div>

Jason N. wrote:

> I have several rails scripts that run
> 
> ```
> ruby /dir/to/app/script/runner 'Actions'
> 
> ```
> 
> and they run just fine. This is on Arch Linux. If you can run them  
> through the cron command manually, I don’t see why it wouldn’t work from  
> crond. Could it be some sort of permissions issue?

Jason,

Well crond runs as root so I don’t think permission is the problem. The  
difference  
seems to be in how I run the script.

ruby /path/script/my\_script

Now that you mention it I vaguely recall reference to runner usage. Let  
see  
if I have this right.

I should  
ruby script/generate task my\_script  
then  
ruby /path/script/runner ‘MyScript’  
to run it.

Any thoughts?

Thanks,

Long

---

<div class="post-metadata">

**Author:** ![Long](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47774/32.png) [@Long](https://www.ruby-forum.com/u/Long)\
**Post date:** [September 25, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/ruby-script-under-cron/69926/4 "2006-09-25T18:43:17Z")

</div>

Hi, Long,

Search the archive of this list for “cron mazel tov” and read to the  
end of that thread. It might help.

---

<div class="post-metadata">

**Author:** ![Long](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47774/32.png) [@Long](https://www.ruby-forum.com/u/Long)\
**Post date:** [September 25, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/ruby-script-under-cron/69926/5 "2006-09-25T18:25:07Z")

</div>

> > 
> 
> I should  
> ruby script/generate task my\_script

hmm… there is no ‘task’ script. See ruby script/generate --help. Also  
found this link

[http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails/](http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails/)

Long

---

<div class="post-metadata">

**Author:** ![Long](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47774/32.png) [@Long](https://www.ruby-forum.com/u/Long)\
**Post date:** [September 25, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/ruby-script-under-cron/69926/6 "2006-09-25T19:43:26Z")

</div>

Zachary H. wrote:

> Hi, Long,
> 
> Search the archive of this list for “cron mazel tov” and read to the  
> end of that thread. It might help.

I searched on gmane and only found the last posting. But did find many  
postings when search with “cron”.

I believe my scripts were missing the ruby initialization line.

#!/usr/bin/env ruby  
require File.dirname( **FILE** ) + ‘/…/config/boot’  
Line taken from script/generate and the likes.

I already had the require environment line needed to access my models.

require File.dirname( **FILE** ) + ‘/…/config/environment’

I think I will create a utility class and put all my scripts there. It  
will  
help clean up my code for sure. Then I can use script/runner  
to start the process. In this way all scripts will run under one rails  
env instead of a separate one for each.

Thanks again everyone,

Long
