Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Last active September 26, 2021 16:10
Show Gist options
  • Save SomajitDey/4e7fcc2b8a8e484fc0c39e977189760f to your computer and use it in GitHub Desktop.
Save SomajitDey/4e7fcc2b8a8e484fc0c39e977189760f to your computer and use it in GitHub Desktop.
Emulate job control signals from bash script
# First run the following 2 lines in your interactive bash attached to a controlling terminal
export bashpidfile="/tmp/bashpid" # File that stores the bash pid
export PROMPT_COMMAND='echo $$ > $bashpidfile' # Or, replace $$ with $BASHPID
# From another terminal do the following
### TPGID gives fg proc group on the tty the process is connected to, or -1 if the process is not connected to a tty
fgpg=$(ps --pid $(awk NR==1 $bashpidfile) -o tpgid=)
# Send signal SIG to all foreground processes to emulate user Ctrl- inputs
# Ctrl-C: INT, Ctrl-Z: TSTP, Ctrl-\: QUIT
pkill -SIG -g|--pgroup $fgpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment