The example in this article describes how Python uses supervisor to manage processes. Share to everyone for your reference. The specific analysis is as follows:
Supervisor can start, stop, and restart programs in the *nix system. You can also restart a crash program.
Supervisord A daemon that is used to run the specified process as a child process.
SUPERVISORCTL is a client program that can view logs and control processes through a unified session.
See Example:
We wrote a py script to record a current time in the log file.
?
| 1 2 3 4 5 6 7 8 9 10 |
root@ubuntu:/home/zoer# cat daemon.py #!/usr/bin/env python import time import os Time.sleep (1) f=open ("Log", ' a ') t=time. Time () F.write (str (t)) F.write ("n") F.close () |
The installation process will not be said.
After installing supervisor, "Place the configuration file under/etc". Modify the configuration file, and at the end add the following:
[PROGRAM:DDD]
command=/home/zoer/daemon.py
Autorestart=true
Then we start the supervisor and start the daemon.py execution.
?
| 1 2 3 4 5 6 7 8 9 10 11-12 |
root@ubuntu:/home/zoer# Supervisord/usr/local/lib/python2.7/dist-packages/supervisor-3.0b1-py2.7.egg/supervisor /options.py:286:userwarning:supervisord is running as root and it are searching for their configuration file in default Loc Ations (including its working directory); You are probably want to specify a "-C" argument specifying a absolute path to a configuration file for improved security. ' Supervisord is running as root and it is searching ' root@ubuntu:/home/zoer# supervisorctl ddd starting supervisor> St Art DDD Ddd:error (already started) supervisor> stop ddd ddd:stopped supervisor> start DDD ddd:started Supervisor > |
From the example above, you can start or stop the DDD process through the start or Stop command. DDD Here is what we add to the configuration file (daemon.py this script).
You can also use restart. As follows:
supervisor> Restart DDD
ddd:stopped
ddd:started
Let's test it out, assuming that we manually kill the DDD process, will ddd automatically resume execution?
To do the experiment, revise the code as follows:
?
| 1 2 3 4 5 6 7 8 9 10 11 |
root@ubuntu:/home/zoer# cat daemon.py #!/usr/bin/env python import time import os while True:time.sleep (1) f=open ("Log", ' A ') T=time.time () f.write (str (t)) F.write ("n") F.close () |
The ID of this process can be found through PS:
?
| 1 2 3 |
Root 9354 0.2 0.4 10924 4200? S 23:16 0:00 python/home/zoer/daemon.py root 9395 0.0 0.0 4392 832 PTS/3 s+ 23:17 0:00 grep--color=auto Daemon Root@ubun tu:/home/zoer# |
Look at the following actions:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 |
root@ubuntu:/home/zoer# rm log;touch Log;kill 9354 root@ubuntu:/home/zoer# cat log 1364710712.51 root@ubuntu:/home/zoer # cat Log 1364710712.51 1364710713.51 root@ubuntu:/home/zoer# cat log 1364710712.51 1364710713.51 root@ubuntu:/home/zoer # cat Log 1364710712.51 1364710713.51 1364710714.52 root@ubuntu:/home/zoer# cat log 1364710712.51 1364710713.51 136471071 4.52 1364710715.52 |
The log file was deleted and recreated. and kill the daemon.py process. You'll find that the log content is new again. PS View the process number again.
?
| 1 2 3 |
Root 9429 0.1 0.4 10924 4200? S 23:18 0:00 python/home/zoer/daemon.py root 9440 0.0 0.0 4392 828 PTS/3 s+ 23:19 0:00 grep--color=auto Daemon Root@ubun tu:/home/zoer# |
You will find that the process number has become 9429. Indicates that supervisor has restarted the process of being killed.
I hope this article will help you with your Python programming.