jakejs / jake

JavaScript build tool, similar to Make or Rake. Built to work with Node.js.
http://jakejs.com
Apache License 2.0
1.97k stars 189 forks source link

Running a jake task as a Cronjob #243

Open cometbalan opened 10 years ago

cometbalan commented 10 years ago

Consider the below jake task

namespace('test', function () {
  desc('Task to generate General invite code');
  task('cron', function (general_ic) {
    console.log("DATA SUCCESS");    
  });
});

I can able to execute this task when i run this like (with absolute path for jake and the jake file)

/home/balan/local/bin/jake -f /home/path/to_project/jake-file.js test:cron > /home/balan/out.txt

But when i add the below code in a crontab, this is not working. I have checked the cron log, this task is actually initiated, but there is no output. (every minute once)

* * * * * /home/balan/local/bin/jake -f /home/path/to_project/jake-file.js test:cron > /home/balan/out.txt
mde commented 10 years ago

What directory are you currently in when you run the task manually?

mde commented 10 years ago

What directory should the task run in? Is it possibly a permissions issue?

cometbalan commented 10 years ago

I am running the jake task directly under my project directory. Below is the structure

my_project/app
my_project/jake/test-jakefile.js

The task runs even when i go to home directory (cd ~) and give absolute path for both jake and the jakefile.

What i found was, the PATH variable loaded during a cron seems not to have all the actual PATH information. I created a shell file (.sh) and added the PATH info as like below

PATH="/usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/balan/local/bin:/home/balan/local/bin:/home/balan/local/bin:/home/balan/local/bin"

///jake invoke here

and add this .sh file to the cronjob, and it worked.

But i am wondering if there is a clean way to do this?

mde commented 10 years ago

If I understand you correctly, the problem is that the Jake executable isn't in the PATH of the cron user. You can actually run Jake via the cli.js script -- the jake executable is really just a symlink to that path. If you've installed it globally, it's probably at /usr/local/lib/node_modules/jake/bin/cli.js.