-1

How work the Control+Z by command line not by keyboard key? (on Linux)

I'm need stop all terminal are running and resume it later.

3
  • 2
    You asked this before at unix.stackexchange.com/questions/386200 , and with this duplicate you have not actually made clear the things that people have asked to be made clear.
    – JdeBP
    Aug 16, 2017 at 8:49
  • Not a true duplicate. This question is about Ctrl-Z, the other is about Ctrl-S, but the answer is actually the same.
    – xhienne
    Aug 16, 2017 at 10:34
  • No, the other one is not about Ctrl+S. At least that much is stated in the question.
    – JdeBP
    Aug 24, 2017 at 13:07

3 Answers 3

4

Control+Z writes the control character susp, which sends the signal SIGTSTP (number 20 on Linux amd64, 24 on Solaris amd64, 18 on FreeBSD amd64... it's better to avoid referring to signals by number as that's not portable) to all the processes in the foreground process group of the terminal.

You can send a signal to stop a process from the command line by using the kill-command.

kill -s STOP <pid>

(where <pid> is the id of the process you want to suspend) or, if you want to address all processes of a program with a certain name (technically, all processes whose name match a given extended regular expression):

pkill -STOP xterm

(where xterm is an example name).

When you need to resume the processes, send signal SIGCONT in the same way.

6
  • 1
    It sends the SIGTSTP (20 on Linux on amd64 at least) signal, not SIGSTOP, and to the foreground process group of the terminal device. SIGTSTP can be handled, not SIGSTOP. Aug 16, 2017 at 8:19
  • 1
    but how to know the number of session the terminal is running
    – abu ali
    Aug 16, 2017 at 8:20
  • 1
    What is the meaning of pid ?
    – abu ali
    Aug 16, 2017 at 8:27
  • 1
    @abuali "PID" is short for "process ID". See en.wikipedia.org/wiki/Process_identifier
    – Kusalananda
    Aug 16, 2017 at 8:35
  • @ Stéphane Chazelas how konw process ID special in the the terminal opened
    – abu ali
    Aug 16, 2017 at 8:57
3

If you wanted to stop all the processes that have /dev/pts/12 as their controlling terminal (including all the foreground and background jobs running in that terminal and the shell), you could do:

pkill -STOP -t pts/12

That would also suspend the session leader. In some cases, that one may be resumed automatically. For instance, GNU screen resumes the session leader as soon as it detects that it has been suspended.

If you wanted to send the SIGTSTP signal to the foreground process group of that terminal like ^Z would you'd do:

kill -s TSTP -- "-$pgid"

Where $pgid is the process group ID of that foreground process group. On Linux and BSDs at list, you can find out which it is by running ps -o tpgid= -p "$pid" where $pid is the id of any process that has that terminal as its controlling terminal. On most systems, any of those processes could also find it out using tcgetpgrp().

In both cases, you resume those processes by sending the SIGCONT signal in place of SIGSTOP/SIGTSTP.

0

Send the STOP signal to the process(es) that you would like to suspend.

You may later send the CONT signal to unsuspend a suspended process.

For example:

pkill -STOP terminal

This would suspend all terminal processes.

 pkill -CONT terminal

This would unsuspend all processes named terminal.

2
  • but how to know the number of session the terminal is running
    – abu ali
    Aug 16, 2017 at 8:20
  • @abuali I'm not sure what you mean. pkill will look up all processes matching the name that you supply and send the signal to all of those. If you want to limit to a particular user, add -u username.
    – Kusalananda
    Aug 16, 2017 at 8:22

Not the answer you're looking for? Browse other questions tagged .