suspend is a builtin command in Bash. When would you naturally use this command and find it useful?

  • 1
    The shell suspends itself everytime it forks a child process, i.e. runs a command where said command is an external program, requiring a fork/exec system call – the_velour_fog Jul 29 '16 at 6:45
  • 1
    In an interactive shell this allows the child process to get control of the terminal, whilst not having the parent shell interacting with the terminal, which gets messy. – the_velour_fog Jul 29 '16 at 6:51
  • 2
    @velour, but that doesn't require running a separate command to that effect? – ilkkachu Jul 29 '16 at 8:24
  • @the_velour_fog Technically, the parent shell is not suspended (it doesn't get a STOP signal) when it forks a child process. I'm also a bit confused by your second comment. – Kusalananda Jul 29 '16 at 10:55
  • 1
    @Kusalananda ok, right wait() sounds reasonable to me. I said The shell suspends itself everytime it forks a child process. I didn't say anything about receiving any stop signals, that would make no sense. – the_velour_fog Jul 29 '16 at 13:57

Let's say you lack both GNU screen and tmux (and X11, and virtual consoles) but want to switch between a login shell and another interactive shell.

You would first login on the console, and then start a new shell, temporarily blocking the login shell. To get the login shell back to do some work there, you'd do suspend. Then you would fg to get the interactive shell back to continue with whatever it was you did there.

In fact, with job control, the login shell could spawn a number of interactive shells as background jobs that you could switch to with fg %1, fg %2 etc., but to get back to the login shell, you would need to use suspend unless you wanted to manually kill -s STOP $$.

Also note that Ctrl+Z at the prompt in an interactive shell won't suspend it.

EDIT: I initially had a long hypothetical section about the use of suspend in a script, but since the command requires job control and since non-interactive shells usually don't have job control, I deleted that section.

Deleted section with suspend replaced by kill -s STOP $$ (this really doesn't belong to the answer any more, but it may be interesting to others anyway):

Let's say you have a background process (a script) in a script, and that this background process at some stage needs to stop and wait for the parent process to tell it to go on. This could be so that the parent has time to extract and move files into place or something like that.

The child script would suspend (kill -s STOP $$), and the parent script would send a CONT signal to it when it was okay to continue.

It gives you the opportunity to implement a sort of synchronisation between a parent process and a child process (although very basic as the parent shell process more or less needs to guess that the child process is suspended, although this can be fixed by having the child trap CONT and not suspend if that signal is received too early).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.