chirag7jain
chirag7jain

Reputation: 1547

Fix Process ID For Python Script

I want to setup my python script to always use a fixed process id. So that every time I want to kill it I don't have to do a ps aux for it. Please Help.

I am using Ubuntu & CentOS.

Ubuntu is my testing system CentOS is my server

Upvotes: 0

Views: 748

Answers (3)

Martijn Pieters
Martijn Pieters

Reputation: 1124708

This is not something you can do, with python or any other process.

The process id is assigned by the Linux kernel, and there are guarantees as to the uniqueness of the id.

Moreover, if your process is used a child process of another, it's process id lives on in the kernel process table until the parent process has acknowledged that it has read the exit status. That means you cannot simply re-use the process id at a later time, it may still be reserved in the process table.

I'm sure you can devise a creative kill command that'll catch your process every time:

kill `ps -fC python2.7 | grep yourscriptname.py`

or similar.

Upvotes: 2

Sam Mirrado
Sam Mirrado

Reputation: 336

Why not write a small script for deleting your process:

#!/bin/sh
#Kill my python process called myPython
kill `ps -A | grep myPython | nawk '{ print $1}'`
# Or
kill `ps -U myname | grep myPython | nawk '{ print $1}'`

And then you can just run the script to kill the process...

Upvotes: 0

dav1d
dav1d

Reputation: 6055

This is impossible, Posix process ids are guaranteed to be random (e.g. OpenSSL uses the process id to seed it's random number generator). Only thing you can do, is writing the process id into a file and killing the process based on the written process id.

kill `cat x.pid`

Upvotes: 2

Related Questions