Your location:Tech News>News>Script>Several ways Python script running in the background

Latest News

Several ways Python script running in the background

Author:fsadmin

Views:

  A monitoring scripts written in python test1.py, it has been running a while True way, in a remote ssh startup script the following command (using a putty terminal):

  Copy the code code is as follows:

  python test1.py &

  Now running the script, by ps can see the process ID, this time directly off ssh terminal (not use the exit command is executed directly by putty close button), log in again after discovery process has retired.

  The problem has been solved by way of background to start, summarize here, I will also facilitate access to.

  Under linux running in the background

  Achieved by fork

  Linux environment, c daemon is achieved by way fork, Python may be realized by this embodiment, the following sample code:

  Copy the code code is as follows:

  #!/ Usr / bin / env python

  import time, platform

  import os

  def funzioneDemo ():

  # This is an example of a specific business function

  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)

  # Redirect the 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 the child

  funzioneDemo () # function demo

  if __name __ == ''__ main__'':

  if platform.system () == "Linux":

  createDaemon ()

  else:

  os._exit (0)

  Achieved upstart way

  The application can be encapsulated into the system upstart service, where the complete example directly recorded.

  1, write python script

  Copy the code 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 upstat profile

  Copy the code code is as follows:

  [Root @ local t27] # cat / etc / init / mikeTest.conf

  description "My test"

  author "Mike_Zhang @ live.com "

  start on runlevel [234]

  stop on runlevel [0156]

  chdir / test / t27

  exec / test / t27 / test123.py

  respawn

  3, reload upstate

  Copy the code code is as follows:

  initctl reload-configuration

  4, start the service

  Copy the code 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.00.0224483716 ? Ss 09:55 0:00 python / test / t27 / test123.py

  root 6677 0.00.0 103212 752 pts / 1 S + 09:56 0:00 grep test123.py

  5. Stop Service

  Copy the code code is as follows:

  [Root @ local t27] # stop mikeTest

  mikeTest stop / waiting

  [Root @ local t27] # ps aux | grep test123.py

  root 6696 0.00.0 103212 752 pts / 1 S + 09:56 0:00 grep test123.py

  [Root @ local t27] #

  Achieved by bash script

  1, python codes

  Copy the code 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 a startup script

  Copy the code code is as follows:

  [Root @ local test] # cat start.sh

  #! / Bin / sh

  python test123.py &

  3, start the process

  Copy the code code is as follows:

  [Root @ local test] #https: // www.jb51.net / article / start.sh

  If the direct use & start the process:

  Copy the code code is as follows:

  python test123.py &

  Ssh directly off the terminal will cause the process to quit.

  Achieved by screen, tmux etc.

  If the temporary run the program, you can through the screen, tmux start the program, under way described here tmux start.

  1, start tmux

  Input terminal to start in tmux

  2, start the program in tmux

  It can directly execute the following command (script refer to the above): python test123.py

  3, ssh directly off the terminal (such as off button on PuTTY);

  4, after the re-up ssh, execute the following command:

  Copy the code code is as follows:

  tmux attach

  You can now see the python program is still running normally.

  Under windows running in the background

  In the windows had not been thoroughly studied, I often used method is to modify the extension python script called ".pyw ", double-click to run in the background, without any code changes.

  You may also be interested in the article: three ways the arguments passed on the command line run Python scripts Detailed Discussion python script to set the operating parameters of the method examples to explain the Python script to be running Windows exe file to run CMD command line python script Regardless of the method of micro letter to hop automatically run python singleton run Python scripts and daemons Detailed Python scripts get run scripts directory of the current directory and how to run a python script with arguments

Recommend article

Relate article