WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Auto restarting a nodejs server if it crashes  (Read 3228 times)

Offline ohyeah00

  • Newbie
  • *
  • Posts: 2
Auto restarting a nodejs server if it crashes
« on: February 18, 2021, 02:56:19 AM »
I'm trying to enable my nodejs server to run at boot and respawn if it crashes. I'm only familiar with systemd and pm2 for setting up services and I'm getting hung up with busybox init.

I see tinycore provides the command start-stop-daemon. Is it possible to have this create the daemon for my nodejs server automatically so that if it crashes it reboots? I'm trying it just in SSH and while I can get it to start the process in the background, if I pkill node it doesnt reboot and its not making a PID file so trying to run it multiple times starts up multiple processes.
Quote
start-stop-daemon --start --exec ./run.sh --make-pidfile --pidfile /run/server.pid -- --daemon

I've also seen that I could put it in the /etc/inittab file:
Code: [Select]
::respawn:/home/tc/node/run.sh
run.sh
Code: [Select]
node ./server.js
I'd like to create an init.d script to stop/start/restart the process, but I'm unsure how that works if I put the process in the inittab file. If I also then called stop, wouldnt inittab just auto respawn it? How do I get these to all work together?

I also tried installing pm2, but since it needs to be installed globally I'm having an issue with it trying to install into the readonly partition.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Auto restarting a nodejs server if it crashes
« Reply #1 on: February 18, 2021, 09:15:15 AM »
Hi ohyeah00
Welcome to the forum.

Does  node  run in the foreground? If it does, changing  run.sh  to this might work:
Code: [Select]
while true
do
    node ./server.js
done

If  node  crashes and returns control to  run.sh  the loop will restart it.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Auto restarting a nodejs server if it crashes
« Reply #2 on: February 18, 2021, 10:34:06 AM »
I would add few seconds sleep in the loop.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline ohyeah00

  • Newbie
  • *
  • Posts: 2
Re: Auto restarting a nodejs server if it crashes
« Reply #3 on: February 18, 2021, 03:39:10 PM »
Thanks for the suggestion, I will go with that. All my PI does is run this single app in a headless mode connected to the internet so I don't really expect much to happen to it just wanted a fallback incase.

Just out of curiosity, is the /bootlocal.sh the earliest where we can start our own applications? I've been reading through the cookbook but I'm unsure if it would be safe to put it earlier in the boot process. I notice that it takes about 4 seconds after the login prompt for my app to start printing out. When running the script manually it immediately starts printing out so I think its getting delayed for some reason. Could be that the node extension is still loading, but it is in the onboot file so shouldnt that all be ready by the time the login prompt appears? I do care about this starting as quick as possible, currently I'm sitting at 22 seconds until my app comes up. On normal RPI Jessie it comes up in 32 so good, but I'm hoping to do better. I am waiting on some Compute Module 4's to show up with eMMC flash so hopefully that speeds it up quite a bit not being on an SD card.
« Last Edit: February 18, 2021, 03:41:18 PM by ohyeah00 »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Auto restarting a nodejs server if it crashes
« Reply #4 on: February 18, 2021, 10:42:48 PM »
Hi ohyeah00
... Just out of curiosity, is the /bootlocal.sh the earliest where we can start our own applications? ...
The earliest would be in bootsync.sh, before it calls bootlocal.sh. Add it like this:
Code: [Select]
/home/tc/node/run.sh &

Offline danielcharles

  • Newbie
  • *
  • Posts: 1
Re: Auto restarting a nodejs server if it crashes
« Reply #5 on: February 28, 2021, 09:28:20 AM »
Hello, Most of the time, it's because of a 500 Error that have not been catch ed and stop the server, then you will have to restart it. forever is using node by default to start your server. nodemon is a npm package that restart your server when the code changes or when your server stops

Regards
Daniel Charles