in reply to Ending system() calls after certain time period.

You can fork, set a time limit in the child with BSD::Resource, and exec the external program.

That does the time limit part, but I don't know how to notify other programs of a timeout - perhaps trapping some signals might help.

You can also look on cpan for ipc, run and system, maybe one of the great many modules does that job for you already.

  • Comment on Re: Ending system() calls after certain time period.

Replies are listed 'Best First'.
Re^2: Ending system() calls after certain time period.
by dwm042 (Priest) on Jun 23, 2008 at 18:31 UTC
    You can try using something like a named pipe.

    Edit: when I've seen named pipes used as a timing mechanism, it was when there was a process that could run to completion in X seconds or then be killed. You would set up two coprocesses in a shell, one monitoring the other. When time was up, one process would kill the other if it had not completed and sent the completion message through the pipe. I'm not sure if the questioner is wanting a run or die scenario.
Re^2: Ending system() calls after certain time period.
by massa (Hermit) on Jun 23, 2008 at 18:05 UTC
    BSD::Resource RLIMIT_CPU apparently does not work in linux:
    h@e12a8t:~$ perl -le 'use BSD::Resource; print for RLIMIT_CPU, q(--), +getrlimit(RLIMIT_CPU), q(--), getrusage(), q(--), setrlimit(RLIMIT_CP +U, 10, 20), qq(--), getrlimit(RLIMIT_CPU), q(--); $SIG{XCPU} = sub { +print "LIMIT" }; (sleep 1), printf "$_: %s %s\n", getrusage() for 1 . +. 100;' 0 -- -1 -1 -- 0.024001 0.008 0 0 0 0 685 0 0 0 0 0 0 0 1 0 -- 1 -- 10 20 -- 1: 0.024001 0.008 2: 0.024001 0.008 3: 0.024001 0.008 4: 0.024001 0.008 5: 0.024001 0.008 6: 0.024001 0.008 7: 0.024001 0.008 8: 0.024001 0.008 9: 0.024001 0.008 10: 0.024001 0.008 11: 0.024001 0.008
    goes all the way up to 100.
      A well, I guess it just limits CPU time and not run time - perhaps not what the OP wanted. Your script doesn't really use much CPU time (less than 0.03 on my machine), so it won't get killed.

      That's fine for my applications, but perhaps not so good in the general application.