0

I'm on a CentOS 6 system.

I'd like to know the pid of a curl command I'm running. Usually to get the pid of a process I use ps or pidof or pgrep or the like. But I generally use those for long running processes...often for processing running too long in fact and I want to kill them.

This situation is different. In my context, curl runs very quickly because the requests and responses are small. But I still need to know the pid of that process. Is there a way to execute a quickly running command so that it will tell you what pid it ran with? Or is there a way to retrospectively tell what the pid of your previous command was?

firebush
  • 131
  • 4
  • Finding the pid fulfills the requirements of a larger test framework that the test is a part of. Other clients run longer and may need to be killed and it uses the pid for that. That's less relevant for curl in the case of my test because it runs within a second. The current code races against curl finishing and the ps command looking for the pid of that curl command. – firebush Sep 27 '19 at 22:21

2 Answers2

2

curl has its own --max-time option that can set an upper bound for how long it will take. --max-time 60 will limit to a minute.


ps to find a PID of a backgrounded job is unreliable. You already found race conditions.

If you must manage the process in some other way, curl does not really report PID. However, any decent scripting language like Python or Perl has ways to fork a subprocess and set a timeout. An implementation isn't provided, but a recent thread on curl-users Re: Finding PID of a curl process suggests this is more the responsibility of the calling script than curl itself.

John Mahowald
  • 30,009
  • 1
  • 17
  • 32
1

In general your shell records the PID of a command, which is the usual method to use when the command itself does not provide native way to log the (main) process PID, and you can use something like :

 curl $options & 
 curl_pid=$!
HBruijn
  • 72,524
  • 21
  • 127
  • 192