A monitoring script test1.py written in python runs all the time in while True. When ssh is remote (using putty terminal), it starts the script with the following command:
The code copy is as follows:
python test1.py &
Now the script is running normally, and you can see the process number through ps. At this time, you directly close the ssh terminal (not using the exit command, but directly execute it through the putty's close button). After logging in again, you find that the process has exited.
The problem has been solved through the background startup method. Here is a summary, which is convenient for me to check it in the future.
Running in the background under Linux
Implemented through fork
In the Linux environment, the daemon process in C is implemented through fork, and Python can also be implemented through this method. The example code is as follows:
The code copy is as follows:
#!/usr/bin/env python
import time,platform
import os
def funzioneDemo():
# This is a specific business function example
fout = open('/tmp/demone.log', 'w')
While True:
fout.write(time.ctime()+'/n')
fout.flush()
time.sleep(2)
fout.close()
def createDaemon():
#fork process
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)
# Redirection standard 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())
# Execute code in child process
funzioneDemo() # function demo
if __name__ == '__main__':
if platform.system() == "Linux":
createDaemon()
else:
os._exit(0)
Implemented through upstart
The application can be encapsulated into system services through upstart, and the complete example is recorded here directly.
1. Write python scripts
The code copy 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 upstat configuration file
The code copy is as follows:
[root@local t27]# cat /etc/init/mikeTest.conf
description "My test"
author "[email protected]"
start on runlevel [234]
stop on runlevel [0156]
chdir /test/t27
exec /test/t27/test123.py
respawn
3. Reload upstate
The code copy is as follows:
initctl reload-configuration
4. Start the service
The code copy 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 service
The code copy 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]#
Implemented through bash script
1. Python code
The code copy 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 a startup script
The code copy is as follows:
[root@local test]# cat start.sh
#! /bin/sh
python test123.py &
3. Start the process
The code copy is as follows:
[root@local test]#./start.sh
If you use & start the process directly:
The code copy is as follows:
python test123.py &
Close the ssh terminal directly will cause the process to exit.
Implemented through screen, tmux and other methods
If you run the program temporarily, you can start the program through screen or tmux. Here is a description of how tmux starts.
1. Start tmux
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.py
3. Turn off the ssh terminal directly (such as the close button on putty);
4. After resshing, execute the following command:
The code copy is as follows:
tmux attach
You can now see that the python program is still executing normally.
Run in the background under windows
I haven't studied it in depth in Windows. The method I often use is to modify the extension of the python script to ".pyw", and double-click to run in the background without modifying any code.