Some notes on writing Python script that involves background execution, kill signals

Notes from writing a python script that involved sending kill signals, wait for reaping child processes and other unix utilities.

  1. python Popen starts a process in the background (child process)
    1. It will run in background for the most part
  2. child process aren not terminated unless you wait of them in the patron process (zombie)
  3. what is stdin and stdout?
    1. Each process has its own stdin, stdout,
  4. what is SIGINT
    1. interrupt from keyboard
  5. Unix Process
    1. 3 states
      1. Running: executing on the CPU or waiting to be executed
      2. Stopped: suspended and will not be scheduled
      3. Terminated: the process is stopped permanently. A process become terminated for on of three reasons:
        1. receiving a signal whose default action is to terminate the process
        2. return from the main routine
        3. calling the exit function
  6. What is kill -9 -l
    1. kill all the process I can kill
    2. 9 is SIGKILL
  7. Why can’t I kill suspended process?
  8. Why can’t I use Ctrl-C to send SIGINT after I started background ocount process?
    1. because the shell’s handler catches the signal and send it to “foreground” process group
      1. ocount in this case is already in background
      2. so we have to explicitly use kill to send it to the background process
  9. Kill a process in python
    1. need to use os.kill, os.system with the command doesn’t seem to work

The script is here


import os

import datetime

import subprocess

import signal

def get_pid(name):

return subprocess.check_output(["pidof",name]).rstrip()

def is_process_running(process_id):


print "checking " + str(process_id)

os.kill(process_id, 0)

print "still running"

return True

except OSError:

return False

def profile (command, args):

#be default create a profile log directory if not existent

print "current pid: " +  str(os.getpid())

logDir = "./profileLogs/"

d = os.path.dirname(logDir)

if os.path.exists("./.ready"):

os.system("rm .ready");

if not os.path.exists(logDir):


outputdir = logDir + str(' ', '') + "/"


#execute the command and get the pid

execProcess = subprocess.Popen([command, args], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

pid =  get_pid(command)

#wait for the signal file to be created

while not os.path.exists("./.ready"):

os.system("sleep 3")

#start the occount process on pid

print "pid: " +  pid

os.system("ocount -e LLC_MISSES:0x41,LLC_REFS:0x4f -p " + pid + " > test.out & ")

os.system("sleep 1")


#output the result in the a file

#get the pid of the ocount

ocPid =  get_pid("ocount")

print "ocpid: " + ocPid

#clean up the ready file

os.system("rm .ready")

#while is_process_running(int(pid)):

#os.system("sleep 2");

#os.system("sleep 5")


os.kill(int(ocPid), signal.SIGINT);

os.system("cat ./test.out")

profile("./pagerankCsr.o", "Graph_S20_E26.graph_no_self_loops.1_0.gtgraph")

This entry was posted in Tools and tagged . Bookmark the permalink.

1 Response to Some notes on writing Python script that involves background execution, kill signals

  1. Pingback: Python Notes | Yunming Zhang's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s