09. Run on Startup

Our ngrok server will automatically reconnect, but what about our node server? For that we'll have to set that up on our own. We'll use a package called pm2 (Process Management)

  1. Install pm2 on the Pi: sudo npm install -g pm2@latest

  2. Now we can start our server with: pm2 server.js

  3. The server will be running in the background, but we can monitor the app with: pm2 monit

  4. Exit the monitoring screen with: ctrl + c

  5. Set pm2 to run at startup: pm2 startup

  6. Type the command it tells you after running the previous command

  7. Reboot: reboot

  8. Check that shSMS is running: pm2 status

You should see the shSMS process in the list

Let's do the same for ngrok:

  1. Edit you ngrok config file: nano ~/.ngrok2/ngrok.yml

  2. Change it to this with exact spacing (4 spaces per indent)

authtoken: <your-auth-token>
tunnels:
    default:
        proto: http
        addr: 4001
        hostname: <your-subdomain>.ngrok.io
  1. Navigate to your system folder: cd /etc/systemd/system

  2. Create service file: sudo touch ngrok.service

  3. Add the following and change it to match your own information

[Unit]
Description=Ngrok
After=network.service

[Service]
type=simple
User=<your_pi_username>
WorkingDirectory=/home/<your_pi_username>
ExecStart=/usr/bin/ngrok start --all --config=/home/<your_pi_username>/.ngrok2/ngrok.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target

4. Enable and start the service we just created: systemctl enable ngrok.service && systemctl start ngrok.service

5. Reboot: reboot

6. Send an SMS to confirm it worked

One note about setting up ngrok as a service: For whatever reason I'm not sure how to view the ngrok screen that shows you the status/url/activity/etc once it's set up to run as a service, so if not being able to see all that is an issue, you can stop ngrok from running at startup with sudo systemctl disable ngrok.service

And then run the regular ngrok http --hostname=XXX 4001

Last updated