Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what PID does os.killpg look for

I am writing code to kill a process and all children processes.

According to this post, all children processes can be killed within the same process group by using os.killpg(pro.pid, signal.SIGTERM)

During test, I launched this process manually which spawned 5 subprocesses.

    UID      PID  PPID  C STIME TTY          TIME CMD
    ddd    25066 19475  0 Nov03 ?        00:00:00 /bin/sh -c gtdownload -c ~/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/download/ab0e89b4-5310-11e4-88da-adc9fc308db6 2
    ddd    25067 25066  0 Nov03 ?        00:00:07 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25073 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25077 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25081 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25085 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d

However when I ran os.killpg(25066, signal.SIGTERM), I got the error "OSError: [Errno 3] No such process". Why can't it find the process with that ID?

like image 532
Nasreddin Avatar asked Nov 04 '15 18:11

Nasreddin


People also ask

What is OS Killpg?

The os. killpg() will help to stop the execution of the current process group.

What kill Python?

kill() method in Python is used to send specified signal to the process with specified process id. Constants for the specific signals available on the host platform are defined in the signal module. Parameters: pid: An integer value representing process id to which signal is to be sent.

How do you kill PID in Python?

You can kill a process via its pid with the os. kill() function. The os. kill function takes two arguments, the process identifier (pid) to kill, and the signal to send to kill the process.

How do you kill a function in Python?

exit() Function. The sys. exit() function in the Python sys module can be used to terminate a program and exit the execution process. The sys.


1 Answers

You need to set process group using os.setpgrp() before calling os.killpg(). If you don't set any process group then you won't be able to kill it using os.killpg()

You can create process group using following ways:

  1. os.setpgrp() -- If no argument is passed then it is equivalent to os.setpgrp(0,0). This will create a process group with id same as the calling process id.
  2. os.setpgrp(0, 999) -- It will create a process group with id 999 and the current process will be part of that group. You can use any process id instead of 0 to make it part of that process group.

os.setpgrp() actually calls linux system call setpgrp(). See following linux man page for detail: https://linux.die.net/man/2/setpgrp

http://man7.org/linux/man-pages/man2/setpgid.2.html

like image 154
Vivek Agrawal Avatar answered Oct 13 '22 20:10

Vivek Agrawal