1

INFO: I'm referring to this question I asked on Super User, but couldn't get an answer. Since my system is a RaspberryPi running Debian I think this is a good place for it.

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?

9
  • Yes this could be considered Raspberry Pi related, but the problem likely lies somewhere in your code, so are you sure this is the best place for programming help?
    – Darth Vader
    May 10, 2016 at 17:05
  • @DarthVader I'm thinking about asking on StackOverflow as well, but I was just curious if some other RPi owner had/has the same problem, since a lot of RaspberryPi's run servers I think. May 10, 2016 at 17:08
  • 1
    I like this question ... so you have processes that just "disappear". Some initial questions ... how are the node.js and script programs being started? What is returned by "uptime" (want to see if your PI rebooted itself).
    – Kolban
    May 10, 2016 at 17:48
  • @Kolban I like, that you like this question... uptime seems like a great start, but sadly I just restarted the pi and will check tomorrow. I started the scripts per hand via SSH. May 10, 2016 at 17:52
  • 1
    Did you SSH in from a remote machine? If so, remember that programs started from a shell opened from SSH are automatically stopped when you close the SSH connection. If you need a program to continue running after you logout, you need to run it in the background AND use the "nohup" command (see the nohup man page).
    – Kolban
    May 10, 2016 at 17:56

2 Answers 2

2

When one connects remotely to a Pi using a remote client (eg. ssh, telnet, etc) then a shell is opened. Commands can be run in the background using the "&" (ampersand) operator. However, when one closes the client connection, all processes that were started from the shell and run in the background are sent the "hangup" signal indicating that the terminal that they ran from is no longer present. The majority of processes that receive the hangup signal terminate themselves. As such, if you start a process in the background and then terminate the remote client, the process you started will likely terminate itself.

Fortunately, there is a simple solution. Linux/Unix provides a shell command called "nohup" (see the man page on nohup for details). This command takes the command you want to run as a parameter. It causes the command you want to run to ignore hangup signals (presumably by receiving the hangup itself and ignoring it).

For example:

$ nohup myprogram param1 param2 &

will cause myprogram to run in the background and when you terminate the shell that started the program, it will continue to run even though the shell that started it is gone.

1
  • New day, same server! Thanks for the great help again :) May 11, 2016 at 14:38
0

There's an npm package called "forever" you should try. I don't know about your specific problem, I don't think we have enough information to have a definitive answer, but I think you should look into that package. If the http server bonks it can restart it automagically, and you can hunt down the problem with some logging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.