205

In the bash terminal I can hit Control+Z to suspend any running process... then I can type fg to resume the process.

Is it possible to suspend a process if I only have it's PID? And if so, what command should I use?

I'm looking for something like:

suspend-process $PID_OF_PROCESS

and then to resume it with

resume-process $PID_OF_PROCESS
Lesmana
  • 27,439
Stefan
  • 25,300

2 Answers2

248

You can use kill to stop the process.

For a 'polite' stop to the process (prefer this for normal use), send SIGTSTP:

kill -TSTP [pid]

For a 'hard' stop, send SIGSTOP:

kill -STOP [pid]

Note that if the process you are trying to stop by PID is in your shell's job table, it may remain visible there, but terminated, until the process is fg'd again.

To resume execution of the process, sent SIGCONT:

kill -CONT [pid]
Manuel Jordan
  • 1,728
  • 2
  • 16
  • 40
  • 36
    Unless there are other reasons for it, I would prefer SIGTSTP over SIGSTOP, as some applications do handle SIGTSTP specially. For example, if scp is showing a progress bar, SIGTSTP will cause it to clean up the terminal mode before suspending, but if you send SIGSTOP, it will not have a chance to do so. – ephemient Sep 15 '10 at 21:55
  • 3
    @ephemient I tried SIGTSTP, I saw what you were saying about it cleaning up the terminal. Thanks for the explanation of SIGTSTP, alawys good to learn new things :) – Steve Burdine Sep 15 '10 at 22:38
  • 4
    Also useful to note that you can reference the [pid] value by using the % symbol and then the job number (one that you can find by running jobs). So you'd go: kill -TSTP %1 – Karoh May 02 '16 at 22:58
  • 2
    See also: [http://stackoverflow.com/questions/11886812/whats-the-difference-between-sigstop-and-sigtstp#11888074] – AAAfarmclub Dec 27 '16 at 08:15
  • Why do you mean by it may remain visible there, but terminated** ? It's not terminated but stopped/paused. I tried kill -STOP and it behaves exactly like kill -TSTP for shell jobs. – Rick Jul 29 '22 at 16:29
71

You should use the kill command for that.

To be more verbose - you have to specify the right signal, i.e.

$ kill -TSTP $PID_OF_PROCESS

for suspending the process and

$ kill -CONT $PID_OF_PROCESS

for resuming it. Documented at 24.2.5 Job Control Signals.

Manuel Jordan
  • 1,728
  • 2
  • 16
  • 40
maxschlepzig
  • 57,532
  • 2
    I wonder what accident of history led to this answer getting fewer votes? The answers are nearly the same and this one came first.... – Wildcard Aug 16 '16 at 03:08
  • 13
    @Wildcard, when I created the answer I was a bit in a hurry, thus, it basically just contained the first part up to kill -TSTP (i.e. how to suspend). 1/2 year later, i.e. 2011, I revisited my answer and noticed its incompleteness. Thus, I edited it and added also the kill -CONT part. This should explain the difference in votes in comparison with Steve's answer. – maxschlepzig Aug 16 '16 at 07:25