Trouble running cron on Joyent - node.js

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.

Related

Spring Boot logs missing using crontab command

I run my Spring Boot/Batch Jar to process large number of files in my local environment like below:
java -jar /path/to_my_dir_for_jar/springbatch.jar --spring.profiles.active=qa
And logs get generated in the same directory: /path/to_my_dir_for_jar
No issues whatsoever till here.
Now I ran the same job using linux crontab using (11:30 am, daily) :
30 11 * * * java -jar /path/to_my_dir_for_jar/springbatch.jar --spring.profiles.active=qa
Please note: I have to rely on linux crontab to schedule and not spring cron-expression as Spring Batch wont read the new files in the staging directory after first run.
Now I can NOT find my logs at - /path/to_my_dir_for_jar. In the application there different types of logs are generated such as:
service_log.gnxError.log
service_log.gcnDone.log
service_log.error.log
service_log.done.log
service_log.log
I tried to follow this and some other similar help but none of them seem to be working as most of them say - No such file or directory
Where to look for them?
One possible way is to redirect the output of my crontab expression to a file like below:
30 11 * * * java -jar /path/to_my_dir_for_jar/springbatch.jar --spring.profiles.active=qa >> foobar.log
But this way I will loose my different type of logs as everything will be fused in just one file in this case.
Will I get the logs the way I get while running the jar manually ever using linux crontab? What are my options in this situation?

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

Execute Node.JS script via crontab on OS X

I have been trying to figure out how to use crontabs on my mac OS X I have a node script written that I would like to be able to execute in a scheduled manner. Here is the line I am using in my crontab file.
30 * * * * /usr/local/bin/node /Users/pmanca/Google Drive/JavaScript/code/Peter/marketing-tests/testBackup.js
It doesn't appear to be executing though. Can anyone see what I am doing wrong? Also is there another way on a mac or linux server to accomplish what I'm looking for? Also can you use the same line for a crontab job in linux as well(besides the file paths needing to change)?
You can use one of the two npm packages, cron or agenda
Both work on linux and OS X. Cron is best if your tasks are lightweight and you don't need your jobs to be persisted into db. Agenda uses mongodb for persistence.
You can configure a job in cron as:
var CronJob = require('cron').CronJob;
var job = new CronJob({
cronTime: '00 */30 * * * *',
onTick: function() {
/*
* Runs every 30 minutes, every hour, every day, all week
*/
},
start: false,
timeZone: 'America/Los_Angeles'
});
job.start();
To run in background on server you can run it with forever
Remember cron has an additional 6th place to the left for seconds. Otherwise the syntax is same.
By default cron process doesn't have the required permission to access the disk in macos. So, you should allow full access disk to cron process. For this one, add /usr/sbin/cron to your allowed full disk access process under Security & Privacy. Further information can be seen in How to Fix Cron Permission Issues in MacOS Catalina & Mojave

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

Cronjob doesn't execute (Cygwin)

I'm using Cygwin to synchronize folders between a Windows-Machine and an Apache server. It works fine if I execute the commands, but I can't get Cronjob to work properly.
I tried to create a simple mkdir command as a cronjob and when I save the crontab, it successfully installs it.
My Cronjob is:
*/1 * * * * mkdir newfolder
I wait for the new folder to pup up, but nothing happens.
In the cron.log it says:
Cron 7072 fork: child -1 - CREATEPROCESSW failed for c:cygwin/usr/sbin/cron.exe, errno 30
Can anybody help?
I solved the problem: (drumroll)
I reconfigurated it... often!
I kept trying "cron-config" time after time and now it works. I'm still not any smarter what I configurated wrong/right.
I'm sorry for the vague help if anybody with the same problem stumbles upon this.

Resources