6

I typically have on the order of a dozen zsh processes running. When I edit my config files, I'd like a clean way to get them all to re-initialize. Ideally, this would not mean completely killing them all and restarting as that loses my working directory, any shell variables I had set locally, temporary aliases, etc. In a given shell, I can exec "${SHELL}" and that works fine, but I want a way to force all active zsh instances under my login to do that.

3

You can define a trap function:

TRAPUSR1() {
  if [[ -o INTERACTIVE ]]; then
     {echo; echo execute a new shell instance } 1>&2
     exec "${SHELL}"
  fi
}

This funtion is called when the running shell catches a USR1 signal, initiated by kill -USR1 <PID>. It checks if the running zsh instance is interactive and if so, replaces it with a new one.+

So, to update all your running zsh interactive session in one run, simply use

killall -USR1 zsh

But please be aware that if you have running zsh instances without the TRAPUSR1() function defined, these will exit upon USR1! That's why you should define the trap in /etc/zshenv, as this is the only file which is read by every zsh instance,+ including scripts and sessions started with zsh -f.


+ Credits go to @Adaephon, who pointed this out in a comment.

  • 1
    There is one problem with that solution: When zsh is used to run a script, it does not load from ~/.zshrc. Scripts therefore will not have a trap function for USR1. As the default behaviour is to terminate on USR1, any script running with zsh will be terminated when running killall -USR1 zsh. The only files read by every zsh instance are /etc/zshenv and ~/.zshenv. So you would have to define the trap there. Also you would need to extend it with a check to see whether the shell is interactive and only then run exec "${SHELL}". – Adaephon Dec 16 '14 at 7:23
  • 1
    @Adaephon: Thanks, two valid points, I included them in my answer. Only some nitpicking here: "The only files read by every zsh instance are /etc/zshenv and ~/.zshenv". This is true only for /etc/zshenv, sourcing of ~/.zshenv depends also on RCS and PRIVILEGED options. – mpy Dec 16 '14 at 20:03
  • On multi-user machines (or if you have system scripts using zsh) you may want to include -u $(whoami) in your killall command. Otherwise you will get Operation not permitted errors for other users' zsh instances. – Sudo Bash Apr 15 at 20:45

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.