bash scripting try something, but quit if it's taking too long

Eric Battersby gyre-Ja3L+HSX0kI at public.gmane.org
Wed Jun 23 07:01:31 UTC 2010


On Tue, 22 Jun 2010, Matt Price wrote:

> On Tue, Jun 22, 2010 at 3:37 PM, Chris F.A. Johnson <chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org>
> wrote:
>       On Tue, 22 Jun 2010, Matt Price wrote:
>
>       > hi,
>       >
>       > quick scripting question.  I have an irritating emacs bug, in
>       which
>       > emacs sometimes hangs when the network's disrupted.  To deal
>       with this
>       > i have just send a command via emacsclient to the running
>       server
>       > before i suspend, which solves a solid 90% of my problems.
>        but
>       > sometimes emacs is already hung when i want to suspend!  in
>       that case,
>       > my script hangs too and the suspend event never takes place,
>       which is
>       > almost always worse since it causes everything to lose data,
>       not just
>       > emacs.  here's my tiny function:
>       >
>       > suspend_wl()
>       > {
>       >       # Get WL to go offline
>       >         if [-f /tmp/emacs1000/server]
>       >           then
>       >             /usr/bin/emacsclient --socket-name
>       /tmp/emacs1000/server
>       > --eval "(wl-toggle-plugged 'off)"
>       >       fi
>       > }
>       > is there a "try" or similar command i can use around the
>       emacsclient
>       > command, to just continue if things are taking too long?
>
> wait=666       ## seconds before timing out
> suspend_wl &   ## put function into the background
> sleep $wait    ## wait
> kill $!        ## kill last background process
>
> wouldn't that guarantee that you have to wait $wait seconds before suspend?
> hmm.. is there no way to, say, test once a second to see whether the job has
> finished?

You don't need to poll.
If you must do it in Shell, what about this?:

  trap "kill %1 %2 2>/dev/null" CHLD; suspend_wl &  sleep $wait

It will take the lesser of $wait or how long 'suspend_wl' runs.

## test timeout
[~]$ time -p (set -m; trap "kill %1 %2 2>/dev/null" CHLD; sleep 9 &  sleep 2)
real 2.00
...
## test normal completion
[~]$ time -p (set -m; trap "kill %1 %2 2>/dev/null" CHLD; sleep 2 &  sleep 9)
real 2.00
...

If your background task can spawn off other children, I would
recommend using Perl or C and creating a new process group for
the children.

--
Eric B.
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list