If your intent is to background the program, use:
./program & # The & sends the command to the background
echo commands here are executed while program is in the background
…
wait # Wait for the completion of background commands
echo commands here are executed after background program has completed
Edit: If your intent is to stop the program (as ctrl-Z often does in *nix shells), you can send it the STOP signal:
kill -STOP pid
To resume the execution, send it the CONT signal:
kill -CONT pid
In each of these examples pid
is the process id of the program. If you launch it in a script, it's easy to get with the variable $!
, e.g.
./prog &
echo prog started in the background
pid_of_prog=$!
kill -STOP $pid_of_prog
echo prog stopped
kill -CONT $pid_of_prog
echo prog continues
wait
echo prog finished
Edit 2: If your program is one that exits when it receives a ctrl-Z character, then remember that the control characters have the numerical value of the position letter in the alphabet (i.e. Ctrl-A is 1, Ctrl-B is 2, etc.). To send this character to a program you can:
echo -e "\032" | ./prog
(032
is 26, i.e. ^Z, in octal. Of course you can produce the same character by any means, perhaps adding it to the end of other input like ( cat inputfile ; echo -e "\032" ) | ./prog
.
But this may not necessarily work; the program must be designed to recognise this character from the input (which it probably won't); usually the shell catches it. Then again, most programs reading input from stdin
just exit when the input ends, so redirecting any finite input (even </dev/null
) should cause it to terminate.
And, finally, if the intent was to stop the execution of the program when some other event (detected elsewhere in the script) has occurred, you can just kill
it…