Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: send SIGTSTP signal (ctrl+z)

I'm rushing against the clock for a programming assignment in which I have to run a number of instances of the same program on the same machine at the same time. Currently, I'm starting the instances one at a time, pressing Ctrl+z to pause them, and then doing 'bg %#' to resume execution in the background.

This is extremely tedious and time consuming to do every time I need to test a small change in my application, so I want to write a bash script that will start the multiple instances for me, however I don't know how to do the background switching in a script.

Can anybody please tell me how I can write a simple script that will start a long standing command, pause it, and resume it in the background?

Thanks

like image 301
noobler Avatar asked Mar 28 '12 04:03

noobler


People also ask

What does Ctrl Z do in bash?

ctrl+z stops the process and returns you to the current shell. You can now type fg to continue process, or type bg to continue the process in the background. Research "bash job control" and see bash manual Job Control Basics.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script.

What is if [- Z in bash?

In both cases, the -z flag is a parameter to the bash's "test" built-in (a built-in is a command that is built-into the shell, it is not an external command). The -z flag causes test to check whether a string is empty. Returns true if the string is empty, false if it contains something.

What is $() in bash script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.


1 Answers

Do you want to just start it in the background? For example:

mycommand &

If you want finer grained job control, you can emulate Ctrl-Z and bg. Control-Z sends SIGTSTP ("tty stop") to the program, which suspends it:

kill -TSTP [processid]

And the bg command just sends it a SIGCONT:

kill -CONT [processid]
like image 83
Edward Thomson Avatar answered Sep 28 '22 12:09

Edward Thomson