Running a rake task from cron?
- Bitbutter
- Posts: 32
- Starts: 14
- Wiki Edits: 2
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!
- Varun
- Posts: 410
- Starts: 0
- Wiki Edits: 0
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).
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
- Bitbutter
- Posts: 32
- Starts: 14
- Wiki Edits: 2
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
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
- William
- Posts: 1010
- Starts: 32
- Wiki Edits: 56
Hi Bitbutter,
all you need in your crontab is something like
and somescript being
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
- Bitbutter
- Posts: 32
- Starts: 14
- Wiki Edits: 2
ah that's great thanks!
2007-05-21 05:38 AM
- Bitbutter
- Posts: 32
- Starts: 14
- Wiki Edits: 2
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
- William
- Posts: 1010
- Starts: 32
- Wiki Edits: 56
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
cd /home/username/apps/myapp/current
/usr/local/bin/ruby rake notify_answerers RAILS_ENV=production
2007-05-21 08:05 AM
- William
- Posts: 1010
- Starts: 32
- Wiki Edits: 56
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
- Bitbutter
- Posts: 32
- Starts: 14
- Wiki Edits: 2
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
- William
- Posts: 1010
- Starts: 32
- Wiki Edits: 56
hmmm - we're testing it out on your server now -- we'll post back here in a minute.
2007-05-21 09:16 AM
- Kumar
- Posts: 423
- Starts: 0
- Wiki Edits: 5
Hi Bitbutter,
Could you please try as follows and see if it works:
Put the following lines in a file (somescript.sh)
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- Bitbutter
- Posts: 32
- Starts: 14
- Wiki Edits: 2
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
- Kumar
- Posts: 423
- Starts: 0
- Wiki Edits: 5
Hello Bitbutter,
Could you please try changing the file somescript.sh as given below:
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- Bitbutter
- Posts: 32
- Starts: 14
- Wiki Edits: 2
Hi, that seems to be working, thanks for looking into it!
summary:
in the root of my app i have notify_answerers.sh
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.rakedesc "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
- Redseat
- Posts: 4
- Starts: 3
- Wiki Edits: 0
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
/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
- Varun
- Posts: 410
- Starts: 0
- Wiki Edits: 0
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
#!/bin/sh
cd /home/username/apps/app/current
/usr/local/bin/rake utils:update:all RAILS_ENV=production
2008-04-26 06:18 AM
- Redseat
- Posts: 4
- Starts: 3
- Wiki Edits: 0
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.
Thanks for the help Varun.
2008-04-26 07:11 AM
- Redseat
- Posts: 4
- Starts: 3
- Wiki Edits: 0
@Varun - could you please edit your post and remove my user information?
Thanks.
Thanks.
2008-04-26 07:14 AM
- Varun
- Posts: 410
- Starts: 0
- Wiki Edits: 0
Redseat - it's been done. Thanks
2008-04-26 07:49 AM
- Redseat
- Posts: 4
- Starts: 3
- Wiki Edits: 0
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'
bash -c 'cd path/to/your/app/; /usr/local/bin/rake taskname RAILS_ENV=production'