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.)
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:
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:
And using above code:
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's answer is correct. From this question on superuser, here's a more concise version:
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 firstfork
, and thenexec
, replacing the running program in the child process with the desired program.The ingredients of this shell command are:
(...)
start a sub-shell: another instance of BASH.kill
command sends the STOP signal to this process, which puts it into the suspended state.CONT
signal), theexec
command causes it to replace itself with your desired program.my_command
keeps the original PID of the sub-shell.$!
variable to get the PID of the process.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
It runs process (command send as parameter), suspends it, prints process id and wait until it ends.