How do I keep a program running after disconnecting from ssh?


I use the BBB to turn on my christmas light using a simple relay circuit. I made a simple server in python that listens to post requests, so far it works, I also use Microsoft Cognitive Services to send post requests based on speech commands.
When I start up the python server: nohup python3 & everything works fine while I am connected to the BBB over ssh but when I leave the server stops working.

Could you please give me some advice on how to keep python3 server running after I disconnect from ssh?

I retried with nohup and it works now. I think I had problems in the sender code.

Generally, all you need is ‘&’ to send the process to the background. So if I had a Nodejs script called ‘app.js’ . . .

$ node app.js &

This would start app.js through node, then send the process into the background. Giving me a PID which I could use later to kill the process.

There is kind of an involved way in C that is how it is done (similar to how Apache runs as a long-running daemon).

What you do is you fork() with the parameters set up such that it invokes a daughter shell process that survives after the parent process is terminated. The reason why the daughter process is shell, is so you can just call your python interpreter inside it. Even though it uses two languages, it is how its done for a stable service-type daemon.

I’ll check back if there is any interest in seeing my C code for how to do this (I wrote something a little while ago that required this approach, so I have the source on hand and its all ANSI posix-compliant C with no external libraries).

Look at the 'nohup' command. Basically you do:-

    nohup <my script> &

The & after the command puts it in the background, the nohup tells the
system not to send it a hangup signal when you disconnect ssh.