Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: timeout problem in ssh sessions

On Feb 19, 11:20am, Andy Spiegl wrote:
} > schaefer<506> ps="$(ps -eo pid,ppid,cmd)"
} > schaefer<507> print -l "PID: $$" "PPID: $PPID" ${(M)${(f)ps}:#*ps -eo*}
} > PID: 16182
} > PPID: 7139
} > 16255 16254 ps -eo pid,ppid,cmd
} Strange, here it's different:
}  condor:~>ps="$(ps -eo pid,ppid,cmd)"
}  condor:~>print -l "PID: $$" "PPID: $PPID" ${(M)${(f)ps}:#*ps -eo*}
}  PID: 23435
}  PPID: 23388
}  30571 23435 ps -eo pid,ppid,cmd

Hmm ... aha.

 * Do we need to fork?  We need to fork if:
 * 1) The command is supposed to run in the background. (or)
 * 2) There is no `exec' flag, and either:
 *    a) This is a builtin or shell function with output piped somewhere.
 *    b) This is an external command and we can't do a `fake exec'.
 * A `fake exec' is possible if we have all the following conditions:
 * 1) last1 flag is 1.  This indicates that the current shell will not
 *    be needed after the current command.  This is typically the case
 *    when when the command is the last stage in a subshell, or is the
 *    last command after the option `-c'.
 * 2) We don't have any traps set.
 * 3) We don't have any files to delete.

I have a TRAPALRM function, so condition (2) for "fake exec" fails and the
$(...) subshell forks before running "ps".

Zsh is pretty darned clever about this, as even the following doesn't
manage to force an additional fork:

ps="$(<=(ps -eo pid,ppid,cmd))"

However, this does the trick:

ps="$(<<(ps -eo pid,ppid,cmd))"

I'm not sure whether to assert that's "better" than:

ps="$(fgrep -v "pid,ppid" =(ps -eo pid,ppid,cmd))"

In my case, with a trap set, the fgrep formulation results in two extra
forks (one in each subshell) so I suspect $(<<(...)) is preferable.

Messages sorted by: Reverse Date, Date, Thread, Author