Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save myandere/e75b26220736dbb6fb6099a9ec9e40e7 to your computer and use it in GitHub Desktop.
Save myandere/e75b26220736dbb6fb6099a9ec9e40e7 to your computer and use it in GitHub Desktop.

Revisions

  1. @CMCDragonkai CMCDragonkai revised this gist Apr 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion job_control_zsh_bash.md
    Original file line number Diff line number Diff line change
    @@ -79,7 +79,7 @@ functions should be pure.
    To send a signal to all jobs, or kill all of them, use this function:

    ```sh
    :'
    : '
    killjobs - Run kill on all jobs in a Bash or ZSH shell, allowing one to optionally pass in kill parameters
    Usage: killjobs [zsh-kill-options | bash-kill-options]
  2. @CMCDragonkai CMCDragonkai revised this gist Apr 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion job_control_zsh_bash.md
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ using the keyboard:

    It will return us to the shell. At this point can do a few things (note that `kill`
    is the shell built in `kill` not the unix `kill`, which can be accessed via `command kill`
    :
    ):

    ```
    # foreground the job 1 (return it to the foreground)
  3. @CMCDragonkai CMCDragonkai revised this gist Apr 1, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion job_control_zsh_bash.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,8 @@ using the keyboard:
    <kbd>Ctrl</kbd> + <kbd>Z</kbd>

    It will return us to the shell. At this point can do a few things (note that `kill`
    is the shell built in `kill` not the unix `kill`:
    is the shell built in `kill` not the unix `kill`, which can be accessed via `command kill`
    :

    ```
    # foreground the job 1 (return it to the foreground)
  4. @CMCDragonkai CMCDragonkai revised this gist Apr 1, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion job_control_zsh_bash.md
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,8 @@ using the keyboard:

    <kbd>Ctrl</kbd> + <kbd>Z</kbd>

    It will return us to the shell. At this point can do a few things:
    It will return us to the shell. At this point can do a few things (note that `kill`
    is the shell built in `kill` not the unix `kill`:

    ```
    # foreground the job 1 (return it to the foreground)
  5. @CMCDragonkai CMCDragonkai revised this gist Apr 1, 2016. 1 changed file with 26 additions and 18 deletions.
    44 changes: 26 additions & 18 deletions job_control_zsh_bash.md
    Original file line number Diff line number Diff line change
    @@ -31,23 +31,6 @@ kill %3
    kill -TSTP %4
    # continue the job 5 (equal to bg %5)
    kill -CONT %5
    # terminate all jobs in bash (should give a list of process group ids to kill)
    kill $(jobs -p)
    # terminate all jobs in zsh (its complicated)
    killjobs () {
    local kill_list="$(jobs)"
    if [ -n "$kill_list" ]; then
    # this runs the ZSH builtin kill, not unix kill, otherwise jobspecs cannot be killed
    # the $@ list must not be quoted to allow one to pass any number parameters into the kill
    # the kill list must not be quoted to allow the shell builtin kill to recognise them as jobspec parameters
    kill $@ $(sed --regexp-extended --quiet 's/\[([[:digit:]]+)\].*/%\1/gp' <<< "$kill_list" | tr '\n' ' ')
    else
    return 0
    fi
    }
    killjobs
    # show all running jobs
    jobs -r
    # show all stopped jobs
    @@ -89,4 +72,29 @@ the job number.
    In ZSH, if you use try to suspend a function operation, it will fork the shell.
    That function will now be in a totally different process, so any mutation side
    effects will not be reflected in your shell. This is good of course, because
    functions should be pure.
    functions should be pure.

    To send a signal to all jobs, or kill all of them, use this function:

    ```sh
    :'
    killjobs - Run kill on all jobs in a Bash or ZSH shell, allowing one to optionally pass in kill parameters
    Usage: killjobs [zsh-kill-options | bash-kill-options]
    With no options, it sends `SIGTERM` to all jobs.
    '
    killjobs () {

    local kill_list="$(jobs)"
    if [ -n "$kill_list" ]; then
    # this runs the shell builtin kill, not unix kill, otherwise jobspecs cannot be killed
    # the `$@` list must not be quoted to allow one to pass any number parameters into the kill
    # the kill list must not be quoted to allow the shell builtin kill to recognise them as jobspec parameters
    kill $@ $(sed --regexp-extended --quiet 's/\[([[:digit:]]+)\].*/%\1/gp' <<< "$kill_list" | tr '\n' ' ')
    else
    return 0
    fi

    }
    ```
  6. @CMCDragonkai CMCDragonkai revised this gist Apr 1, 2016. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions job_control_zsh_bash.md
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,38 @@ kill %3
    kill -TSTP %4
    # continue the job 5 (equal to bg %5)
    kill -CONT %5
    # terminate all jobs in bash (should give a list of process group ids to kill)
    kill $(jobs -p)
    # terminate all jobs in zsh (its complicated)
    killjobs () {
    local kill_list="$(jobs)"
    if [ -n "$kill_list" ]; then
    # this runs the ZSH builtin kill, not unix kill, otherwise jobspecs cannot be killed
    # the $@ list must not be quoted to allow one to pass any number parameters into the kill
    # the kill list must not be quoted to allow the shell builtin kill to recognise them as jobspec parameters
    kill $@ $(sed --regexp-extended --quiet 's/\[([[:digit:]]+)\].*/%\1/gp' <<< "$kill_list" | tr '\n' ' ')
    else
    return 0
    fi
    }
    killjobs
    # show all running jobs
    jobs -r
    # show all stopped jobs
    jobs -s
    # show all jobs
    jobs
    # show all jobs with process ids
    jobs -l
    # show all jobs with process group ids
    jobs -p
    # only in ZSH, show all jobs with the directory the started in
    jobs -d
    # show status about 1 job
    jobs %6
    # the above flags can be combined to filter for specific jobs
    ```

    Or (using prefix search), which only works with `fg` or `bg`, but not `kill`:
  7. @CMCDragonkai CMCDragonkai created this gist Apr 1, 2016.
    60 changes: 60 additions & 0 deletions job_control_zsh_bash.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    Job Control in ZSH and Bash
    ===========================

    All processes in ZSH/Bash under job control are in 3 states: foregrounded,
    backgrounded and suspended.

    ```
    # run command in the foreground
    command
    # run commend in the background
    command &
    # show the list of jobs ([job-id] priority status command)
    jobs
    ```

    Once we are running the `command`, we can suspend by sending the `SIGTSTP` signal
    using the keyboard:

    <kbd>Ctrl</kbd> + <kbd>Z</kbd>

    It will return us to the shell. At this point can do a few things:

    ```
    # foreground the job 1 (return it to the foreground)
    fg %1
    # background the job 2 (make it continue in the background)
    bg %2
    # kill the job 3 (sends SIGTERM, not like Ctrl + C which sends SIGINT)
    kill %3
    # suspend the job 4 (equal to Ctrl + Z)
    kill -TSTP %4
    # continue the job 5 (equal to bg %5)
    kill -CONT %5
    ```

    Or (using prefix search), which only works with `fg` or `bg`, but not `kill`:

    ```
    fg %command
    bg %command
    ```

    There are some shortcuts like:

    ```
    # foreground the most recent backgrounded job
    %
    # same thing as above
    %+
    # foreground the second most recent backgrounded job
    %-
    ```

    But for best portability across commands, use the job numbers `%n` where `n` is
    the job number.

    In ZSH, if you use try to suspend a function operation, it will fork the shell.
    That function will now be in a totally different process, so any mutation side
    effects will not be reflected in your shell. This is good of course, because
    functions should be pure.