4

I know that you can run a Node.js script in Crontab by doing something like:

0 * * * * node /path/to/your/script.js

But I want to run a Node.js app, not a script, using Crontab. I created a Node.js app in order to write some automated tests using Mocha, Chai and Selenium, and I want to run it periodically by using Crontab. How would I go about doing this? I currently run my app by writing in the command line:

npm run api-pro

Where api-pro is a script from my package.json that invokes some tests for the production api.

Note that if I simply try to write a Crontab job with the command "npm run api-pro" it doesn't recognize the command npm (and obviously I do have Node installed in my computer).

3 Answers 3

10

My guess is that the user cron use do not configure the PATH in the same way as your user, and do not know node nor npm.

What you can try is to use the command which node to know where your node binary is (/some/path/to/node)

Then you can use the absolute path in your crontab:

0 * * * * /some/path/to/node /path/to/your/script.js

EDIT:

The difference between adding node and npm to $PATH and using absolute paths is that absolute path will work for one executable, since Linux will not have to search the PATH. Adding to the PATH will make Linux recognize node and npm just as in your user. The fact that they are in the same folder do not affect that.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so if I do this for node, it now recognizes the node command, but it does not recognize the npm command. I don't see how this is possible since my npm and node are both in the same path /usr/local/bin. So if I do /usr/local/bin/node -v in crontab it works fine and gives me the version, but if I do /usr/local/bin/npm -v I get the error "env: node: no such file or directory". Any thoughts?
I was able to fix it by adding my $PATH value to Cron's PATH. Not really sure how this is any different from specifying the path of npm in the Cron's command (/usr/local/bin/npm), but it works.
6

For anyone trying to run with npm, here were my steps to get it working on MacOS 12.3.1, using node/npm installed by brew.

Note, this is inflexible, and the PATH will need to be updated if brew updates its paths again:

  1. Print two things we will need
echo $PATH
// /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:...

which npm
// /opt/homebrew/bin/npm
  1. Open crontab with EDITOR="vim" crontab -e
  2. Add these two lines
PATH={what you copied from step 1}
* * * * * cd {your_project_dir_where_package.json_lives} && {location_of_npm_from_step_1} run start >>/tmp/crontab.log 2>&1

Note: The >>/tmp/crontab.log 2>&1 is to help you debug. You can tail -f /tmp/crontab.log in another terminal to watch for STDOUT or STDERR

1 Comment

THANK YOU!!! This was the only thing that worked for me
0

I guess that by using crontab, you're running your node app on a Linux machine so why don't you write a simple bash script ?

run_test.bash

#!/bin/bash
cd /path/to/your/app && \
npm run api-pro

then your crontab should look like :

0 * * * * /path/to/your/bash/script/run_test.bash

Of course, your script will have to be executable for your user :

$ chmod u+x run_test.bash

8 Comments

Is the \ after the && in the script a typo or why is there?
This doesn't resolve the initial issue, this is just another way of writing what I had already wrote and it gives me the error "/bin/sh npm: command not found". I'm starting to think that it doesn't recognize npm because it's trying to run it at /bin/sh. Do you have any idea as to how to change this so that it runs somewhere in my computer where it can recognize npm? Npm is installed in my computer and I can use it perfectly in the Terminal.
the `&& ` is not a typo, it's just to check the path exists before calling the script.
If I run the commands directly in Crontab (without a bash script) I get "/bin/sh npm: command not found" and using the bash script I get "path/to/script: line 3: npm: command not found"
It runs correctly on my computer, I've edited a simple crontab with my user to run npm -v each minute and it's ok, I don't have the command not found error. Could it be possible the crontab was edited with a user that doesn't have the right to run npm ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.