# Crontab and ruby script not working

**URL:** <https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426>\
**Category:** ruby-talk\
**Created:** [4 September 2013 13:41 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426 "2013-09-04T13:41:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 September 2013 13:41 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/1 "2013-09-04T13:41:38Z")

</div>

I have a ruby script that I am trying to set up on a cronjob. I have  
tested it both just running it in the normal mode and a test shell mode.

When I am logged in as a normal user or in 'env -i $SHELL --norc' I will  
run ruby /home/salinn/dev/ruby/webmail/script.rb and it works in both  
environments. Could someone help point me out what I am doing wrong?

My crontab looks like this:

\* \* \* \* \* ruby /home/salinn/dev/ruby/webmail/script.rb

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

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/peter_hickman2/32/1832_2.png) [@Peter\_Hickman2](https://rubytalk.org/u/Peter_Hickman2)\
**Post date:** [4 September 2013 14:14 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/2 "2013-09-04T14:14:52Z")

</div>

Have you tried giving the full path to Ruby? /usr/local/bin/ruby or  
whatever it is on your machine?

> **···**
>
> On 4 September 2013 14:41, Paul Darragh \<lists@ruby-forum.com\> wrote:
> 
> > I have a ruby script that I am trying to set up on a cronjob. I have  
> > tested it both just running it in the normal mode and a test shell mode.
> > 
> > When I am logged in as a normal user or in 'env -i $SHELL --norc' I will  
> > run ruby /home/salinn/dev/ruby/webmail/script.rb and it works in both  
> > environments. Could someone help point me out what I am doing wrong?
> > 
> > My crontab looks like this:
> > 
> > \* \* \* \* \* ruby /home/salinn/dev/ruby/webmail/script.rb
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [4 September 2013 14:13 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/3 "2013-09-04T14:13:49Z")

</div>

To make sure it is not an issue with the PATH environment variable  
(cron may have its own?), try invoking Ruby with the full path,  
e.g. /usr/bin/ruby (you can find out with 'which ruby').

Regards,  
Marcus

> **···**
>
> Am 04.09.2013 15:41, schrieb Paul Darragh:
> 
> > I have a ruby script that I am trying to set up on a cronjob. I have  
> > tested it both just running it in the normal mode and a test shell mode.
> > 
> > When I am logged in as a normal user or in 'env -i $SHELL --norc' I will  
> > run ruby /home/salinn/dev/ruby/webmail/script.rb and it works in both  
> > environments. Could someone help point me out what I am doing wrong?
> > 
> > My crontab looks like this:
> > 
> > \* \* \* \* \* ruby /home/salinn/dev/ruby/webmail/script.rb
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 September 2013 16:54 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/4 "2013-09-04T16:54:03Z")

</div>

I have tried to give it the specific Path to ruby and loadingin the path  
from the profile I am using. It is still not working only when I run it  
as a cronjob.

This is what the cronjob now looks like:

PATH="$HOME/bin:$PATH"  
\* \* \* \* \* /usr/bin/ruby /home/salinn/dev/ruby/webmail/script.rb

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

---

<div class="post-metadata">

**Author:** ![Panagiotis\_Atmatzidi](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@Panagiotis\_Atmatzidi](https://rubytalk.org/u/Panagiotis_Atmatzidi)\
**Post date:** [4 September 2013 19:00 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/5 "2013-09-04T19:00:35Z")

</div>

Hello,

> I have a ruby script that I am trying to set up on a cronjob. I have  
> tested it both just running it in the normal mode and a test shell mode.
> 
> When I am logged in as a normal user or in 'env -i $SHELL --norc' I will  
> run ruby /home/salinn/dev/ruby/webmail/script.rb and it works in both  
> environments. Could someone help point me out what I am doing wrong?
> 
> My crontab looks like this:
> 
> \* \* \* \* \* ruby /home/salinn/dev/ruby/webmail/script.rb
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

This was my crontab at a Gentoo system with fcron (a more powerful solution than cron) but same works for other systems

NOTE: I use 'rvm'

# atmat's crontab configuration  
SHELL=/bin/bash  
PATH=/home/atma/.rvm/gems/ruby-1.9.3-p0/bin:/home/atma/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/atma/.rvm/rubies/ruby-1.9.3-p0/bin:/home/atma/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i486-pc-linux-gnu/gcc-bin/4.5.3  
RUBYLIB=/home/atma/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1  
GEM\_HOME='/home/atma/.rvm/gems/ruby-1.9.3-p0'GEM\_PATH='/home/atma/.rvm/gems/ruby-1.9.3-p0:/home/atma/.rvm/gems/ruby-1.9.3-p0@global'  
RUBYOPT=rubygems

%nightly,mail(no) \* 8-9 /home/atma/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /usr/local/bin/morula -s username update

You can get the idea.

Panagiotis (atmosx) Atmatzidis

email: atma@convalesco.org  
URL: [http://www.convalesco.org](http://www.convalesco.org)  
GnuPG ID: 0x1A7BFEC5  
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

> **···**
>
> On 4 Σεπ 2013, at 15:41 , Paul Darragh \<lists@ruby-forum.com\> wrote:  
> --  
> The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."

---

<div class="post-metadata">

**Author:** ![Reid\_Thompson1](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Reid\_Thompson1](https://rubytalk.org/u/Reid_Thompson1)\
**Post date:** [4 September 2013 14:27 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/6 "2013-09-04T14:27:33Z")

</div>

cron uses a very minimal path.  
you may need to explicitely set the PATH at the top of your crontab, or  
give the full path to any exes that you utilize

> **···**
>
> On Wed, 2013-09-04 at 16:13 +0200, sto.mar@web.de wrote:
> 
> > Am 04.09.2013 15:41, schrieb Paul Darragh:  
> > \> I have a ruby script that I am trying to set up on a cronjob. I have  
> > \> tested it both just running it in the normal mode and a test shell mode.  
> > \>  
> > \> When I am logged in as a normal user or in 'env -i $SHELL --norc' I will  
> > \> run ruby /home/salinn/dev/ruby/webmail/script.rb and it works in both  
> > \> environments. Could someone help point me out what I am doing wrong?  
> > \>  
> > \> My crontab looks like this:  
> > \>  
> > \> \* \* \* \* \* ruby /home/salinn/dev/ruby/webmail/script.rb
> > 
> > To make sure it is not an issue with the PATH environment variable  
> > (cron may have its own?), try invoking Ruby with the full path,  
> > e.g. /usr/bin/ruby (you can find out with 'which ruby').
> > 
> > Regards,  
> > Marcus

---

<div class="post-metadata">

**Author:** ![Reid\_Thompson1](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Reid\_Thompson1](https://rubytalk.org/u/Reid_Thompson1)\
**Post date:** [4 September 2013 17:55 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/7 "2013-09-04T17:55:13Z")

</div>

Don't use $HOME -- give the full path name

PATH=/path/to/user/home/bin:$PATH

reid

> **···**
>
> On Wed, 2013-09-04 at 18:54 +0200, Paul Darragh wrote:
> 
> > I have tried to give it the specific Path to ruby and loadingin the path  
> > from the profile I am using. It is still not working only when I run it  
> > as a cronjob.
> > 
> > This is what the cronjob now looks like:
> > 
> > PATH="$HOME/bin:$PATH"  
> > \* \* \* \* \* /usr/bin/ruby /home/salinn/dev/ruby/webmail/script.rb

---

<div class="post-metadata">

**Author:** ![Reid\_Thompson1](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Reid\_Thompson1](https://rubytalk.org/u/Reid_Thompson1)\
**Post date:** [4 September 2013 20:29 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/8 "2013-09-04T20:29:13Z")

</div>

05 00 \* \* \* /mnt/nfs/dataproc\_work/applhome/cadredd/rubybin/rvm\_cronit ruby /path/to/somerubyscript.rb "param1" "param2" etc  
33 06 \* \* 1-6 /mnt/nfs/dataproc\_work/applhome/cadredd/rubybin/rvm\_cronit /path/to/some/shell\_script\_that\_includes\_a\_call\_to\_a\_ruby\_script.sh "param1" "param2" etc

$ cat /home/${user}/bin/rvm\_cronit  
#!/bin/bash

> **···**
>
> On Wed, 2013-09-04 at 21:00 +0200, Panagiotis Atmatzidis wrote:
> 
> > Hello,
> > 
> > On 4 Σεπ 2013, at 15:41 , Paul Darragh \<lists@ruby-forum.com\> wrote:
> > 
> > \> I have a ruby script that I am trying to set up on a cronjob. I have  
> > \> tested it both just running it in the normal mode and a test shell mode.  
> > \>  
> > \> When I am logged in as a normal user or in 'env -i $SHELL --norc' I will  
> > \> run ruby /home/salinn/dev/ruby/webmail/script.rb and it works in both  
> > \> environments. Could someone help point me out what I am doing wrong?  
> > \>  
> > \> My crontab looks like this:  
> > \>  
> > \> \* \* \* \* \* ruby /home/salinn/dev/ruby/webmail/script.rb  
> > \>  
> > \> --  
> > \> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > This was my crontab at a Gentoo system with fcron (a more powerful solution than cron) but same works for other systems
> > 
> > NOTE: I use 'rvm'
> > 
> > # atmat's crontab configuration  
> > SHELL=/bin/bash  
> > PATH=/home/atma/.rvm/gems/ruby-1.9.3-p0/bin:/home/atma/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/atma/.rvm/rubies/ruby-1.9.3-p0/bin:/home/atma/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i486-pc-linux-gnu/gcc-bin/4.5.3  
> > RUBYLIB=/home/atma/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1  
> > GEM\_HOME='/home/atma/.rvm/gems/ruby-1.9.3-p0'GEM\_PATH='/home/atma/.rvm/gems/ruby-1.9.3-p0:/home/atma/.rvm/gems/ruby-1.9.3-p0@global'  
> > RUBYOPT=rubygems
> > 
> > %nightly,mail(no) \* 8-9 /home/atma/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /usr/local/bin/morula -s username update
> > 
> > You can get the idea.
> > 
> > Panagiotis (atmosx) Atmatzidis
> > 
> > email: atma@convalesco.org  
> > URL: [http://www.convalesco.org](http://www.convalesco.org)  
> > GnuPG ID: 0x1A7BFEC5  
> > gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5  
> > --  
> > The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."
> 
> ###############################################################################  
> #  
> lclcmd=`basename $0`  
> #  
> if ["$1" = "-h"]; then  
> &nbsp;&nbsp;&nbsp;&nbsp;Description  
> fi  
> minargv=1  
> if [$# -lt $minargv]; then  
> &nbsp;&nbsp;&nbsp;&nbsp;echo "Invalid number of parameters specified."  
> &nbsp;&nbsp;&nbsp;&nbsp;Description  
> fi  
> #  
> ###############################################################################  
> # set up some standard variables  
> ###############################################################################  
> #  
> exitsts=0  
> #  
> ###############################################################################  
> # validate parameters  
> ###############################################################################  
> #  
> # N/A  
> #  
> ###############################################################################  
> # run the job  
> ###############################################################################  
> #  
> cd /home/${user}  
> . /home/${user}/.profile  
> envfile=`/usr/local/rvm/bin/rvm env --path`  
> . $envfile  
> #  
> $\*  
> cmdsts=$?  
> if [$cmdsts -ne 0]; then  
> &nbsp;&nbsp;&nbsp;&nbsp;exitsts=1  
> &nbsp;&nbsp;&nbsp;&nbsp;echo "cronit: ERROR"  
> &nbsp;&nbsp;&nbsp;&nbsp;echo "quitting $lclcmd"; exit $exitsts  
> fi  
> #  
> exit $exitsts
> 
> --  
> If you have received the message in error, please advise the sender by  
> reply email and please delete the message. This message contains  
> information which may be confidential or otherwise protected. Unless  
> you are the addressee (or authorized to receive for the addressee), you  
> may not use, copy, or disclose to anyone the message or any information  
> contained in the message.

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [5 September 2013 04:29 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/9 "2013-09-05T04:29:26Z")

</div>

"It isn't working" isn't really very descriptive. What errors are you getting?

> **···**
>
> On Sep 4, 2013, at 11:54 AM, Paul Darragh \<lists@ruby-forum.com\> wrote:
> 
> > I have tried to give it the specific Path to ruby and loadingin the path  
> > from the profile I am using. It is still not working only when I run it  
> > as a cronjob.
> > 
> > This is what the cronjob now looks like:
> > 
> > PATH="$HOME/bin:$PATH"  
> > \* \* \* \* \* /usr/bin/ruby /home/salinn/dev/ruby/webmail/script.rb
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 September 2013 12:26 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/10 "2013-09-05T12:26:07Z")

</div>

tamouse m. wrote in post #1120716:

> **···**
>
> > On Sep 4, 2013, at 11:54 AM, Paul Darragh \<lists@ruby-forum.com\> wrote:
> > 
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > "It isn't working" isn't really very descriptive. What errors are you  
> > getting?
> 
> The problem is I am not seeing any errors besides that it is not  
> running. I can see in syslog that it runs it.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [6 September 2013 02:25 UTC](https://rubytalk.org/t/crontab-and-ruby-script-not-working/68426/11 "2013-09-06T02:25:46Z")

</div>

So you see in syslog that it runs. But you somehow detect that it's not running, which I assume means you can't see the effect from it.

Are you getting an email from cron with any job output at all?

Given your crontab:

PATH="$HOME/bin:$PATH"  
\* \* \* \* \* /usr/bin/ruby /home/salinn/dev/ruby/webmail/script.rb

You should be seeing an email with the combined stdout/stdin from that job showing up once a minute. If not, change it to something like:

PATH="$HOME/bin:$PATH"  
\* \* \* \* \* /usr/bin/ruby /home/salinn/dev/ruby/webmail/script.rb \>\> $HOME/webmailscript.log 2\>&1

to ensure you're getting the output, if any.

You might add a cron job that is simply:

\* \* \* \* \* set \> $HOME/crontab-env.txt 2\>&1

to see the environment your cronjob is actually running in. After that cronjob runs, the file should appear in your home directory. You can remove the cronjob after that. Inspect the environment, see if there's anything missing you need.

> **···**
>
> On Sep 5, 2013, at 7:26 AM, Paul Darragh \<lists@ruby-forum.com\> wrote:
> 
> > tamouse m. wrote in post #1120716:
> > 
> > > On Sep 4, 2013, at 11:54 AM, Paul Darragh \<lists@ruby-forum.com\> wrote:
> > > 
> > > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > > 
> > > "It isn't working" isn't really very descriptive. What errors are you  
> > > getting?
> > 
> > The problem is I am not seeing any errors besides that it is not  
> > running. I can see in syslog that it runs it.
