27

I typed help suspend and got this short explanation:

suspend: suspend [-f]
    Suspend shell execution.

    Suspend the execution of this shell until it receives a SIGCONT signal.
    Unless forced, login shells cannot be suspended.

    Options:
      -f    force the suspend, even if the shell is a login shell

    Exit Status:
    Returns success unless job control is not enabled or an error occurs.

How I understand this is: I type suspend and the terminal freezes, not even strg + c can unfreeze it. But when I open another terminal and search for the PID for the frozen one and type kill -SIGCONT PID-NR a SIGCONT signal is send to the frozen terminal and thaws it up, so that it gets unfrozen.

But, what is the actual purpose of suspending a terminal? Which every day applications are typical for it? What did the people who made it a shell builtin have in mind?

1
  • 2
    avoid running external programs (kill) for suspend. Commented May 11, 2017 at 12:29

2 Answers 2

30

If you start a shell from another shell, you can suspend the inner one. Say when using su, and wanting to switch back to the regular user for a moment:

user$ su
Password: ...
root# do something
root# suspend
user$ do something as the ordinary user again
user$ fg
root# ...

(If you do that, don't forget the privileged shell open in the background...)

Similarly, if you escape to a shell from some other program (the ! command in e.g. less), you can still suspend the shell. But I wouldn't expect many other programs to handle it nicely when they launch a subprocess, which then suspends itself.

3
  • 1
    I just tried running vim, and did a :sh to get a subshell (specifically, a zsh subshell). Running suspend popped me into the original shell (it suspended both the subshell and vim).
    – Kevin
    Commented May 11, 2017 at 17:34
  • 1
    @Kevin, zsh seems smarter in that than bash, I tried escaping to both from both less and vi (Debian's vim.tiny): zsh stopped the parent app too, bash hung the whole thing. bash to bash works though.
    – ilkkachu
    Commented May 11, 2017 at 18:11
  • 1
    i was not aware, that what i type sudo su a subshell is started. when I typed suspend in the main shell the shell simply freezes, but how you showed, when i supsend it inside the subshell I go automatically back to the main shell.
    – sharkant
    Commented May 12, 2017 at 10:22
27

That's the equivalent of pressing Ctrl+Z in other commands.

It suspends the shell and gives control back to parent shell or process if any.

Example:

zsh$ bash
bash-4.4$ cd /
bash-4.4$ suspend
zsh: suspended (signal)  bash
zsh$ fg
[1]  + continued  bash
bash-4.4$ pwd
/

The feature comes from csh, the shell of BSD (where job control comes from) in the early 80s.

In AT&T ksh, it's a builtin alias for kill -s STOP $$ (yes, without the quotes!)

In your case, bash was probably the one started directly by the terminal emulator. And your terminal emulator didn't expect the process to be suspended.

That bash was a session leader. If the session leader is suspended, if we take the view of old time terminals, the user will have no way to resume it.

bash addresses that by refusing to suspend if it's a login shell. But in your case, your terminal emulator probably doesn't start bash in login mode, so that safeguard is not in place.

zsh and mksh don't have the problem because they send a SIGTSTP (the one also sent upon Ctrl+Z) signal like csh instead of SIGSTOP (and to the caller's process group for mksh like in csh, and to the main process group of the shell for zsh, not the $$ process alone). SIGTSTP is ignored when delivered to an orphaned process group, and the group of the leader qualifies. The idea is that SIGTSTP should not suspend something that is not resumable by a user.

In mksh or yash, one can also use suspend to have a subshell suspend itself:

$ (set -x; sleep 1; suspend; sleep 2)
+ sleep 1
+ suspend
[1] + Stopped(SIGSTOP)     (set -x; sleep 1; suspend; sleep 2)
$ fg
[1] (set -x; sleep 1; suspend; sleep 2)
+ sleep 2

That wouldn't work with zsh that sends the SIGTSTP to the main process group instead of the caller. In any shell that has kill builtin, one can always use kill -s TSTP 0 instead.

2
  • 4
    You can cite a common use for suspend is: in bash (and probably lots of shells) : when you started a long command, and don't want to interrupt it but whished you did that in the background (ie, you want command .... & but you forgot the & and now wish you could add it without interrupting the command) : you can : ctrl-z, and then type: bg to resume the latest suspended task in the background. (or bg %n to do this with task number n. Use jobs to get a list of suspended or running tasks) Commented May 11, 2017 at 13:19
  • 6
    @OlivierDulac, that wouldn't explain what the actual suspend command is for.
    – Wildcard
    Commented May 12, 2017 at 3:29

You must log in to answer this question.

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