This is an oft-asked question:

This time, I'm adding a twist. I need to do this in zsh. Additionally, the solution:

  • Must be silent (i.e. no job control messages)
  • Must be portable (i.e. as close to pure zsh as possible. Assume no coreutils)
  • Should be correct (see notes)
  • Should behave like GNU timeout

On correctness:

Starting a process, waiting for a while, and killing its PID is wrong. It's wrong because it's possible for the process we're timing to die and a new process to start with the same PID later if the timeout is log, or the system is low on PIDs. In practice, this rarely happens, but it would be nice if there was a cleaner way to track when the process has terminated and only kill it if it has not.


1 Answer 1


First what comes to my mind is to get PID after process creation, use zstat tool (see man zshmodules) to get get process timestamp, e.g. zstat '+mtime' /proc/PID, and after desired amount of time check if timestamp for given /proc/PID has no changed - if yes kill the job.

  • Thanks this is a great idea. I'm having a bit of trouble tying it all together. In particular, I'd like to preserve the stream behavior of STDOUT, like GNU timeout does, and I'm having a hard time with that.
    – PythonNut
    Commented Aug 27, 2015 at 3:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .