I want my server to execute a node script every minute. The program executes perfectly if I execute the file manually (./main.js
), so I'm pretty sure it's not the problem. But when I hand it over to cron to execute, nothing happens.
Here's the line from the cron file.
*/1 * * * * /home/bryce/scripts/wudu/main.js
And here's a sample log:
Oct 11 15:21:01 server CROND[2564]: (root) CMD (/home/bryce/scripts/wudu/main.js)
The executable: home/bryce/scripts/wudu/main.js
#!/usr/bin/env node
var program = require('commander');
var v = require('./cli/validation');
var a = require('./cli/actions');
program
.version('0.0.1')
.option('-u, --url', 'Register url')
.option('-s, --selector', 'Register selector')
.option('-p, --pagination', 'Register pagination')
.option('-i, --index', 'Pass an index, to destroy')
.parse(process.argv);
var args = process.argv.slice(2),
mode = v.mode(args[0]),
options = v.hasArgs(mode, program);
a.init(mode, options);
Any idea why I'm getting radio silence? Somewhere else I should be looking to debug?
UPDATE:
I believe the problem has to do with my relative filepaths, and main.js being executed from outside its own directory.
So now, I've placed exe.sh
in the wudu
directory. It looks like this:
#!/bin/bash
cd ${0%/*}
./main.js mail
exit
Now, I've set cron to execute this file every minute. I tried executing this file from other folders, and it works as expected. But again, cron isn't picking it up.
I had the exact same problem, so I ended creating a passthrough script that would load my environment variables, then execute node like so:
##!/bin/bash
# Source bash profile to load env variables
# or any other that you want to set explicitly
. ~/.bash_profile
# tunnel that allows us to execute cron jobs
node $1 $2
To use just add it to your crontab
* * * * * <user> <passthrough_script> <arg1> <arg2>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With