Forum: Ruby on Rails rake not working when called through cron

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-04-23 07:31
This is currently the contents of "app-backup" in my cron.daily.

--
#!/bin/sh
# Cron Job for Mixbook Code Backup
#
# Updated February 1, 2007

cd /var/rails/beta
rake s3:backup:scm --trace >> /var/rails/beta/log/backup.log
cd /var/rails/beta/log
echo "Backup Successful on" >> backup.log
date >> backup.log
echo "--" >> backup.log
--

When I run the file "./app-backup," the RAKE works fine and completes
successfully. However, when this same file is called through crontab, I
get the following output:

--
(in /var/rails/beta)
rake aborted!
Don't know how to build task 's3:backup:scm'

(See full trace by running task with --trace)
Backup Successful on
Sat Apr 21 04:02:39 PDT 2007
--


Anybody have any clue why this is happening?
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2007-04-23 20:19
>
> When I run the file "./app-backup," the RAKE works fine and completes
> successfully. However, when this same file is called through crontab, I
> get the following output:
>
> --
> (in /var/rails/beta)
> rake aborted!
> Don't know how to build task 's3:backup:scm

Are you sure you don't have a file permission or path problem?

Stephan
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-04-24 00:51
hmm, doesnt the cron job work as Root? The thing is, if I got a
permissions problem, it would say that it couldnt even run the file in
the first place, but its clearly calling the Rake command.

I definately doubt its a path problem because I can run that same file
manually just fine.

Stephan Wehner wrote:
>>
>> When I run the file "./app-backup," the RAKE works fine and completes
>> successfully. However, when this same file is called through crontab, I
>> get the following output:
>>
>> --
>> (in /var/rails/beta)
>> rake aborted!
>> Don't know how to build task 's3:backup:scm
>
> Are you sure you don't have a file permission or path problem?
>
> Stephan
8fec3199fc2e0c7a81a3484455b1f556?d=identicon&s=25 Lukhnos D. Liu (Guest)
on 2007-04-24 00:58
(Received via mailing list)
On 4/24/07, Aryk Grosz <rails-mailing-list@andreas-s.net> wrote:
> hmm, doesnt the cron job work as Root? The thing is, if I got a
> permissions problem, it would say that it couldnt even run the file in
> the first place, but its clearly calling the Rake command.

Each user owns his/her crontab. Another possibility might have to do
with your environment settings, though. Have you tried putting what
you've got in your shell profile in the crontab?

d.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-04-24 05:26
I believe Im running all this under root. I think I narrowed down the
problem a little though. When I run rake -T, I get a list of all the
available rake commands. The one i need to run isnt in there. It is only
in there when Im logged in.

This has something to do with the fact that the *.rake file is ran from
the vendors directory. It is ran from a plugin under the /tasks folder.
For some reason, when run from cron, it doesnt check the rake tasks from
the Rails plugins.

Any idea why?

I looked in the .profile and bashrc and there is nothing there that I
think could affect this. Where else should I look?

Lukhnos D. Liu wrote:
> On 4/24/07, Aryk Grosz <rails-mailing-list@andreas-s.net> wrote:
>> hmm, doesnt the cron job work as Root? The thing is, if I got a
>> permissions problem, it would say that it couldnt even run the file in
>> the first place, but its clearly calling the Rake command.
>
> Each user owns his/her crontab. Another possibility might have to do
> with your environment settings, though. Have you tried putting what
> you've got in your shell profile in the crontab?
>
> d.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-04-24 06:31
It turns out that this issue is related to one line in the s3.rake file.
It actually is getting found, but the problem is, that there is an error
thrown, and rather than telling me, it says it cannot find the task.

Here is whats happening:

There is a line at the top- "require File.join(File.dirname(__FILE__),
'..')+'/lib/s3'"

Through testing, I found out that this is the problem line. I believe in
the original code it was requir 's3'. I changed it to what it is now
because it couldnt find the file before.

I checked the global variable &: for the lib directory and it is
include, I just don't understand why it cant find it. That would solve
my problem. For some reason, when calling Rake from cron, it doesnt like
the File.join or File.dirname command.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-04-24 07:10
Ok, I think I pin pointed this thing down a little more.

Its not the File command as I thought. It is calling s3.rb.

In that file, there are requirements that dont work when called from
cron.

All of these required rbs are not found or throw an error when run
through cron.

require 'openssl'
require 'digest/sha1'
require 'net/https'

Do you guys have any idea why these required libraries would throw
errors only when Rake is called through cron?
Cb2b768a5e546b24052ea03334e43676?d=identicon&s=25 Dr Nic (nicwilliams)
on 2007-05-01 14:52
Some thoughts on environment variables not available in cron:

http://kb.mediatemple.net/article.php?id=251
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-05-01 22:33
I've looked at that. While the RUBYLIB declaration definately helped, I
was running into a problem within the rake code. I think the popen
command was giving me problems through cron. It was trying to run "svn
info". Have you guys ever heard of this problem?

Dr Nic wrote:
>
> Some thoughts on environment variables not available in cron:
>
> http://kb.mediatemple.net/article.php?id=251
This topic is locked and can not be replied to.