This is an oft-asked question:
- Super User: Loop: Start program and kill after time out
- SO: How to kill a child process after a given timeout in Bash?
- SO: How to run a process with a timeout in Bash?
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 nocoreutils
) - 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.