I've set up a node.js app on an Ubuntu environment using Vagrant/Puppet (generated from puphpet.com). I'm going to send the completed package to someone, and would like for the node.js app to run the moment that "vagrant up" finishes for them (so that they don't have to bother connecting through ssh and running commands).
The puphpet download package includes a folder /puphpet/files/start-always and /exec-always which I'm wondering if I could use those.
Or if I'm not mistaken, it may be also possible to add some Ubuntu run commands at the end of the .Vagrantfile (just not sure of the syntax).
Anyone have experience with this?
Solution: Figured it out. For anyone that needs to know: create a file in /puphpet/files/exec-once
(or whichever folder/frequency you need) called run.sh
(these files are loaded in alphabetical order, so if you have more than one, name your files appropriately). Example file:
#!/bin/bash
sudo npm install pm2 -g --unsafe-perm 2&>1 >/dev/null
cd /var/www/app && sudo pm2 start app.js
In this case, I'm installing an npm module called "pm2" (2&>1 >/dev/null
will hide console and error messages). This module allows you to run a node.js app as a service. The next line we have to cd
into the app dir, and from there, start the app with sudo pm2
.
Supposedly from this point forward, the node.js app will run/restart automatically (including when app crashes or when Ubuntu restarts), so you don't have to run this command more than once (hence the /exec-once
folder instead of exec-always
, which would run each time "vagrant up" is called, including a future boot after the initial setup).
pm2 also has features allowing you to watch for changes in a folder so that your node.js app will be restart automatically if you're developing. See pm2 documentation for those details or type pm2
in ssh.