简体   繁体   中英

HTTP Server stops after some time (Node.js)

INFO: I'm referring to this question I asked on Super User, but couldn't get an answer and I think this is a good place to ask, since the problem is probably code related.

I'm currently running a simple Node.JS server with express.js on my RaspberryPi with Debian installed on it. Everything works fine, but every morning I wake up to see my server isn't running anymore (the server process I started with the command node main.js ).

My first guess was, that the Pi has some kind of sleep mode, which it enters after a couple of hours without traffic/etc, and which shuts down the server, but I also run a dydns-client, which is still up every morning ( I also was informed, that the RaspberryPi doesn't come with a sleep mode ).

I wrote a simple script to check whether the process is running and writes it into a log file, but today morning I had to notice, that this script was wasn't running as well (only for around two hours, it logs the server state every 15 seconds and the last state was running ).

Here is the script:

#!/bin/sh                                                                                                                                     

MATCH="SOME_PROCESS_NAME"                                                                                                

while [ true ]                                                                                                                                
 do                                                                                                                                            
   if [ "$(ps -ef | grep ${MATCH} | grep -v grep)" ]; then                                                                                     
     echo "$(date): Process is running ..."                                                                                                    
   else                                                                                                                                        
     echo "$(date): Process has ended ..."                                                                                                                                                                                                                    
   fi                                                                                                                                          

   sleep 15                                                                                                                                     
done 

Is there a way to track a process after I started it to check tomorrow morning, what killed my process or why it ended (the script obviously didn't work)?

The server itself looks pretty simple and I don't think there is some kind of auto-shutdown I missed. Here is the code I used.

var express = require('express');
var path    = require('path');

var server  = express();
server.use(express.static(path.join(__dirname, 'public')));

server.listen(1337);
console.log("Server listening (PORT: " + 1337 + ") ...");

Any idea what to do, to keep the server running/find out what is the stopping reason?

UPDATE: I received a working answer over at RaspberryPi -stackexchange.

My guess is the Raspberry Pi restarts at midnight or something similar. to fix this maybe add an entry for your server process rc.local file. you can add commands to the rc.local file by editing /etc/rc.local

Would this helps https://raspberrypi.stackexchange.com/questions/8741/when-does-the-os-kill-an-application ?

I would like to suggest a different approach to monitor your process until you can get more information, to edit, then check, then start (wrote on the fly)

var fs = require('fs')
var spawn = require('child_process');

var child = spawn(process.argv[0], 'your/bin.js', {stdio:['ignore', 'pipe', 'pipe']})

child.stdout.pipe(fs.createReadStream('stdout.log'))
child.stderr.pipe(fs.createReadStream('stderr.log'))
child.on('error', function (err) {
  fs.writeFile('error.log', JSON.stringify(err), function () { /* void */ })
})
child.on('close', function (code, signal) {
  fs.writeFile('exit.log', "code="+code+" signal="+signal, function () { /* void */ })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM