3

I was looking to test a node server in a shared hosting environment.

I'm using an ssh terminal session to test.

The server works fine, but of course you can't leave the terminal session without stopping the server.

Using pm2 (npm package), I'm able to keep the server running, but on exiting the terminal session, the pm2 job quits as well, which stops the server. Curious why that would be.

Tried using a crontab to run a shell script that in turn runs the pm2 which in turn starts the node server. The cron tab runs every minute, but the node server never starts.

The sh script works just fine, pm2 works fine, and the node server works fine. What doesn't work: keeping the node server running after exiting the terminal session.

Here's the shell script that runs the pm2 to trigger the node server...

ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
  echo "Process running."
else
  echo "Process not running."
  PATH=$PATH:/usr/local/bin
  pm2 start '/path/to/NodeServer.js' --restart-delay=100
fi
WhatsYourFunction
  • 621
  • 1
  • 9
  • 25
  • Have you tried the `pm2 startup` command? pm2 tries to detect your init system and tell you how to configure it to start on boot. Then just do `pm2 start ...` followed by `pm save`. Docs: https://pm2.keymetrics.io/docs/usage/startup/ – rudolfbyker Aug 20 '20 at 17:15
  • Thanks rudolfbyke -- `pm2 start` requires root access to the server, which you don't get with shared hosting. In fact, although pm2 was installed, it seems to no longer function at all, nor does npm. I'm assuming the hosting provider -- forever up-selling VPS service -- uninstalled it, so I don't know that anything node -- on this shared hosting with this particular provider -- is going to work. – WhatsYourFunction Aug 23 '20 at 12:18
  • Only the `pm2 startup` command needs root access, but you might be able to compensate for that using cron, or a vendor-specific startup script, if your provider has such a thing. I'm using [npm-g_nosudo](https://github.com/glenpike/npm-g_nosudo), to install "global" npm packages inside a very limited jailkit environment on my shared hosting, and periodically making sure that `pm2` is still running using a cron script defined through my provider's web interface (ISPConfig). – rudolfbyker Aug 25 '20 at 07:16

2 Answers2

1

If someone else like me uses pm2 for godaddy shared hosting, and uses a modified version of Habib's answer with pm2 instead of node, do not forget to include path to node into PATH variable, as it is required by pm2:

PATH=$PATH:/home/user/.nvm/versions/node/v11.15.0/bin
pm2 ps | grep 'my-app'
if [ $? -eq 0 ]; then
  echo "Process running."
else
  echo "Process not running."
  cd /home/user/public_html/app
  pm2 start app.js --name my-app
fi

The script above works for me. Specifying only full path to pm2 was not working, as it looks for node, which was not in PATH in the cron environment.

Mikhail Tulubaev
  • 4,141
  • 19
  • 31
0
ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
  echo "Process running."
else
  echo "Process not running."
  /home/user/.nvm/versions/node/v11.15.0/bin/node /home/user/public_html/node/app.js
fi

I used this in cron job in GoDaddy Shared hosting and it is working for me. Before this, I used node /home/user/public_html/node/app.js in the same script but it throws an error because of path of the node. Still, I can use node in the terminal but in cron job, I have to use full path.

Habib Siddiqui
  • 174
  • 1
  • 5
  • Thanks Habib, For me the cron approach didn't work if only because the hosting provider, forever trying to up-sell from Shared to VPS hosting, disallowed the cron job being used every minute. (They don't make this easy. Furthermore their up-sell worked, -- I purchased the VPS -- but I'd still love to be able to run a node server or two on the shared hosting service) I tried an approach using tmux, but that, too, isn't included in the shared hosting. So far my attempts to install tmux without root access have failed (Grrr). Thanks again Habib. – WhatsYourFunction Sep 02 '20 at 20:58
  • I am using it in GoDaddy Shared Hosting and it is working fine. – Habib Siddiqui Sep 04 '20 at 13:05
  • No doubt it depends on the limitations imposed by the hosting provider. – WhatsYourFunction Sep 07 '20 at 02:46