10

I've coded a stock trading bot in Python3. I have it hosted on a server (Ubuntu 18.10) that I use iTerm to SSH into. Wondering how to keep the script actively running so that when I exit out of my session it won't kill the active process.

Basically, I want to SSH into my server, start the script then close out and come back into it when the days over to stop the process.

David D
  • 103
  • 1
  • 4
  • 1
    I'd take a look at [`tmux`](https://github.com/tmux/tmux), but you can also look into running your script as a daemon – wpercy Apr 01 '19 at 19:28
  • also here's a good question/answer on AskUbuntu about [How to keep processes running after ending ssh session?](https://askubuntu.com/questions/8653/how-to-keep-processes-running-after-ending-ssh-session) – wpercy Apr 01 '19 at 19:32

4 Answers4

4

You could use nohup and add & at the end of your command to safely exit you session without killing original process. For example if your script name is script.py:

nohup python3 script.py &

Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (kill -SIGHUP <pid>). This can be prevented using nohup, as it catches the signal and ignores it so that it never reaches the actual application.

Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22
3

You can use screen

sudo apt-get install screen

screen 

./run-my-script

Ctrl-A then D to get out of your screen

From there you will be able to close out your ssh terminal. Come back later and run

screen -ls

screen -r $screen_running

The screen running is usually the first 5 digits you see after you've listed all the screens. You can see if you're script is still running or if you've added logging you can see where in the process you are.

0

Using tmux is a good option. Alternatively you could run the command with an & at the end which lets it run in the background.

https://tmuxcheatsheet.com/

0

I came here for finding nohup python3 script.py &

Right solution for this thread is screen OR tmux. :)

Anto
  • 3,128
  • 1
  • 20
  • 20