Guest User

Untitled

a guest
Mar 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. # Run `node` scripts using `nvm` and `crontab` without hardcoding the node version
  2.  
  3. # cronjob.env.sh
  4.  
  5. ```bash
  6. #!/bin/bash
  7.  
  8. # NVM needs the ability to modify your current shell session's env vars,
  9. # which is why it's a sourced function
  10.  
  11. # found in the current user's .bashrc - update [user] below with your user!
  12. export NVM_DIR="/home/[user]/.nvm"
  13. [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
  14.  
  15. # uncomment the line below if you need a specific version of node
  16. # other than the one specified as `default` alias in NVM (optional)
  17. # nvm use 4 1> /dev/null
  18. ```
  19.  
  20. # crontab -e
  21.  
  22. ```bash
  23. # paths can be relative to the current user that owns the crontab configuration
  24.  
  25. # NVM should be sourced here!
  26. # otherwise `$(which node)` in `script.sh` won't work!
  27. */1 * * * * (. ~/path/cronjob.env.sh; ~/path/script.sh >> ~/path/file.log; )
  28.  
  29. # alternatively the node version can be specified here
  30. */1 * * * * (. ~/path/cronjob.env.sh; nvm use 4 1> /dev/null; ~/path/script.sh >> ~/path/file.log; )
  31. ```
  32.  
  33. # script.sh
  34.  
  35. ```bash
  36. #!/bin/bash
  37.  
  38. # paths can be relative to the current user that owns the crontab configuration
  39.  
  40. # $(which node) returns the path to the current node version
  41. # either the one specified as `default` alias in NVM or a specific version set above
  42. # executing `nvm use 4 1> /dev/null` here won't work!
  43. $(which node) ~/path/script.js
  44. ```
  45.  
  46. # script.js
  47.  
  48. ```js
  49. console.log(process.version)
  50. ```
Add Comment
Please, Sign In to add comment