2

I want to test some bug, and to do that I need to have a situation where the PID is larger than 99999. I am thinking of a bash script, a for loop. Any ideas?

terdon
  • 242,166
drpaneas
  • 2,320

2 Answers2

2

You could do something like this to start a process with the desired PID.

while true; do bash -c '[[ "$$" == 99999 ]] && echo PID is 99999'; done

You can wait until you get the desired PID and probably replace the echo statement to whatever you actually need to test.

References

will the same pid be used after getting killed?

EDIT

Why the PID number grows? Is it due to the fact you run a different bash instance over and over again?

I believe what you have told is what's happening. This could be tested simply as below.

while true; do bash -c 'echo $$' ; done

We could see that the PID's keep getting incremented and from the man page of bash, I see that,

-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

So as per my understanding, each echo statement in the above testing is getting executed as a separate process which is why we could see that the PID's are getting incremented.

Ramesh
  • 39,297
  • Thanks very much. You have a minor mistake. Replace the double quotation marks in with single. Otherwise, the equality check operator doesn't work. (e.g. '$$' == 99999 ) – drpaneas Oct 14 '14 at 18:15
  • @drpaneas, it worked with double quotes for me. Not sure if it needs to be replaced. Are you getting an error? – Ramesh Oct 14 '14 at 18:17
  • For me it doesn't work with double quotes. It treats the number as string (maybe). eg [["9999" == 9999 ]] returns false (in my shell). – drpaneas Oct 14 '14 at 18:18
  • Oh ok. Glad that the solution worked with single quotes for your test case. :) – Ramesh Oct 14 '14 at 18:19
  • 1
    Thank you very much, once again. Could you please explain, why the PID number grows? Is it due to the fact you run a different bash instance over and over again? – drpaneas Oct 14 '14 at 18:22
  • @drpaneas, That's a very good question. I believe whatever you have mentioned is what's happening and updated the answer. – Ramesh Oct 14 '14 at 18:32
1

Whatever you are trying to do there is most probably the better approach, but if you are determined something like

while [[ 1 == 1 ]]; do sleep 10000& done

will start many sleeps, but it may take a while to start all of them. When you will have enough processes just hit Ctrl-C to exit while loop.

jimmij
  • 47,140