question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for system crash/restart

See original GitHub issue

I’ve been experimenting with Forever for some time and it is a great module. However I have a Debian server that sometimes hits memory limit and force restarts.

As the name implies, I’d have expected Forever to survive through system crash/restarts. I see that the documentation doesn’t state anything about system restarts, I’ve also done some testing on Windows too and there’s apparently no option to automatically restart it on system boot.

Did I overlook something, or do I have to actually add it to the system startup manually as an workaround? In that case, what would be a recommended approach e.g. adding the forever start script.js line to a network script at /etc/network/if-up.d/? And possibly a stop command before that to don’t unnecessarily spawn multiple instances of the same script?

I’d like this live-through-restart/system crash to be a proper option in Forever, but if it can’t be, should I use the approach above or is there any better recommendations? As I’m using a VPS, a solution that doesn’t require root would be great.

Thanks in advanced.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
UltCombocommented, Jan 11, 2013

Yes, at least this VPS allows for Cron jobs. Here’s my slightly hackish solution, hopefully it may be useful to other node developers that may run into a similar issue:

#! /bin/bash
export PATH="READ BELOW"
a=`forever list`

if [ `expr length "$a"` -eq 47 ] ; then
  echo "Restarting node server through troll script"
  forever start -l forever.log -o out.log -e err.log -a /full/path/to/script.js
fi

(pardon me if the code style sucks, I’ve never had linux shell experience previously)

Execute a echo "$PATH" in the console and paste the returned string between the quotes in export PATH="" otherwise the script won’t work when running as a cron job.

Of course, this .sh isn’t very sturdy due to relying on the string length of the message returned by forever list. Any minor change to that message will break this script and it will have to be updated. Maybe a condition checking for a substring may be more sturdy:

if [[ "$a" == *"No forever processes running"* ]] ; then

Still, it relies too much on a generic message. If there was only a way to obtain the number of running Forever instances it would be really useful for this use-case.

Nevertheless, after having saved the .sh above, create a Cron job for it crontab -e:

MAILTO="your@email.com"
*/15 * * * * /full/path/to/script_above.sh

Where 15 is the minutes interval between each check. I guess that’s it. Setting an e-mail is useful so you’d receive the echoed message(s) through e-mail whenever the script detects that no Forever instances are running, but e-mail could be set to an empty string to disable that feature as well.

Though, I’d like to know if it is possible to retrieve the number of running Forever instances in an easy way so I could eq it with 0 in the conditional? I guess simply returning the number of running instances wouldn’t be a hard feature to implement if it isn’t implemented yet. :)

0reactions
UltCombocommented, Jan 11, 2013

Thanks again. =] Closing this as my issue has been resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

System crashes after you restart or wake up the computer in ...
Method 1: Windows Update for Windows 8.1 · This update is provided as an important update. · If you select the Install updates...
Read more >
System crash or unexpected reboot | Support - SUSE
The system encountered a crash or rebooted unexpectedly. In order to identify the root cause for this issue, a request with Customer Care...
Read more >
How To Configure a Linux Service to Start Automatically After ...
This tutorial shows you how to configure system services to automatically restart after a crash or a server reboot.
Read more >
Fix an Android device that's restarting or crashing
Troubleshoot your phone · Open your phone's Settings app. · Near the bottom, tap System and then System update. If needed, first tap...
Read more >
How can I automatically restart a Windows service if it crashes?
Open Services.msc, double-click on the service to open the Properties of the service, there is a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found