1

I have an application written in python, which run on a VPS server. It is a small application that writes, reads and receives read requests from a SQLite database, through a TCP socket.

The downside is that the application runs only when the console is open (using the ssh protocol), when closing the console, that is, the ssh session closes the application. How should it be implemented? Or I must implement it? because the server is a ubuntu server

Julián Oviedo
  • 564
  • 5
  • 19

2 Answers2

1

nohup should help in your case:

  • in your ssh session, launch your python app prefixed with nohup, as recommended here
  • exit your ssh session

The program should continue working even if its parent shell (the ssh session) is terminated.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have tried the nohup command and it works but after a while the application closes. Could you please tell me why it could be? – Julián Oviedo Sep 08 '20 at 20:01
  • @JuliánOviedo That seems more related to the application run in your SSH session. Maybe the socket it uses gets closed, triggering the exit of that program. You would need to add some traces/logs in your program to get a better sense of its lifecycle. – VonC Sep 08 '20 at 20:06
  • Thanks with the answer. I think that the socket is actually closed since in the nohup.out file I does not show any error messages. How can I make the socket always active? Since I see that it is not closed by mistake but because it is not used for a long time – Julián Oviedo Sep 09 '20 at 23:14
  • @JuliánOviedo Can you try a `nohup 1>/tmp/error.log 2>&1 &` and see when the app gets eventually closed, is `/tmp/error.log` includes any clues? – VonC Sep 10 '20 at 06:46
  • I got this: File "servidor_tcp_python.py", line 169, in ejecucion_servidor() File "servidor_tcp_python.py", line 28, in ejecucion_servidor data = connection.recv(4096).decode("utf-8") UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 1: invalid$ – Julián Oviedo Sep 12 '20 at 14:03
  • @JuliánOviedo That looks like an issue with the application run in the SSH session, one from [PyMOTOW TCP](https://pymotw.com/2/socket/tcp.html). Its iisue looks like https://stackoverflow.com/a/35444608/6309. – VonC Sep 12 '20 at 14:12
1

There are (at least) two solutions:

1- The 'nohup' command, use it as follows: nohup python3 yourappname.py &

This will run your program in the background and won't be killed if you terminate the ssh session, It'll also give you a free prompt after running this command to continue your work.

2- Another GREAT option is the 'screen' command. This gives you everything that nohup gives you, besides It allows you to check the output of your program (if any) in later logins. Although It may look a little complicated at first sight, but it's SUPER COOL! and I highly recommend you to learn it and enjoy it for the rest of your life! A good explanation of it is available here

  • I have tried the nohup command and it works but after a while the application closes. Could you please tell me why it could be? – Julián Oviedo Sep 08 '20 at 20:01