Home > Article > Web Front-end > Several ways to run Python scripts in the background
A monitoring script test1.py written in python, running in while True mode, start the script through the following command when ssh remote (using putty terminal):
The code is as follows:
python test1.py &
Now the script is running normally and the process number can be seen through ps. At this time, the ssh terminal is closed directly (not with the exit command, but directly through putty) button executed), after logging in again, I found that the process had exited.
The problem has been solved through background startup. I will summarize it here for my future reference.
linux runs in the background
implemented through fork
In the Linux environment, the daemon process in c is implemented through fork. Python can also be implemented in this way. The sample code is as follows:
The code is as follows:
#!/usr/bin/env python import time,platform import os def funzioneDemo(): # 这是具体业务函数示例 fout = open('/tmp/demone.log', 'w') while True: fout.write(time.ctime()+'\n') fout.flush() time.sleep(2) fout.close() def createDaemon(): # fork进程 try: if os.fork() > 0: os._exit(0) except OSError, error: print 'fork #1 failed: %d (%s)' % (error.errno, error.strerror) os._exit(1) os.chdir('/') os.setsid() os.umask(0) try: pid = os.fork() if pid > 0: print 'Daemon PID %d' % pid os._exit(0) except OSError, error: print 'fork #2 failed: %d (%s)' % (error.errno, error.strerror) os._exit(1) # 重定向标准IO sys.stdout.flush() sys.stderr.flush() si = file("/dev/null", 'r') so = file("/dev/null", 'a+') se = file("/dev/null", 'a+', 0) os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) # 在子进程中执行代码 funzioneDemo() # function demo if name == 'main': if platform.system() == "Linux": createDaemon() else: os._exit(0)
It is implemented through upstart method
The application can be encapsulated into a system service through upstart. The complete example is recorded directly here.
1. Write python script
The code is as follows:
[root@local t27] # cat test123.py #!/usr/bin/env python import os,time while True : print time.time() time.sleep(1)
2. Write upstatConfiguration file
The code is as follows:
[root@local t27] # cat /etc/init/mikeTest.confdescription "My test"author "Mike_Zhang@live.com" start on runlevel [234]stop on runlevel [0156] chdir /test/t27exec /test/t27/test123.pyrespawn
3. Reload upstate
The code is as follows:
initctl reload-configuration
4. Start the service
The code is as follows:
[root@local t27]# start mikeTest mikeTest start/running, process 6635 [root@local t27]# ps aux | grep test123.py root 6635 0.0 0.0 22448 3716 ? Ss 09:55 0:00 python /test/t27/test123.py root 6677 0.0 0.0 103212 752 pts/1 S+ 09:56 0:00 grep test123.py
5. Stop the service
The code is as follows:
[root@local t27]# stop mikeTest mikeTest stop/waiting [root@local t27]# ps aux | grep test123.py root 6696 0.0 0.0 103212 752 pts/1 S+ 09:56 0:00 grep test123.py [root@local t27]#
Achieved through bash script
1 , python code
The code is as follows:
[root@local test]# cat test123.py#!/usr/bin/env python import os,time while True : print time.time() time.sleep(1)
2. Write startup script
If Directly use & to start the process:
[root@local test]# cat start.sh#! /bin/sh python test123.py &
The code is as follows:
Close the ssh terminal directly, which will cause the process to exit.
Achieved through screen, tmux, etc.
[root@local test]#./start.sh
If you want to run the program temporarily, you can start the program through screen, tmux. Here is a description of how to start tmux.
1. Start tmux
python test123.py &
Enter tmux in the terminal to start
2. Start the program in tmux
Just execute the following command directly (refer to the script above): python test123.py3. Directly close the ssh terminal (such as the close button on putty);
4. After sshing up again, execute the following command:
The code is as follows:
Now you can see that the python program is still there Execute normally.Running in the background under windows
I have not studied it in depth under windows. The method I often use is to modify the extension of the python script to ".pyw" and double-click it. It can be run in the background without modifying any code.
The above is the content of several ways of running Python scripts in the background. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!