# Syntax errors when job run from cron

**URL:** <https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742>\
**Category:** Ruby\
**Created:** [August 21, 2006, 2:25am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742 "2006-08-21T02:25:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 2:25am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/1 "2006-08-21T02:25:53Z")

</div>

Well this is weird! I’m used to odd problems when you run programs from  
cron but these are almost invariably to do with file permissions or  
stuff in the environment variables not matching the command line  
environment.

I’m using ruby 1.8.4 (2005-12-24) [i686-linux] on a RHE 3.0 system.

The program runs fine from command line but gets nonsensical syntax  
errors when run from cron.

Any idea where to look for the problem?

Russell

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 2:33am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/2 "2006-08-21T02:33:50Z")

</div>

On 8/21/06, Russell F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any idea where to look for the problem?  
> Check your paths, maybe?

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 3:21am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/3 "2006-08-21T03:21:01Z")

</div>

Daniel B. wrote:

> On 8/21/06, Russell F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Any idea where to look for the problem?  
> > Check your paths, maybe?

All the ruby files are in the same directory which the job cds into  
before invoking the program. I’ve just added an ‘env’ to the job to  
compare all the environment variables. Should have though of that  
before…

Russell

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 4:15am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/4 "2006-08-21T04:15:56Z")

</div>

On Mon, 21 Aug 2006, Russell F. wrote:

> Any idea where to look for the problem?
> 
> Russell

probably you have two ruby installations and the wrong one is running  
your  
script.

-a

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 5:47am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/5 "2006-08-21T05:47:01Z")

</div>

James B. wrote:

> Russell F. wrote:
> 
> > Any idea where to look for the problem?  
> > What is the user that actually runs the job in cron?
> 
> Are you using absolute paths?
> 
> #!/usr/local/bin/ruby
> 
> not
> 
> #!/usr/bin/env ruby

we are getting close here! I’ve managed to reproduce the error on the  
cli by executing the file directly instead of running ruby

i.e  
selms.rb … dies  
whereas  
ruby selms.rb … works fine.

file starts with:

#! /usr/bin/ruby

[rful011@pateke selms]$ ls -l /usr/bin/ruby  
-rwxr-xr-x 1 root root 3548 Oct 5 2005 /usr/bin/ruby

cron job looks like this:

40 \* \* \* \* cd /home/rful011/selms; ruby ./selms.rb -M -o  
selms -t periodic log.patterns

note that the script is invoked via ruby in the cron job but gives the  
same symptoms as the file being executed directly.

Here is the output from the cron job:

SHELL=/bin/sh  
MAILTO=removed\_email\_address@domain.invalid  
SHEll=/bin/sh  
OLDPWD=/home/logowner  
USER=logowner  
PATH=/usr/bin:/bin  
PWD=/home/rful011/selms  
SHLVL=1  
HOME=/home/logowner  
LOGNAME=logowner  
\_=/usr/bin/env  
./selms.rb:55: syntax error  
when ‘periodic’ : Periodic.new( conf, $options[:syntax])  
^  
./selms.rb:56: syntax error  
when ‘realtime’ :  
^

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 5:14am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/6 "2006-08-21T05:14:03Z")

</div>

Russell F. wrote:

> Any idea where to look for the problem?  
> What is the user that actually runs the job in cron?

Are you using absolute paths?

#!/usr/local/bin/ruby

not

#!/usr/bin/env ruby

in the script (since env will differ for different users)?

When run from the CLI are there RUBYOPT values (e.g., rubygems) in the  
environment that are not there for cron?

–  
James B.

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://refreshingcities.org](http://refreshingcities.org) - Design, technology, usability

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 6:40am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/7 "2006-08-21T06:40:10Z")

</div>

unknown wrote:

> On Mon, 21 Aug 2006, Russell F. wrote:
> 
> > > 
> > 
> > file starts with:
> > 
> > #! /usr/bin/ruby
> 
> you have 1.8.4 installed in one place, probably /usr/local/bin/ and  
> 1.6.8 in

Bugger! you’re dead right!

[rful011@pateke selms]$ whereis ruby  
ruby: /usr/bin/ruby /usr/lib/ruby /usr/local/bin/ruby  
/usr/local/lib/ruby /usr/share/man/man1/ruby.1.gz  
[rful011@pateke selms]$ ls -l /usr/local/bin/ruby  
-rwxr-xr-x 1 root root 1214420 Jul 24 16:28  
/usr/local/bin/ruby

I was using darwin ports on a mac to install FreeRide and aborted it  
when I realised it was installing its copy of Ruby. I though I had got  
rid of it, but clearly not!

Now I’ll figure out why 1.8.4 is generating syntax errors.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [August 21, 2006, 6:18am UTC](https://www.ruby-forum.com/t/syntax-errors-when-job-run-from-cron/65742/8 "2006-08-21T06:18:06Z")

</div>

On Mon, 21 Aug 2006, Russell F. wrote:

> > 
> 
> file starts with:
> 
> #! /usr/bin/ruby

you have 1.8.4 installed in one place, probably /usr/local/bin/ and  
1.6.8 in  
/usr/bin

type

which -a ruby

and

/usr/bin/ruby -v  
/usr/local/bin/ruby -v

probably you should change the shebang line in your script. alternately  
you  
can set the PATH in your crontab like so

PATH=/usr/local/bin:$PATH

regards.

-a
