How to run a script in nodeserver every hour? - node.js

I have a mean.js project, with this structure in the app (where the server files are):
-app
--controllers
--models
--docs
--logs
--routes
--tests
--views
bower.json
Gruntfile.js
package.json
README.md
I want to do a "cron job" every hour or so on my mongodb.
I ran into this:
https://github.com/scripting/noderunner
Where should i place it and how do i use it let's say for the sake of example,
to do console.log of "hello world" every 1 hour?

first you must install cron to your os, after this you must set cron job, for do this you must write "crontab -e" in console and set job, somthing like this:
0 * * * * /home/user/project/bin/your_script.js
Important! You must add hashbang(#!/usr/bin/env node
) to first line in you script and set permissions. Good luck.

Related

cronjob to run shell script and execute npm command on raspberry pi (as opposed to calling node directly)

I am trying to build a shell script which can be called via a cronjob to trigger an npm nodejs application.
This is my start.sh shell script
#!/bin/bash
#!/usr/local/bin/npm
cd /home/lharby/sites/mysite
npm run start
If I cd to this folder and execute ./start.sh the command appears to run. (Path to bash and npm are both correct after checking which npm).
My cron job looks like this:
*/5 * * * * /home/lharby/sites/mysite/start.sh >> /home/lharby/sites/mysite/src/log/cron-errors.txt 2>&1
This is throwing an error and additionally I was lead to believe that using >> would append to the file, it seems to overwrite it each time.
My guess is that trying to run this command via cron it cannot access certain environment variables that are set up in my index.js
For example:
const config = {
access_token: process.env.NEXT_MASTODON_ACCESS_TOKEN,
client_key: process.env.NEXT_MASTODON_CLIENT_KEY,
client_secret: process.env.NEXT_MASTODON_CLIENT_SECRET,
timeout_ms: 60 * 1000,
api_url: 'https://botsin.space/api/v1/',
};
const M = new Mastodon(config);
I believe I see the same issue when I try to run node index.js from /home/sites/lharby/mysite/src/
As my package.json has this configuration:
"scripts": {
"start": "node ./src/index.js --experimental-modules",
"temp": "node ./src/temp.js --experimental-modules"
},
I was exploring looking at just trying to run the whole app passing in node index.js and passing in the argument flags but I need to be able to run the cron file invoking npm rather than node, as I guess that creates a wrapper and npm can access process.env variables.
From my cron-errors.txt file I am seeing this:
/home/lharby/sites/mysite/node_modules/mastodon-api/lib/mastodon.js:345
throw new Error('Mastodon config must include \'' + reqKey + '\' when using \'user_auth\'');
^
Error: Mastodon config must include 'access_token' when using 'user_auth'
at /home/lharby/sites/glyphbot/node_modules/mastodon-api/lib/mastodon.js:345:27
//
//
/home/lharby/sites/mysite/start.sh: line 4: npm: command not found
/bin/sh: 1: /home/sites/glyphbot/start.sh: not found
/bin/sh: 1: /home/sites/glyphbot/start.sh: not found
How can I ensure the crontab will invoke npm? I feel like I am doing everything correctly.
EDIT
My issues are:
How to run a node project using npm from a cron job?
Can the cronjob access environment variables from the npm command?
Should >> cron-errors.txt 2>&1 append the file rather than replace the content each time.
UPDATE 19.01.23
So I added exports for my variables to the .bashrc file. And when checking echo $NEXT_MASTODON_ACCESS_TOKEN I am seeing my string value.
Updated my .sh file so it now looks like this:
#!/bin/bash
node ./src/index.js --experimental-modules
And updated my cronjob to read this:
*/5 * * * * /home/lharby/sites/mysite/start.sh >> /home/lharby/sites/mysite/src/log/cron-errors.txt 2>&1
And I tried this also trying to bypass the shell script
*/5 * * * * /home/lharby/sites/mysite/src && /usr/local/bin/node index.js --experimental-modules >>/home/lharby/sites/mysite/src/log/cron-errors.txt 2>&1
It still failed with the same message being logged to the cron-errors.txt file.
However I am now able to run this command invoking node with argument flags (rather than using the npm command) So in the terminal I can type
node index.js --experimental-modules
As well as just running the .sh file I just don't understand why it is not passing this information to my cronjob.
I don't understand how if my code reads:
process.env.NEXT_MASTODON_ACCESS_TOKEN,
Will this get replaced or read by the bash export value instead?
Where are your environment variables being set up? It looks like your cron is at least running as expected, and your only problem is to get those env variables into the script correctly. My guess is that you need to use "export" (see bullet point 3)
I would debug this in a few different ways.
you should verify that it is the case that your index.js is not able to read your environment variables. I would recommend adding console.log(JSON.stringify(config, undefined, 2)) to check this.
you should verify that the SHELL has access to those variables before it runs the script. for this just run echo $NEXT_MASTODON_ACCESS_TOKEN (and similar for each variable) to verify if that is the case.
environment variables are a little funny. Assuming you set these values in your .bashrc with NEXT_MASTODON_ACCESS_TOKEN="whateverTheValueIs", just setting variables like this only affects your current process. In order for sub-processes to have the variable you need to export: export NEXT_MASTODON_ACCESS_TOKEN="insertValueHere"
Hope this helps!
Edit:
It's a little unclear what your current issues are, so it might be helpful if you bullet point each issue you are trying to solve

Add a job to linux cron list programmatically with Nodejs

I have a job with Nodejs that I want to do it each 30 minutes to scan the Database and Update Products Data in An Ecommerce API with my Nodejs Program, note that the Nodejs Program is serving an REST API (Backend) for a react js web application
So I searched for that and I found that I can do that with Nodejs Cron Library like "node-schedule" but I know that will be more interesting to do it with Linux Cron
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
Is there any library that can let me add Cron jobs to Linux using Nodejs Or would I do it with "fs" only? so I will open the cron job file and add my command?
The command crontab which is part of Vixie Cron allows you to create, edit and delete per-user cron entries.
Or if you are running as the root user, which you should not be doing, you can drop cron files into /etc/cron.d
This is not always supported, and if you're running in a Docker type container environment it is doubtful that you have any cron at all. In that environment you'd want your running Nodejs to handle scheduled jobs for you. Or use some other kind of distributed scheduled work system.
You can put your cron job to a nodejs script. Then adding to the crontab can be done with cronbee module, via API:
import { cronbee } from 'cronbee'
await cronbee.ensure({
taskName: 'do smth',
taskRun: `node my-script`,
cron: '42 * * * *'
})
or you can ensure the cron job via CLI, if the module is installed globally or from npm scripts:
$ cronbee ensure mytasks.json

run command at interval on debian

So i have a debian web mapping server for my minecraft world. In order for the map to display the correct information two commands have to be run periodically. I have tried following a few guides to use crontab but so far have failed (and even had to restore the debian image -.-) I am new to linux as a whole and need a step by step guide in plain english to do the following.
run:
"overviewer.py --config /home/mc/test.cfg"
every 30 minutes on the hour and
"overviewer.py --config /home/mc/test.cfg --genpoi"
every five minutes on the hour
It seems pretty straight forward but I have literally spent the better part of two months doing this cause I keep screwing things up.
Thanks for any help!
Remember if you are using crontab, to use the full path to the python script. In debian you can type pwd in the terminal to show the path to your current location.
Assuming the python script is also located in /home/mc/ you should use the command:
/home/mc/overviewer.py --config /home/mc/test.cfg
I would suggest you look into crontab again, the ubuntu help page has alot of information. https://help.ubuntu.com/community/CronHowto
For every 30 minutes:
0,30 * * * * /home/mc/overviewer.py --config /home/mc/test.cfg
And for every 5 minutes:
*/5 * * * * /home/mc/overviewer.py --config /home/mc/test.cfg --genpoi

How to run a nodejs script every second

I need to run my nodejs script for every second ,Similar to PHP cron jobs. I have tried some nodejs cron libraries like https://github.com/ncb000gt/node-cron but the issue was first run should be manual i:e I have to run the file with cron script for first time manually.
But in php cron jobs, they run by the server so if the apache server running script will automatically start and even if the script return an error for a cycle then script will run again from the beginning from the next cycle
So is there any way to achieve this in nodejs ?
You have two options:
using Node as a daemon, with something like Supervisord to run your node-cron script. This alternative is wasteful on resources such as RAM because Node and Supervisord are running all the time.
using the system's crontab, you can run your script like calling Node on the command line, such as * * * * node /path/to/your/script.js. This alternative is highly efficient but lacks some control, like being able to log the output in case of an error, although you could just pipe the output to a file: node script.js > logfile

Trouble running cron on Joyent

I'm trying to set up a node script to run as a cron job on Joyent. I can run arbitrary commands but node scripts to seem to execute. As an example:
# cron
# call a script every minute
# being specific about the location of node and the script to run
* * * * * /home/node/local/nodejs/bin/node /full/path/to/some-script.js
// node script at /full/path/to/some-script.js
var fs = require('fs');
fs.writeFile('/home/node/node-service/some-script.log', new Date.toString(), 'utf8');
What I expect to see after one minute is a file at /home/node/node-service/some-script.log with content like Mon Jan 21 2013 15:19:11 GMT-0600 but I see nothing. This is still the case even if the script is set to full read, write and execute permissions for all users and whether the crontab is set for the root or node users.
What am I missing?
Thanks
The fourth optional argument to writeFile is a callback to fire when the file system is done writing the file. You can use it to determine the error that is happening, as it's only argument is an error. Refer to the docs here.
It appears to be working now. I'm not sure what I changed that got it working. It may have been a permissions issue.

Resources