简体   繁体   中英

How can I make a Python script keep running after I close my shell?

I've coded a Discord bot in Python. I have it hosted on a server that I use PuTTy to SSH into. Closing that terminal will obviously result in the bot ceasing to work. Does Python have a process management system that will allow me to keep a Python script running?

I'm running centOS.

It depends about how much experience you have programming in python. For example you could use daemonize (which I personally prefer). And this is the simplest example available (from the daemonize documentation)

from time import sleep
from daemonize import Daemonize

pid = "/tmp/test.pid"


def main():
    while True:
        sleep(5)

daemon = Daemonize(app="test_app", pid=pid, action=main)
daemon.start()

Another way to keep your script running, it could be install screen . Execute screen before executing your script and then detach the session using "Ctrl+a"+"d"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM