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

Re: Idle function?

On Mar 29,  1:37pm, Bek Oberin wrote:
} Subject: Idle function?
} Is it possible for a console to auto-execute a command

What is "a console"?

} when it has been idle for x number of minutes?

Like Sweth, I remember you asking a similar question some while ago.
It was the reverse last time, though, wasn't it?  How to do something
when the tty had NOT been idle for some length of time?

Zsh has the $TTYIDLE variable and $TMOUT ... I personally have this in
my init files:

 # Update title bar periodically if it has been idle a long time (10 min).
 # Otherwise let precmd() do it, so we don't cause unwanted scroll-to-bot.
 precmd() { TMOUT=600 ; title }
 TRAPALRM() { TMOUT=60 ; title }

The `title` function puts the current time and current directory on the
screen somewhere -- in the status bar if the terminal has one, or in the
title bar of an xterm (whence the name), or wherever.  Normally this is
updated every time a prompt is printed, in precmd(), which is often more
than once a minute.

If I'm away from my terminal for 10 minutes, however, TMOUT kicks in and
the title starts updating every 60 seconds from TRAPALRM.  As soon as I
get back and start running commands again, the delay goes back to 10

You'll probably find almost exactly that description in the archives a
few previous times, if you look.

If you want something that works even when a command (say, an editor) is
in the foreground, then you have to do something like background a loop
that alternately sleeps for a while and checks $TTYIDLE.  Unfortunately,
you can't do this with a subshell because TTYIDLE is always -1 in a job
backgrounded that way; so something like:

   zsh -fc "sleep 10; ((TTYIDLE > x * 60)) && echo Do Something 1>&2"

That still doesn't get the "topmost" zsh (assuming that's what you mean
by "a console") to do anything; it makes itself pretty unreachable when
a child process is in the foreground.  So you might be just as well off
with the TMOUT hack.

Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

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