how to keep a python script running when I close putty

You have two main choices:

  1. Run the command with nohup. This will disassociate it from your session and let it continue running after you disconnect:

    nohup pythonScript.py
    

    Note that the stdout of the command will be appended to a file called nohup.out unless you redirect it (nohup pythonScript.py > outfile).

  2. Use a screen multiplexer like tmux. This will let you disconnect from the remote machine but then, next time you connect, if you run tmux attach again, you will find yourself in exactly the same session. The command will still be running (it will continue running when you log out) and you will be able to see its stdout and stderr just as though you'd never logged out:

    tmux 
    pythonScript.py
    

    Once you've launched that, just close the PuTTY window. Then, connect again the next day, run tmux attach again and you're back where you started.


The screen tool, available for all Linux distros, supports this.

To install it, run apt-get install screen for deb-based Linux distros, or dnf install -y screen or yum install -y screen for RPM-based ones.

To use:

$ screen

A new shell is started. In this shell, you can start your Python script. Then you can press Ctrl+Shift+A then D. It will detach your terminal from the shell that is running your script. Furthermore, the script is still running in it.

To see how your script is running, you can call screen -r. This will reattach your terminal to the shell with the Python script you left running in background.

UPD: as Fox mentioned, screen works badly with systemd, but we can use systemd to start the script, like they say in official example.

For example, if your script is started by /usr/bin/myPythonScript, you can create Systemd unit file, like this.

$ cat /etc/systemd/system/myPythonScript.service

[Unit]
Description=MyPythonScript

[Service]
ExecStart=/usr/bin/myPythonScript

[Install]
WantedBy=multi-user.target

Than, you can start this script # systemctl daemon-reload # systemctl start myPythonScript

If you want to make this script started automatically on system start -

# systemctl enable myPythonScript

Anytime you can view how your script is running

# systemctl status myPythonScript

Ad you can review logs of your script

# journalctl -u myPythonScript -e

Tags:

Python

Putty