BASH: send SIGTSTP signal (ctrl+z)
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]
How to correctly send SIGTSTP signal to a child process?
Your child process is stopped, not exited. If it had exited, WIFSTOPPED(status) would be 0 and WIFEXITED(status) would be nonzero.
To continue the process, when your fg
command is executed, send the SIGCONT
signal to the child:
kill(child, SIGCONT)
Creating my own shell. Handling Ctrl-Z and then sending SIGCONT closes the process instead of continue it
It seems like the problem is caused because of process groups. I did only create different process groups for background jobs but since you cannot change the process group of a child after it executed exec command, you better do it at the beginning before exec call. Now, the problem is solved thanks to "@that other guy" and "@John Bollinger".
Can successfully trap CTRL-Z, but not SIGTSTP
First you must not trap SIGTSTP for the shell itself (it should ignore it), only for its child. Second if you really want to write a job controller shell, you need to manage children with the help of process group and correctly set the foreground group. Writing a shell that behave correctly with job control is a heavy task. Read POSIX standard about shells, groups, sessions, terminal control.
About your current problem. If your sub process does an exec then each handled signal is reset to its default behavior. This is because exec recovers the old code with the new one, so the previously set handler is no more available. Now you must let the child behave normally against TSTP
and just let the parent track its status either with a synchronous call to wait/waitpid
or with the asynchronous help of SIGCHLD
. When the child stops or terminates, the parent is able to see it in the returned status (WIFEXITED
, WIFSIGNALED
, WIFSTOPPED
).
Related Topics
How to Block Push to Master Branch on Remote
How to Redirect Http to Https Using Gcp Load Balancer
Linux Kernel - How to Get Physical Address (Memory Management)
Install Library in Home Directory
Full Process Name from Task_Struct
How to Find Files Except Given Name
Difference Between Arm-None-Eabi and Arm-Linux-Gnueabi
The Repository 'Http://Dl.Google.Com/Linux/Chrome/Deb Stable Release' Is Not Signed
Find Which Program Caused a Core Dump File
Can't Remove, Purge, Unistall Mongodb from Debian
Why Kernel Needs Virtual Addressing
Linux: How to Lock The Pages of a Process in Memory
Mongo Daemon Doesn't Run by Service Mongod Start
Autoconf Complains "C Compiler Cannot Create Executables" on Linux Mint