# Crontab and ruby script

**URL:** <https://rubytalk.org/t/crontab-and-ruby-script/64528>\
**Category:** ruby-talk\
**Created:** [5 January 2012 10:30 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528 "2012-01-05T10:30:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CC\_Chen](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@CC\_Chen](https://rubytalk.org/u/CC_Chen)\
**Post date:** [5 January 2012 10:30 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/1 "2012-01-05T10:30:07Z")

</div>

Hi all,

I used crontab command such as  
"\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb"

But it can't execute script1.rb, I let it print some message every 5  
min.

I also changed script1.rb to chmod 755.

Does the crontab command is right?

Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Venkatakrishnan\_Gane](https://avatars.discourse-cdn.com/v4/letter/v/50afbb/32.png) [@Venkatakrishnan\_Gane](https://rubytalk.org/u/Venkatakrishnan_Gane)\
**Post date:** [5 January 2012 13:19 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/2 "2012-01-05T13:19:35Z")

</div>

Looks ok. I don't think you need execute permission either, just read permission would do, because you're passing the file to the interpreter.

Verify that:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Interpreter path is correct  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Program does not have errors and you are able to run it yourself  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* It has correct permissions for accessing files etc as the user you've setup the cron for  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* If you're just printing stuff remember it doesn't have $stdout or $stderr to write to, redirect those to some log file

Hope this helps

> **···**
>
> On 05/01/12 4:00 PM, CC Chen wrote:
> 
> > Hi all,
> > 
> > I used crontab command such as  
> > "\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb"
> > 
> > But it can't execute script1.rb, I let it print some message every 5  
> > min.
> > 
> > I also changed script1.rb to chmod 755.
> > 
> > Does the crontab command is right?
> > 
> > Thanks.

---

<div class="post-metadata">

**Author:** ![Giampiero\_Zanchi](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@Giampiero\_Zanchi](https://rubytalk.org/u/Giampiero_Zanchi)\
**Post date:** [5 January 2012 15:34 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/3 "2012-01-05T15:34:09Z")

</div>

I am quite noob  
however...  
if your script does use gui, maybe you must use  
DISPLAY=:0.0  
\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb

a useful link may be

> **[cron and crontab](https://www.unix.com/answers-to-frequently-asked-questions/13527-cron-crontab.html)**
>
> We have hundreds of threads involving cron and crontab. Rather than creating a post with dozens of links, I thought I'd just write up a tutorial with | The UNIX and Linux Forums

CC Chen wrote in post #1039530:

> **···**
>
> > Hi all,
> > 
> > I used crontab command such as  
> > "\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb"
> > 
> > But it can't execute script1.rb, I let it print some message every 5  
> > min.
> > 
> > I also changed script1.rb to chmod 755.
> > 
> > Does the crontab command is right?
> > 
> > Thanks.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [6 January 2012 00:09 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/4 "2012-01-06T00:09:01Z")

</div>

Run 'env -i $SHELL --norc'. It will bring up a new naked shell that is equivalent to what cron runs. Run your command as-is in this shell to ensure it works correctly.

> **···**
>
> On Jan 5, 2012, at 02:30 , CC Chen wrote:
> 
> > Hi all,
> > 
> > I used crontab command such as  
> > "\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb"
> > 
> > But it can't execute script1.rb, I let it print some message every 5  
> > min.
> > 
> > I also changed script1.rb to chmod 755.
> > 
> > Does the crontab command is right?

---

<div class="post-metadata">

**Author:** ![CC\_Chen](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@CC\_Chen](https://rubytalk.org/u/CC_Chen)\
**Post date:** [6 January 2012 02:39 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/5 "2012-01-06T02:39:13Z")

</div>

CC Chen wrote in post #1039530:

> Hi all,
> 
> I used crontab command such as  
> "\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb"
> 
> But it can't execute script1.rb, I let it print some message every 5  
> min.
> 
> I also changed script1.rb to chmod 755.
> 
> Does the crontab command is right?
> 
> Thanks.

I write a script which it can link to ftp server and to download files.

I also used this command  
"\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/ftpscript.rb

> > /local/myuser/rubyscript/ftplog.txt"

It just can dump ftplog.txt file but it can't download any file from the  
ftp server.

What should I do??

Thanks,

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Zachary\_Scott2](https://avatars.discourse-cdn.com/v4/letter/z/ccd318/32.png) [@Zachary\_Scott2](https://rubytalk.org/u/Zachary_Scott2)\
**Post date:** [5 January 2012 22:05 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/6 "2012-01-05T22:05:15Z")

</div>

You could also try using [whenever]([http://github.com/javan/whenever\](http://github.com/javan/whenever%5C))

> **···**
>
> On Thu, Jan 5, 2012 at 10:34 AM, Giampiero Zanchi \<cidza@tin.it\> wrote:
> 
> > I am quite noob  
> > however...  
> > if your script does use gui, maybe you must use  
> > DISPLAY=:0.0  
> > \*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb
> > 
> > a useful link may be  
> > [cron and crontab](http://www.unix.com/answers-frequently-asked-questions/13527-cron-crontab.html)
> > 
> > CC Chen wrote in post #1039530:
> > 
> > > Hi all,
> > > 
> > > I used crontab command such as  
> > > "\*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb"
> > > 
> > > But it can't execute script1.rb, I let it print some message every 5  
> > > min.
> > > 
> > > I also changed script1.rb to chmod 755.
> > > 
> > > Does the crontab command is right?
> > > 
> > > Thanks.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![CC\_Chen](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@CC\_Chen](https://rubytalk.org/u/CC_Chen)\
**Post date:** [6 January 2012 02:18 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/7 "2012-01-06T02:18:41Z")

</div>

Venkatakrishnan Ganesh wrote in post #1039547:

> Looks ok. I don't think you need execute permission either, just read  
> permission would do, because you're passing the file to the interpreter.
> 
> Verify that:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Interpreter path is correct  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Program does not have errors and you are able to run it yourself  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* It has correct permissions for accessing files etc as the user  
> you've setup the cron for  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* If you're just printing stuff remember it doesn't have $stdout or  
> $stderr to write to, redirect those to some log file
> 
> Hope this helps

Thanks,

I changed to

" \*/5 \* \* \* \* /usr/local/bin/ruby /local/myuser/rubyscript/script1.rb \>\>  
/local/myuser/rubyscript/log.txt "

It find the message dump on log.txt, but does it can't print out  
directly??

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![CC\_Chen](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@CC\_Chen](https://rubytalk.org/u/CC_Chen)\
**Post date:** [6 January 2012 02:20 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/8 "2012-01-06T02:20:32Z")

</div>

Ryan Davis wrote in post #1039638:

> **···**
>
> > On Jan 5, 2012, at 02:30 , CC Chen wrote:
> > 
> > > Does the crontab command is right?
> > 
> > Run 'env -i $SHELL --norc'. It will bring up a new naked shell that is  
> > equivalent to what cron runs. Run your command as-is in this shell to  
> > ensure it works correctly.
> 
> So I add this command 'env -i $SHELL --norc' on crontab directly?
> 
> Thanks,
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [9 January 2012 10:00 UTC](https://rubytalk.org/t/crontab-and-ruby-script/64528/9 "2012-01-09T10:00:01Z")

</div>

No. This was intended for testing only.

Btw, you do not have those quotes at the beginning and end of the line  
in your crontab, do you? If yes, you must remove them.

Kind regards

robert

> **···**
>
> On Fri, Jan 6, 2012 at 3:20 AM, CC Chen \<dickyhide@gmail.com\> wrote:
> 
> > Ryan Davis wrote in post #1039638:
> > 
> > > On Jan 5, 2012, at 02:30 , CC Chen wrote:
> > > 
> > > > Does the crontab command is right?
> > > 
> > > Run 'env -i $SHELL --norc'. It will bring up a new naked shell that is  
> > > equivalent to what cron runs. Run your command as-is in this shell to  
> > > ensure it works correctly.
> > 
> > So I add this command 'env -i $SHELL --norc' on crontab directly?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
