2

I'm currently working on a stub for tests purpose. Using Python I need to create a process with a specific name ("mysoft") and a specific pid ("1234")

My final purpose is to be able to run the command "pgrep mysoft" on a terminal and get the PID I set (1234).

The process doesn't need to do anything, it just need to exists.

I looked at the subprocess module but I think this is not exactly what I need. What do you think ?

5
  • I don't think it is possible to set a PID to a process. That is the job of the OS & not the user. Commented Apr 23, 2013 at 14:00
  • What would happpen if the PID is already in use? Commented Apr 23, 2013 at 14:00
  • Ok, i get what you mean, thanks. Let's say I just want to create a process running for a long time with the specific name "mysoft" so that the command "pgrep mysoft" will return a pid. How could I do that? Commented Apr 23, 2013 at 14:06
  • OK, why do you need pgrep to return a specific PID? Commented Apr 23, 2013 at 14:19
  • 3
    @JulienGreard - name your python script 'myfile', make it executable and start it with a shebang #!/usr/bin/python (not #!/usr/bin/env python!). Now when you execute it directly it should show up as mysoft in the process table. Commented Apr 23, 2013 at 14:32

2 Answers 2

3

To run a process with the name mysoft,

  • Create a python with the name mysoft without .py extension.
  • Inside that file create a endless while loop or something like that, in a way that it runs long time. Or put a line like raw_input("enter something"). It will wait until you give the input.
  • Make the file executable by chmod 775 [filename]
  • First line of this file should be #!/usr/bin/python. Change this line according to your python path.
  • Put this file system path. Or add this file path to system path. (eg. /home/[user]/bin/)
  • Now, type mysoft. It will start.

You need to kill this manually when you want to terminate this process. Setting pid to a process is not possible to my knowledge.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't create processes with specific PIDs. The PID is assigned by the OS.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.