Is is possible to get the current context, or front-most user-facing process, to trigger a script when it changes?

For instance, the user

  • starts with the command prompt,
  • then goes into Emacs
  • quits back into the command line
  • goes into top
  • quits
  • etc.

For each of these transitions, can I make the terminal run a script automatically which gets the name of the new process, and potential performs an action according to the process?


You can use rcaloras' bash-preexec script to run pre-execute commands whenever the user runs something from bash. If the user enters any other shell/application, your pre-exec script will be run first, and can run its own handlers. To tell when the user exits that program, one potential solution would be to have your script continue polling in the background to see if the pid still exists.

Relevant SuperUser Answer

| improve this answer | |

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.