21

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.)

java.is.for.desktop
  • 889
  • 3
  • 9
  • 15

3 Answers3

28

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:

#!/bin/bash
kill -STOP $$
exec "$@"

And using above code:

michael@challenger:~$ ./stopcall.sh ls -al stopcall.sh

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

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 :)

MikeyB
  • 38,725
  • 10
  • 102
  • 186
21

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.
nibot
  • 311
  • 2
  • 4
  • 2
    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 08:36
  • 1
    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 09:55
  • I tried to use this solution for listing file descriptors of my_command. (https://goo.gl/cNbUE8) but the descriptors are still the same no mather what's my_command. – rasty.g Nov 14 '17 at 08:11
  • For options to replace `$BASHPID` for shells other than bash, see [here](https://stackoverflow.com/a/20726041) – Jakob Aug 14 '19 at 04:08
  • I wonder: Isn't that code relying on a race condition?: The subshell with PID `$BASHPID` executes two commands in sequence, the `kill` and the `exec`. But if the signal arrived before `kill` exited, won't the shell be stopped before `exec` is started? Why not `( kill -SIGSTOP $BASHPID& exec my_command ) &`? That would still be a race condition, but with a better likelihood for success IMHO. – U. Windl Apr 20 '21 at 12:28
7

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

#!/bin/bash
$@ &
PID=$!
kill -STOP $PID
echo $PID
wait $PID

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

sciurus
  • 12,493
  • 2
  • 30
  • 49
radious
  • 1,002
  • 6
  • 5