Welcome Guest | Login

Running a rake task from cron?

Could someone post an example cron item that would run a rake task in a particular application at daily intervals? (or otherwise describe the easiest way to achieve that). thanks!

2007-05-21 04:13 AM

Hi  Bitbutter ,

You will need to put the required rake command in a .sh file and tell us the exact path where you have put this file in your account. We will write the required cron entry for the same. Also do mention the time specifications (when you want to run the same).

2007-05-21 04:46 AM

htanks varun,

if i put the .sh file in my app in the lib/tasks directory, do i need to prefix the rake command with a path?

At the moment i have this:

myapp/lib/tasks/notify_answerers
rake notify_answerers

2007-05-21 05:07 AM

Hi Bitbutter,

all you need in your crontab is something like

23 1 * * * /home/username/somescript
This will execute at 1:23AM every day

and somescript being
cd /home/username/railsapp
rake your_rake_task RAILS_ENV=production

2007-05-21 05:17 AM

ah that's great thanks!

2007-05-21 05:38 AM

mm i'm getting a complaint from cron (by email) that the rake command wasn't found. (i confirmed that running rake notify_answerers does work correctly if i run it from my application root manually.)

/home/username/apps/myapp/current/lib/tasks/notify_answerers.sh: line 2: rake: command not found
my crontab line looks like this:
* * * * * /home/username/apps/myapp/current/lib/tasks/notify_answerers.sh
and here's how the .sh file looks:
cd /home/username/apps/myapp/current
rake notify_answerers RAILS_ENV=production
any ideas?

2007-05-21 07:59 AM

Ok - then I'm guessing you'll need the full path

cd /home/username/apps/myapp/current
/usr/local/bin/ruby rake notify_answerers RAILS_ENV=production

2007-05-21 08:05 AM

And actually - I'm sorry for the confusion on this.  I believe you actually can do it all with just this cron:

23 1 * * * cd /home/username/apps/myapp/current && /usr/local/bin/ruby rake notify_answerers RAILS_ENV=production 

2007-05-21 08:13 AM

thanks. I've now done it the way you mentioned in the latest post. Getting a different error:
/usr/local/bin/ruby: No such file or directory -- rake (LoadError)

2007-05-21 08:30 AM

hmmm - we're testing it out on your server now -- we'll post back here in a minute.  

2007-05-21 09:16 AM

Hi  Bitbutter,

Could you please try as follows and see if it works:

Put the following lines in a file (somescript.sh)

#!/bin/sh
cd /home/username/apps/myapp/current
/usr/local/bin/ruby rake notify_answerers RAILS_ENV=production
Now chmod the file to 755

# chmod 755 somescript.sh
Set the cronjob as folows:

23 1 * * * /bin/sh /home/username/somescript.sh

2007-05-21 09:57 AM

HostingRails Support
that results in the following error
/usr/local/bin/ruby: No such file or directory -- rake (LoadError)
(incase it makes a difference my .sh script is in the lib/tasks folder of my app and the paths are adjusted accordingly)

2007-05-21 10:08 AM

Hello  Bitbutter,

Could you please try changing the file somescript.sh as given below:

#!/bin/sh
cd /home/username/apps/myapp/current
/usr/local/bin/rake notify_answerers RAILS_ENV=production

2007-05-21 10:50 AM

HostingRails Support
Hi, that seems to be working, thanks for looking into it!

summary:

in the root of my app i have notify_answerers.sh
#!/bin/sh
cd /home/username/apps/myapp/current
/usr/local/bin/rake notify_answerers RAILS_ENV=production
in myapp/lib/tasks i have myapp.rake
desc "Notify answerers of new questions"
task :notify_answerers => :environment do
# code to send notifications etc here
end
In my cron file i have:
23 1 * * * /bin/sh /home/username/apps/myapp/current/notify_answerers.sh
(use "crotab -e" on the server to edit this file)

2007-05-21 11:00 AM

I've followed these instructions but I get the folowing email notification:

/usr/bin/env: ruby: No such file or directory


The only difference is that the .sh file is in my home dir, not my application dir

2008-04-26 05:55 AM

Could you try changing the path to rake as below ?

#!/bin/sh
cd /home/username/apps/app/current
/usr/local/bin/rake utils:update:all RAILS_ENV=production

2008-04-26 06:18 AM

yeah, I just saw my typo in the script (ruby instead of rake) and was about to update my entry.

Thanks for the help Varun.

2008-04-26 07:11 AM

@Varun - could you please edit your post and remove my user information?

Thanks.

2008-04-26 07:14 AM

Redseat - it's been done. Thanks

2008-04-26 07:49 AM

You can also use this command for your cron job (instead of calling a script):

bash -c 'cd path/to/your/app/; /usr/local/bin/rake taskname RAILS_ENV=production'

2008-05-14 05:45 AM


Hello Guest! In order to post you must be an active client with us, please log in or sign up today!