What's the Unix command-line symbol for "the PID of the last suspended process"?
Solution 1
You might be looking for the $!
variable (bash manual, section Special Parameters).
However, you don't need the PID – the built-in kill
command also accepts job identifiers, such as %2
, which are shown when you press Ctrl-Z or type jobs
. You can use %
, %+
or %%
to refer to the latest job. (Other possibilities are in bash manual, section Job Control.)
>>>
[4]+ Stopped python
$ kill %4
Solution 2
jobs -p %
It shows PID of last suspended job (after pressing Ctrl+z
).
Related videos on Youtube
Nathan Long
I code mostly in Ruby and Elixir. More about me at nathanmlong.com and Stackoverflow Careers.
Updated on September 18, 2022Comments
-
Nathan Long over 1 year
In an interactive console like
irb
, sometimes something will go wrong and the console becomes unresponsive. One solution is to suspend the process, then kill it.To suspend the process, I can press
Control+Z
. To kill that process, I can runps -ef | grep 'irb'
to list all the processes and show only the ones that contain 'irb', then get the process ID from that list and typekill [insert PID here]
.That's a hassle. I know that there's a way to just suspend the process, then type
kill [some symbol here]
, with the symbol representing "the last process that was suspended."What's the command-line symbol for "the PID of the last suspended process"?
-
Nathan Long over 12 yearsI think
kill %
was what I was looking for. Though I suppose this is actually the last job started rather than the last one suspended, for me this is typically the same one. -
user1686 over 12 years@NathanLong: No,
%
is the last suspended job. -
Brian Cannard over 6 years