Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Python script running in the background     - Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)

- Systemd on RHEL7 (Linux)

- Linux how to view your JDK installation (Linux)

- HAProxy performance under high concurrency (Server)

- CentOS yum install LNMP PHP5.4 version (Server)

- Install the Solaris 10 operating system environment over the network to sparc (Linux)

- SecureCRT connect virtual machine Linux system (Ubuntu) (Linux)

- Vim custom color (Linux)

- Common DDOS attacks (Linux)

- MongoDB simple replication configuration (Database)

- Use libcurl library download url image (Programming)

- Tomcat configuration memory and remote debug port (Server)

- Pydev installed and configured on the Eclipse (Linux)

- Ora-14450: attempt to access ah transactional temptable already in use (Database)

- Upload the project to GitHub, synchronous remote repository Github (Linux)

- Linux permissions Detailed (Linux)

- Snort build a secure Linux server (Linux)

- DM9000 timing settings (Programming)

- Java concurrent programming using the synchronized keyword ReentrantLock alternative primitive (Programming)

- AngularJS - Custom instructions (Programming)

 
         
  Python script running in the background
     
  Add Date : 2017-04-13      
         
         
         
  Problem Description:

Environment: CentOS6.4

The following command starts a script written in Python monitoring scripts test1.py, by the way has been running while True, the remote ssh (using putty terminal) when:

python test1.py &

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

By way of background to start the problem has been solved, summarized here, it is also easy to me later reference.

Under Linux running in the background

Realized by fork

Under Linux, daemon c is achieved by way of fork, python can also be achieved in this manner, the sample code is as follows:

#! / Usr / bin / env python
# E-Mail: Mike_Zhang@live.com
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 a child process
    funzioneDemo () # function demo

if __name__ == '__main__':
    if platform.system () == "Linux":
        createDaemon ()
    else:
        os._exit (0)

By upstart ways

By the upstart application packaged as a system service, here is recorded directly under complete example.

1, write python scripts
[Root @ local t27] # cat test123.py
#! / Usr / bin / env python

import os, time

while True:
    print time.time ()
    time.sleep (1)

2, the preparation upstat profile
[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
initctl reload-configuration

4, start the service
[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
[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] #

By bash scripts

1, python codes
[Root @ local test] # cat test123.py
#! / Usr / bin / env python

import os, time

while True:
    print time.time ()
    time.sleep (1)

2, start writing scripts
[Root @ local test] # cat start.sh
#! / Bin / sh

python test123.py &

3, the boot process
[Root @ local test] #. / Start.sh

If the direct use & start the process:
python test123.py &

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

By screen, tmux and other ways

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

1. Start tmux
Terminal input tmux to start

2, start the program in the tmux

Directly execute the following command (script reference to the above): python test123.py

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

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

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

Under Windows running in the background
Under Windows have not been thoroughly studied, I frequently used method is to modify the python script extension ".pyw", double-click to run in the background, without any code changes.
     
         
         
         
  More:      
 
- CentOS 6.4 install and configure Squid Proxy Server (Linux)
- shellinabox: one uses AJAX Web-based terminal emulator (Linux)
- Compare Swift achieve rapid sorting and sorted Methods (Programming)
- Linux network security strategy (Linux)
- gzip, bzip2, xz, tar, zip compression, archive Detailed (Linux)
- The difference Docker save and export commands (Linux)
- ORA-04031 error appears in the solution 11.2.0.4 run utlrp.sql (Database)
- Postfix mail service system principle and configuration (Linux)
- CentOS 7.0 Close firewall is enabled by default iptables firewall (Linux)
- Install Java on RHEL6 (Linux)
- Ubuntu 14.04 LTS 64-bit install GNS3 1.3.7 (Linux)
- Ubuntu install virtual machine software VirtualBox 4.3.10 (Linux)
- Use in Linux ipmitool tool (Linux)
- Java code JIT compiler-friendly Mody (Programming)
- RedHat Linux 6.4 installation RTL8188CUS wireless network card driver (Linux)
- Ten best plug surge Emacs Productivity (Linux)
- Python exception summary (Programming)
- C ++ function object (Programming)
- MySQL flip-flop (Database)
- SELinux multi-level security (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.