Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Practically, I need a process which behaves, as if I had pressed Ctrl+Z just after it started.

Hopefully, it is possible to do such thing using a shell script.

(Also, knowing the resulting PID would be great, so I could continue the process afterwards.)

share|improve this question
up vote 6 down vote accepted

After starting a process, you can send it SIGSTOP to suspend it. To resume it, send SIGCONT. I think that this little script may help you

$@ &
kill -STOP $PID
echo $PID
wait $PID

It runs process (command send as parameter), suspends it, prints process id and wait until it ends.

share|improve this answer
I modified it to continue at the end: [enter] #!/bin/bash [enter] $@ & [enter] PID=$! [enter] kill -STOP $PID [enter] echo "Suspended: $PID, press ENTER to continue it" [enter] read [enter] kill -CONT $PID [enter] wait $PID [enter] echo – Jul 25 '11 at 5:35
Just so you know, the subprocess may finish before you even get a chance to send the STOP signal to it. – MikeyB Jul 25 '11 at 13:06
Another day at the races. – richard Apr 16 '15 at 12:11

From which environment are you creating the process?

If you're doing it from an environment such as C code, you can fork() and then in the child, send a SIGSTOP to yourself before the exec(), preventing further execution.

A more generic solution (probably best) would be to create a stub that does so. So the stub program would:

  • Take arguments consisting of the real program's name and arguments
  • send a SIGSTOP to itself
  • exec() the real program with appropriate arguments

This will ensure that you avoid any sort of race conditions having to do with the new processing getting too far before you can send a signal to it.

An example for shell:

kill -STOP $$
exec "$@"

And using above codez:

michael@challenger:~$ ./ ls -al

[1]+  Stopped                 ./ ls -al
michael@challenger:~$ jobs -l
[1]+ 23143 Stopped (signal)        ./ ls -al
michael@challenger:~$ kill -CONT 23143; sleep 1
-rwxr-xr-x 1 michael users 36 2011-07-24 22:48
[1]+  Done                    ./ ls -al

The jobs -l shows the PID. But if you're doing it from a shell you don't need the PID directly. You can just do: kill -CONT %1 (assuming you only have one job).

Doing it with more than one job? Left as an exercise for the reader :)

share|improve this answer
I was hoping to do this from a shell script... – Jul 24 '11 at 23:32
Great, so since you want to do this from a shell script, use the stub program. – MikeyB Jul 25 '11 at 2:46

MikeyB's answer is correct. From this question on superuser, here's a more concise version:

( kill -SIGSTOP $BASHPID; exec my_command ) &

To understand this, one needs to understand how processes are started on unix: the exec system call replaces the currently running program with a new one, preserving the existing PID. So the way an independent process is created is to first fork, and then exec, replacing the running program in the child process with the desired program.

The ingredients of this shell command are:

  1. The parentheses (...) start a sub-shell: another instance of BASH.
  2. The kill command sends the STOP signal to this process, which puts it into the suspended state.
  3. As soon as you allow the process to continue (by sending it the CONT signal), the exec command causes it to replace itself with your desired program. my_command keeps the original PID of the sub-shell.
  4. You can use the $! variable to get the PID of the process.
share|improve this answer
I like this one, as it's fundamentally correct (just that I had to use -s STOP instead of -SIGSTOP). Still wonder: why it has to be $BASHPID instead of $$? (I may not really understand the difference). – Hibou57 Jul 15 '14 at 8:36
For $$ vs $BASHPID, I check the latter is the PID of the sub‑shell, while not the former. There is a funny issue: applying the tip in a bash script, most often fails, and I have to do something like this: PID=$!; ps -p $PID; kill -s CONT $PID;… it fails if I remove the ps -p $PID part: the process seems to disappears (killed?) without an error message anywhere. It's OK from a interactive shell, the issue is only from script. That's too much mysterious. – Hibou57 Jul 15 '14 at 9:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.