# Running a rake task from cron on a shared webhost (joyent)

**URL:** <https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690>\
**Category:** Rails\
**Created:** [August 31, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690 "2009-08-31T14:52:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 31, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/1 "2009-08-31T14:52:55Z")

</div>

I’ve created a cron task via my webhost’s (a joyent shared accelerator)  
cron control panel. The control panel has a “run now” button, when I hit  
this button the thing works, I get my nice backup file created in /db,  
all fine. Thing is when I leave the job to run over night, all it  
creates is an empty db backup file!

Here’s the the crontab line…

11 3 \* \* \* cd /users/home/foo/domains/foo.com/web &&  
RAILS\_ENV=production /usr/local/bin/rake db:mysql:backup #foo nightly  
backup

What gives?

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 31, 2009, 3:07pm UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/2 "2009-08-31T15:07:15Z")

</div>

2009/8/31 bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 11 3 \* \* \* cd /users/home/foo/domains/foo.com/web &&  
> RAILS\_ENV=production /usr/local/bin/rake db:mysql:backup #foo nightly  
> backup

Could be an issue with not being logged in? Permissions or path or  
something?

Colin

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 31, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/3 "2009-08-31T15:13:19Z")

</div>

Maybe, just unsure what. Annoying as it’s stopping me doing lots of rake  
stuff on a schedule.

I mean I know there are solutions to run background tasks in the app but  
I didn’t really want the memory overhead for now and this seems like a  
quick win. If “only” I could get it working!

Any ideas what else I could try anyone?

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 31, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/4 "2009-08-31T17:04:21Z")

</div>

Are you not able to see the output of the cron job? The output should  
be logged somewhere or sent in an email - that depends on your hosting  
environment.

If you can’t find that, you could move your command to a shell script  
that is referenced by the crontab.  
You can add code in that script to inspect the environment and/or  
capture the output of your rake command in your own log file.  
But you could use a little more information to help track down the  
problem.

On Aug 31, 6:13 am, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [September 2, 2009, 10:27am UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/5 "2009-09-02T10:27:49Z")

</div>

Thanks to all.

I got this working in the end - the problem was with my rake  
db:mysql:backup, it specfied the path to the mysqldump command, and I  
don’t know why but when cron ran the command it failed due to this path  
(when the rake task was run directly from the command line it worked  
out). I ran “which mysqldump” and then specified that path explicitly in  
the rake task - and all is good!

All is fine (I think).

But now on to stage two, all I’ve done so far with my single rake task  
run from cron is create the backup file nightly. I now need to manage  
these backups (cull the oldest and make a copy on another machine).

I figure I can use this command for the cull (or something like it)…

/usr/bin/find PATHTORAILSAPP/db/\*.gz -mtime +14 -exec rm {} ;

and a nightly scp command to copy the files off the machine to another  
machine.

Thing is, should I write rake tasks to do this stuff (how easy is that)  
or should I run the commands from cron?

Any and all tips appreciated. I’ve done the first bit, so at least I’ve  
got backups happening nightly (phew)!

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [September 2, 2009, 10:40am UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/6 "2009-09-02T10:40:57Z")

</div>

Am 02.09.2009 um 10:27 schrieb bingo bob:

> I  
> don’t know why but when cron ran the command it failed due to this  
> path

cron on linux doesn’t preserve environment variables, nor does it read  
global ones (/etc/profile.d and others), i.e. if you need some, you  
have to set them up yourself. Further information in ‘man crontab’

> **[crontab(5): tables for driving cron - Linux man page](https://linux.die.net/man/5/crontab)**
>
> A crontab file contains instructions to the cron(8) daemon of the general form: "run this command at this time on this date". Each user has their own ...

Best regards,

Felix

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 31, 2009, 3:22pm UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/7 "2009-08-31T15:22:29Z")

</div>

2009/8/31 bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> –  
> As you snipped everything and the message seems to have become  
> de-threaded no-one except possibly me will know what this message is  
> about.

Colin

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [September 2, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/running-a-rake-task-from-cron-on-a-shared-webhost-joyent/174690/8 "2009-09-02T18:08:12Z")

</div>

Felix SchÃ¤fer wrote:

> Am 02.09.2009 um 10:27 schrieb bingo bob:
> 
> > I  
> > don’t know why but when cron ran the command it failed due to this  
> > path
> 
> cron on linux doesn’t preserve environment variables, nor does it read  
> global ones (/etc/profile.d and others), i.e. if you need some, you  
> have to set them up yourself. Further information in ‘man crontab’  
> [crontab(5): tables for driving cron - Linux man page](http://linux.die.net/man/5/crontab)
> 
> Best regards,
> 
> Felix

Ok well that makes sense then - noted.

Any tips on the backup side of things?
