Skip to content

Instantly share code, notes, and snippets.

@simov
Last active March 28, 2024 03:26
Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save simov/cdbebe2d65644279db1323042fcf7624 to your computer and use it in GitHub Desktop.
Save simov/cdbebe2d65644279db1323042fcf7624 to your computer and use it in GitHub Desktop.
Run `node` scripts using `nvm` and `crontab` without hardcoding the node version

Run node scripts using nvm and crontab without hardcoding the node version

cronjob.env.sh

#!/bin/bash

# NVM needs the ability to modify your current shell session's env vars,
# which is why it's a sourced function

# found in the current user's .bashrc - update [user] below with your user! 
export NVM_DIR="/home/[user]/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

# uncomment the line below if you need a specific version of node
# other than the one specified as `default` alias in NVM (optional)
# nvm use 4 1> /dev/null

crontab -e

# paths can be relative to the current user that owns the crontab configuration

# NVM should be sourced here!
# otherwise `$(which node)` in `script.sh` won't work!
*/1 * * * *     (. ~/path/cronjob.env.sh; ~/path/script.sh >> ~/path/file.log; )

# alternatively the node version can be specified here
*/1 * * * *     (. ~/path/cronjob.env.sh; nvm use 4 1> /dev/null; ~/path/script.sh >> ~/path/file.log; )

script.sh

#!/bin/bash

# paths can be relative to the current user that owns the crontab configuration

# $(which node) returns the path to the current node version
# either the one specified as `default` alias in NVM or a specific version set above
# executing `nvm use 4 1> /dev/null` here won't work!
$(which node) ~/path/script.js

script.js

console.log(process.version)
#!/bin/bash
# NVM needs the ability to modify your current shell session's env vars,
# which is why it's a sourced function
# found in the current user's .bashrc - update [user] below with your user!
export NVM_DIR="/home/[user]/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
# uncomment the line below if you need a specific version of node
# other than the one specified as `default` alias in NVM (optional)
# nvm use 4 1> /dev/null
console.log(process.version)
#!/bin/bash
# paths can be relative to the current user that owns the crontab configuration
# $(which node) returns the path to the current node version
# either the one specified as `default` alias in NVM or a specific version set above
# executing `nvm use 4 1> /dev/null` here won't work!
$(which node) ~/path/script.js
@vjsingh
Copy link

vjsingh commented Mar 1, 2019

This is amazing!! Works perfectly. Thank you

@sayore
Copy link

sayore commented Jul 26, 2019

Doesnt this */1 * * * * mean it executes every Second?

@mikeseese
Copy link

Thanks for this tip! You could change export NVM_DIR="/home/[user]/.nvm" to export NVM_DIR="$HOME/.nvm" (which is the default anyway from nvm)

@eljenso
Copy link

eljenso commented Feb 21, 2020

Thank you for this, really helped me. 🙂
Because I wanted to run an npm script in a project, I adjusted script.sh slightly:

# Go to project
cd ~/path/to/project;
# Run a script, as defined in package.json (note that we are using `which npm` here)
$(which npm) run start;

@eljenso
Copy link

eljenso commented May 6, 2020

You've probably meant to say cd ~/path/to/project; to change directory, right?

@nextlevelshit Yes, thank you for pointing that out. Edited my comment.

@SaragocaG
Copy link

Doesnt this */1 * * * * mean it executes every Second?

no, it means every Minute.

@wjx0912
Copy link

wjx0912 commented May 24, 2021

good job!

@Nardonist
Copy link

Thank you so much!

@AndreasGrip
Copy link

Thanks, this solved a big problem I had with ancient node version on a server i use.

Also, to contribute, you don't actually need the .sh file, you can run commands directly from crontab.
At least as long as your program look for all files/store all files relative to where the program is started.
(path.dirname(process.argv[1])

In my cron job it simply looks like this.

18 3 * * * (. /home/users/cronjob.env.sh; $(which node) /home/user/reportgenerator/reportgenerator_v3.0.js myreport.yaml;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment