Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Run `node` scripts using `nvm` and `crontab` without hardcoding the node version
- # cronjob.env.sh
- ```bash
- #!/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
- ```bash
- # 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
- ```bash
- #!/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
- ```js
- console.log(process.version)
- ```
Add Comment
Please, Sign In to add comment