hotwalk
Posts: 2
Joined: Thu Nov 30, 2017 3:29 pm

restart script when failing

Sat Feb 03, 2018 6:27 pm

Hi there!

I have written a Javascript file, which sometimes crashes for unknown reasons... i would like to restart this script, whenever it has stopped working. how can i achive that? :?
another thing is, if i run this script in the terminal, it works great, but if i doubleclick the file, it immediatly closes... any ideas why that happens?

my script is:

Code: Select all

var blynkLib = require('blynk-library');
var sensorLib = require('node-dht-sensor');

var AUTH = 'xxxxxxxxxxxxx';

// Setup Blynk
var blynk = new blynkLib.Blynk(AUTH);

// Setup sensor, exit if failed
var sensorType = 22; // 11 for DHT11, 22 for DHT22 and AM2302
var sensorPin  = 2;  // The GPIO pin number for sensor signal
if (!sensorLib.initialize(sensorType, sensorPin)) {
    console.warn('Failed to initialize sensor');
    process.exit(1);
}

// Automatically update sensor value every 2 seconds
setInterval(function() {
    var readout = sensorLib.read();
    blynk.virtualWrite(3, readout.temperature.toFixed(1));
    blynk.virtualWrite(4, readout.humidity.toFixed(1));
    
    console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
    console.log('Humidity:   ', readout.humidity.toFixed(1)    + '%');
}, 2000);

Heater
Posts: 9722
Joined: Tue Jul 17, 2012 3:02 pm

Re: restart script when failing

Sat Feb 03, 2018 9:26 pm

hotwalk,

Firstly I would suggest you find out why it crashes sometimes. Before you start apply band aid fixes like restarting it. Make it not crash first.

I have no idea about double clicking it. I presume you are using node.js. You should be running it with the node command from the command line.

I might guess that when you click on it then a terminal window opens, the script is run, it crashes immediately and the terminal window closes.

hotwalk
Posts: 2
Joined: Thu Nov 30, 2017 3:29 pm

Re: restart script when failing

Sun Feb 04, 2018 10:44 am

Heater wrote: I might guess that when you click on it then a terminal window opens, the script is run, it crashes immediately and the terminal window closes.
i dont think that is the case, since, this script usually runs for more than 2-3days and then crashes (if i run it from the comandline)... so why would it crash immediatly if i double-click it?
yet i havent found any other solution how i could run this script 24/7...
so tehre is no "easy" way i can restart the script when it stops working?

jahboater
Posts: 2858
Joined: Wed Feb 04, 2015 6:38 pm

Re: restart script when failing

Sun Feb 04, 2018 10:50 am

You could run it as a service, or have a simple watchdog program.
But as Heater said, I would find out why its intermittently crashing first.

n67
Posts: 788
Joined: Mon Oct 30, 2017 4:55 pm

Re: restart script when failing

Sun Feb 04, 2018 11:04 am

The usual way to address this sort of thing is to write two scripts:

1) A simple script that just does (pseudo-code; hopefully this is clear enough to be translated into whatever language you are using):

:loop
RunWait "otherscript"
goto loop

This script is the one you actually run (by whatever means you like)

2) "otherscript" does the actual work. When it crashes/exits/whatevers, it just gets re-run.

Note that, in practice, this is usually all written as a single script, with a command line arg controling whether it functions in mode 1 or mode 2.
"L'enfer, c'est les autres"

If a post offends you, just put that poster on your foes list, and be done with it (and with them).

To do otherwise, risks being banned.

Heater
Posts: 9722
Joined: Tue Jul 17, 2012 3:02 pm

Re: restart script when failing

Sun Feb 04, 2018 2:33 pm

hotwalk,
yet i havent found any other solution how i could run this script 24/7...
so tehre is no "easy" way i can restart the script when it stops working?
The modern way to do this is to run your program as a systemd service.

I have some instructions and an example of how to run a javascript (node.js) program under systmd here: viewtopic.php?t=138861#p921354

Notice that in that example it sets an option for the service:

Restart=always

Which means systemd will restart the program automatically for you if it fails.

No need for a wrapper script to restart it as n67 suggests.

Still, if I were you I'd want to know why it is crashing all the time in the first place.

n67
Posts: 788
Joined: Mon Oct 30, 2017 4:55 pm

Re: restart script when failing

Sun Feb 04, 2018 2:58 pm

Gee, systemd is so way overkill.
And a real trap for the first timer, in so many, many ways.

Sometimes, as much as forum regs hate to admit it, a one-off really is the best solution.

Especially because, as noted above, the "extra script" can be embedded in the existing script, so you really don't need any additional scripts.
"L'enfer, c'est les autres"

If a post offends you, just put that poster on your foes list, and be done with it (and with them).

To do otherwise, risks being banned.

Heater
Posts: 9722
Joined: Tue Jul 17, 2012 3:02 pm

Re: restart script when failing

Sun Feb 04, 2018 5:37 pm

n67,
Gee, systemd is so way overkill.
How so?

Linux and Unix before it has always had a start up system. A way to get things running automatically at boot time.

There was SysV init, there was upstart, I'm sure there are others. Today Raspbian has systemd.

A handful of lines to configure a systemd service and away you go. No scripting. No muss, no fuss. Different than before but it does the job.
And a real trap for the first timer, in so many, many ways.
How so? What ways?

I'm no expert in init systems but setting things up as a systemd service has worked flawlessly for me for some years now.
Sometimes, as much as forum regs hate to admit it, a one-off really is the best solution.

Especially because, as noted above, the "extra script" can be embedded in the existing script, so you really don't need any additional scripts.
Perhaps.

But your proposal does not answer the question here.

Sure you can run something from a script. In whatever way you like. But how is that all getting started at boot time?

gkaiseril
Posts: 433
Joined: Mon Aug 08, 2016 9:27 pm
Location: Chicago, IL

Re: restart script when failing

Sun Feb 04, 2018 5:54 pm

I would try to find out why the script is failing in the first place.

JavaScript includes the "try{...}catch(e) { // report error}" statement as well as the "Error" object, so one can trap the cause of the error and report various variables at the time of the error. This could help you learn the source of the error.

Return to “General programming discussion”

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests