supervision - discussion about system services, daemon supervision, init, runlevel management, and tools such as s6 and runit
 help / color / mirror / Atom feed
* timeout chpst -L
@ 2006-02-13 16:30 Alex Efros
  2006-02-13 20:18 ` Alex Efros
  0 siblings, 1 reply; 12+ messages in thread
From: Alex Efros @ 2006-02-13 16:30 UTC (permalink / raw)


Hi!

Is there exists some standard utility to run other process with timeout?
ulimit isn't suitable because it can only limit used CPU time, while I'm
speaking about real time.

My goal is run `chpst -L` but don't allow it to hang forever.

Of course there simple custom solution:
    chpst -L lock-file \
	perl -e 'alarm(shift);exec(@ARGV)' $timeout \
	$prog_to_execute
but I suppose there already exists such simple utility and also running perl
just to set alarm sounds little overkill.

Background:
    I've a couple of scripts which run by fluxbox when I press Alt-Fx or
    menu/winkey to switch between virtual desktops like it work in console.
    These scripts:
    - switch virtual desktop
    - store ID of previous virtual desktop into file (this allow me
      to switch between last two virtual desktops by pressing a menu key)
    - show ID of current virtual desktop using OSD
    The problem is what I sometimes switch too faster, these scripts execute
    in parallel, switching to different virtual desktops, and file with ID
    of previous desktop become corrupt (contain ID of current desktop instead).
    To fix this I wanna run my script using `chpst -L`, but I don't wanna
    fall into situation when I will not be able to switch virtual desktops
    because my script will hang (never happens before, but everything happens
    sometimes, especially bugs :)) and no new scripts will run because they
    will wait for lock forever.

-- 
			WBR, Alex.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-13 16:30 timeout chpst -L Alex Efros
@ 2006-02-13 20:18 ` Alex Efros
  2006-02-14  8:45   ` Gerrit Pape
  0 siblings, 1 reply; 12+ messages in thread
From: Alex Efros @ 2006-02-13 20:18 UTC (permalink / raw)


Hi!

On Mon, Feb 13, 2006 at 06:30:54PM +0200, Alex Efros wrote:
> Is there exists some standard utility to run other process with timeout?
> ulimit isn't suitable because it can only limit used CPU time, while I'm
> speaking about real time.

I've just discovered `tryto`. Sadly, but looks like there some race
condition bug in it when tryto used to run some process which exit
very quickly. I've tried to run `tryto chpst -l`, noticed strange
results and moved to more simple examples like `tryto true` (see
details below).

> My goal is run `chpst -L` but don't allow it to hang forever.

Of course, I mean -l option here, not -L.


Another fun thing is timelimit(8) mentioned on tryto(1). Google doesn't
know what is 'timelimit(8)' or know but refrain from saying to me. :-)



powerman@home ~/tmp $ for i in $(seq 1 10); do echo "i=$i"; tryto -v -t 2 bash -c 'true'; done
i=1
tryto: warning: child "bash" timed out. sending TERM...
tryto: warning: child "bash" not terminated. sending KILL...
tryto: fatal: child timed out, giving up.
i=2
tryto: warning: child "bash" timed out. sending TERM...
tryto: warning: child "bash" not terminated. sending KILL...
tryto: fatal: child timed out, giving up.
i=3
tryto: warning: child "bash" timed out. sending TERM...
tryto: warning: child "bash" not terminated. sending KILL...
tryto: fatal: child timed out, giving up.
i=4
tryto: warning: child "bash" timed out. sending TERM...
tryto: warning: child "bash" not terminated. sending KILL...
tryto: fatal: child timed out, giving up.
i=5
tryto: warning: child "bash" timed out. sending TERM...
tryto: warning: child "bash" not terminated. sending KILL...
tryto: fatal: child timed out, giving up.
i=6
i=7
i=8
i=9
tryto: warning: child "bash" timed out. sending TERM...
tryto: warning: child "bash" not terminated. sending KILL...
tryto: fatal: child timed out, giving up.
i=10
tryto: warning: child "bash" timed out. sending TERM...
tryto: warning: child "bash" not terminated. sending KILL...
tryto: fatal: child timed out, giving up.

-- 
			WBR, Alex.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-13 20:18 ` Alex Efros
@ 2006-02-14  8:45   ` Gerrit Pape
  2006-02-14 10:23     ` Alex Efros
  0 siblings, 1 reply; 12+ messages in thread
From: Gerrit Pape @ 2006-02-14  8:45 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 935 bytes --]

On Mon, Feb 13, 2006 at 10:18:07PM +0200, Alex Efros wrote:
> On Mon, Feb 13, 2006 at 06:30:54PM +0200, Alex Efros wrote:
> > Is there exists some standard utility to run other process with timeout?
> > ulimit isn't suitable because it can only limit used CPU time, while I'm
> > speaking about real time.
> 
> I've just discovered `tryto`. Sadly, but looks like there some race
> condition bug in it when tryto used to run some process which exit
> very quickly. I've tried to run `tryto chpst -l`, noticed strange
> results and moved to more simple examples like `tryto true` (see
> details below).

> powerman@home ~/tmp $ for i in $(seq 1 10); do echo "i=$i"; tryto -v -t 2 bash -c 'true'; done
> i=1
> tryto: warning: child "bash" timed out. sending TERM...
> tryto: warning: child "bash" not terminated. sending KILL...
> tryto: fatal: child timed out, giving up.

Yes, it's a race, the patch below should help.  Thanks, Gerrit.

[-- Attachment #2: diff --]
[-- Type: text/plain, Size: 433 bytes --]

Index: src/tryto.c
===================================================================
RCS file: /var/lib/cvs/socklog/src/tryto.c,v
retrieving revision 1.8
diff -u -r1.8 tryto.c
--- src/tryto.c	6 Feb 2005 11:36:50 -0000	1.8
+++ src/tryto.c	13 Feb 2006 20:42:13 -0000
@@ -104,6 +104,7 @@
   taia_now(&now);
   taia_uint(&deadline, timeout);
   taia_add(&deadline, &now, &deadline);
+  timeout =0;
 
   for (;;) {
     int iopausefds;

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14  8:45   ` Gerrit Pape
@ 2006-02-14 10:23     ` Alex Efros
  2006-02-14 10:36       ` Gerrit Pape
  0 siblings, 1 reply; 12+ messages in thread
From: Alex Efros @ 2006-02-14 10:23 UTC (permalink / raw)


Hi!

On Tue, Feb 14, 2006 at 08:45:09AM +0000, Gerrit Pape wrote:
> Yes, it's a race, the patch below should help.  Thanks, Gerrit.

Thanks, patch works. But I've found another bug, sorry. Now '-n' option
don't work:


$ tryto -t 2 -n 2 bash -c 'echo trying...; true'; echo done
trying...
done
$ tryto -t 2 -n 2 bash -c 'echo trying...; false'; echo done
trying...
trying...
trying...
trying...
trying...
trying...
trying...
trying...
trying...
trying...
[endless loop until Ctrl-C]

-- 
			WBR, Alex.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 10:23     ` Alex Efros
@ 2006-02-14 10:36       ` Gerrit Pape
  2006-02-14 11:01         ` Alex Efros
  0 siblings, 1 reply; 12+ messages in thread
From: Gerrit Pape @ 2006-02-14 10:36 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 386 bytes --]

On Tue, Feb 14, 2006 at 12:23:23PM +0200, Alex Efros wrote:
> On Tue, Feb 14, 2006 at 08:45:09AM +0000, Gerrit Pape wrote:
> > Yes, it's a race, the patch below should help.  Thanks, Gerrit.
> 
> Thanks, patch works. But I've found another bug, sorry. Now '-n' option
> don't work:

Yes, it's the same race, different impact.  Can you try this slightly
extended patch?

Thanks, Gerrit.

[-- Attachment #2: diff --]
[-- Type: text/plain, Size: 959 bytes --]

Index: src/tryto.c
===================================================================
RCS file: /var/lib/cvs/socklog/src/tryto.c,v
retrieving revision 1.8
diff -u -r1.8 tryto.c
--- src/tryto.c	6 Feb 2005 11:36:50 -0000	1.8
+++ src/tryto.c	14 Feb 2006 10:33:59 -0000
@@ -104,6 +104,7 @@
   taia_now(&now);
   taia_uint(&deadline, timeout);
   taia_add(&deadline, &now, &deadline);
+  timeout =0;
 
   for (;;) {
     int iopausefds;
@@ -163,15 +164,15 @@
       int i;
       char *s;
 
-      if (wait_nohang(&rc) == pid) break;
-      taia_now(&now);
-      if ((timeout =taia_less(&deadline, &now))) break;
-
       sig_unblock(sig_child);
       iopause(x, iopausefds, &deadline, &now);
       sig_block(sig_child);
       
       while (read(selfpipe[0], &ch, 1) == 1) {}
+
+      if (wait_nohang(&rc) == pid) break;
+      taia_now(&now);
+      if ((timeout =taia_less(&deadline, &now))) break;
 
       r = buffer_feed(&buffer_x);
       if (r < 0) {

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 10:36       ` Gerrit Pape
@ 2006-02-14 11:01         ` Alex Efros
  2006-02-14 11:21           ` Gerrit Pape
  0 siblings, 1 reply; 12+ messages in thread
From: Alex Efros @ 2006-02-14 11:01 UTC (permalink / raw)


Hi!

On Tue, Feb 14, 2006 at 10:36:25AM +0000, Gerrit Pape wrote:
> Yes, it's the same race, different impact.  Can you try this slightly
> extended patch?

Yep, now looks like it working.

One more question. If prog exit with non-zero return code, it will be
executed again (until number of tries reached). But if timeout happens
and it will be killed by TERM or KILL - it will not be restarted. Why?

-- 
			WBR, Alex.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 11:01         ` Alex Efros
@ 2006-02-14 11:21           ` Gerrit Pape
  2006-02-14 11:30             ` Alex Efros
  0 siblings, 1 reply; 12+ messages in thread
From: Gerrit Pape @ 2006-02-14 11:21 UTC (permalink / raw)


On Tue, Feb 14, 2006 at 01:01:59PM +0200, Alex Efros wrote:
> One more question. If prog exit with non-zero return code, it will be
> executed again (until number of tries reached). But if timeout happens
> and it will be killed by TERM or KILL - it will not be restarted. Why?

The timeout applies to tryto, not to the prog it runs.  When the timeout
is reached, tryto cleans up and exits as soon as possible, regardless
how often prog has been tried.

Regards, Gerrit.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 11:21           ` Gerrit Pape
@ 2006-02-14 11:30             ` Alex Efros
  2006-02-14 11:36               ` Gerrit Pape
  0 siblings, 1 reply; 12+ messages in thread
From: Alex Efros @ 2006-02-14 11:30 UTC (permalink / raw)


Hi!

On Tue, Feb 14, 2006 at 11:21:06AM +0000, Gerrit Pape wrote:
> The timeout applies to tryto, not to the prog it runs.  When the timeout
> is reached, tryto cleans up and exits as soon as possible, regardless
> how often prog has been tried.

Really? Then why below example don't exit after 3 seconds?


$ time tryto -t 3 -n 5 bash -c 'echo trying...; sleep 2; false'
trying...
trying...
trying...
trying...
trying...
tryto: fatal: child crashed, giving up.

real    0m14.039s
user    0m0.000s
sys     0m0.004s

-- 
			WBR, Alex.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 11:30             ` Alex Efros
@ 2006-02-14 11:36               ` Gerrit Pape
  2006-02-14 11:54                 ` Alex Efros
  0 siblings, 1 reply; 12+ messages in thread
From: Gerrit Pape @ 2006-02-14 11:36 UTC (permalink / raw)


On Tue, Feb 14, 2006 at 01:30:09PM +0200, Alex Efros wrote:
> On Tue, Feb 14, 2006 at 11:21:06AM +0000, Gerrit Pape wrote:
> > The timeout applies to tryto, not to the prog it runs.  When the timeout
> > is reached, tryto cleans up and exits as soon as possible, regardless
> > how often prog has been tried.
> 
> Really? Then why below example don't exit after 3 seconds?

It's a bug, at the same place in the source as the other two problems
you discovered.  After looking at the recent patch I sent, do you have
an idea how to fix it ;-)?

Thanks, Gerrit.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 11:36               ` Gerrit Pape
@ 2006-02-14 11:54                 ` Alex Efros
  2006-02-14 12:39                   ` Gerrit Pape
  0 siblings, 1 reply; 12+ messages in thread
From: Alex Efros @ 2006-02-14 11:54 UTC (permalink / raw)


Hi!

On Tue, Feb 14, 2006 at 11:36:23AM +0000, Gerrit Pape wrote:
> It's a bug, at the same place in the source as the other two problems
> you discovered.  After looking at the recent patch I sent, do you have
> an idea how to fix it ;-)?

Not fair! I don't know C, I'm a Perl programmer... but I'm also a
sysadmin, so I've spend a lot of time patching other's C, C++, etc.
programs without knowing these languages.

Moving 'while' 3 lines below does the trick. Correct? :-) Ohh, it may be
not correct and even completely wrong, but it's working - usual result when
sysadmin make a patch instead of programmer... :-)

-- 
			WBR, Alex.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 11:54                 ` Alex Efros
@ 2006-02-14 12:39                   ` Gerrit Pape
  2006-02-14 12:53                     ` Alex Efros
  0 siblings, 1 reply; 12+ messages in thread
From: Gerrit Pape @ 2006-02-14 12:39 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 633 bytes --]

On Tue, Feb 14, 2006 at 01:54:05PM +0200, Alex Efros wrote:
> On Tue, Feb 14, 2006 at 11:36:23AM +0000, Gerrit Pape wrote:
> > It's a bug, at the same place in the source as the other two problems
> > you discovered.  After looking at the recent patch I sent, do you have
> > an idea how to fix it ;-)?
> 
> Not fair! I don't know C, I'm a Perl programmer... but I'm also a
> sysadmin, so I've spend a lot of time patching other's C, C++, etc.
> programs without knowing these languages.

Fair enough ;-).  I suggest the patch below against tryto from socklog
2.0.2.  Can you please try it out?

Thanks for your bug reports, Gerrit.

[-- Attachment #2: diff --]
[-- Type: text/plain, Size: 1254 bytes --]

Index: src/tryto.c
===================================================================
RCS file: /var/lib/cvs/socklog/src/tryto.c,v
retrieving revision 1.8
diff -u -r1.8 tryto.c
--- src/tryto.c	6 Feb 2005 11:36:50 -0000	1.8
+++ src/tryto.c	14 Feb 2006 12:34:04 -0000
@@ -104,6 +104,7 @@
   taia_now(&now);
   taia_uint(&deadline, timeout);
   taia_add(&deadline, &now, &deadline);
+  timeout =0;
 
   for (;;) {
     int iopausefds;
@@ -163,16 +164,16 @@
       int i;
       char *s;
 
-      if (wait_nohang(&rc) == pid) break;
-      taia_now(&now);
-      if ((timeout =taia_less(&deadline, &now))) break;
-
       sig_unblock(sig_child);
       iopause(x, iopausefds, &deadline, &now);
       sig_block(sig_child);
       
       while (read(selfpipe[0], &ch, 1) == 1) {}
 
+      taia_now(&now);
+      if ((timeout =taia_less(&deadline, &now))) break;
+      if (wait_nohang(&rc) == pid) break;
+
       r = buffer_feed(&buffer_x);
       if (r < 0) {
 	if ((errno == error_intr) || (errno == error_again)) continue;
@@ -201,6 +202,7 @@
     close(cpipe[1]);
 
     if (timeout) {
+      if (wait_nohang(&rc) == pid) break;
       /* child not finished */
       strerr_warn4(WARNING,
 		   "child \"", *argv, "\" timed out. sending TERM...", 0);

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: timeout chpst -L
  2006-02-14 12:39                   ` Gerrit Pape
@ 2006-02-14 12:53                     ` Alex Efros
  0 siblings, 0 replies; 12+ messages in thread
From: Alex Efros @ 2006-02-14 12:53 UTC (permalink / raw)


Hi!

On Tue, Feb 14, 2006 at 12:39:54PM +0000, Gerrit Pape wrote:
> Fair enough ;-).  I suggest the patch below against tryto from socklog
> 2.0.2.  Can you please try it out?

Yeah, it's work as good as my `incorrect sysadmin's way' patch. :-)

Thanks!

-- 
			WBR, Alex.


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2006-02-14 12:53 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-13 16:30 timeout chpst -L Alex Efros
2006-02-13 20:18 ` Alex Efros
2006-02-14  8:45   ` Gerrit Pape
2006-02-14 10:23     ` Alex Efros
2006-02-14 10:36       ` Gerrit Pape
2006-02-14 11:01         ` Alex Efros
2006-02-14 11:21           ` Gerrit Pape
2006-02-14 11:30             ` Alex Efros
2006-02-14 11:36               ` Gerrit Pape
2006-02-14 11:54                 ` Alex Efros
2006-02-14 12:39                   ` Gerrit Pape
2006-02-14 12:53                     ` Alex Efros

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).