I
I
i1yas2019-06-28 21:07:10
Node.js
i1yas, 2019-06-28 21:07:10

How to execute nodejs scripts in crontab?

I tried to force the script to be executed on node with a certain periodicity, but there were problems.
The script should write to the logs. Logs are empty. As a result, I created a test script that looks like this: The task is written in crontab like this
console.log(Number(new Date()));

* * * * * /usr/bin/node /path/to/script/test.js >> /path/to/script/test.log

This command works manually. But cron doesn't execute the script for some reason. If the file does not exist, it is created.
Initially, I wrote to the file not through stdout, but using the fs module, but the picture was the same.
In /var/log/syslog there is a record of the task execution.
Cron is running, of course.
Ubuntu 18.04.2
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
i1yas, 2019-06-29
@i1yas

In general, I did not immediately think of logging stderr, but there
And the nature of this error is not very clear to me, I kind of directly indicate the path to the node.
In general, the solution is a crutch, I found it here : create a file execute.sh. Use env in it:

#!/usr/bin/env sh
node /path/to/file/test.js >> /path/to/file/test.log

Then added to cron
and it worked

V
Vitaly, 2019-06-28
@vshvydky

I think the problem is only in the fact that the cron documentation was not read, what * * * * * , where is at least 1 number, so that cron understands that you need to run the script then?
stackoverflow progressives evil, read the docks buddy!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question