How can I limit processes to less than 1s CPU time in Bash? I am using version 4.1.5(1) and if I type ulimit -t 0.5 command, I get:
-bash: ulimit: 0.5: invalid number
I am using ulimit -t 1 command as an imperfect substitute, but for technical reasons I don’t need to bother you with, I would very much like command to be stopped after it has had CPU for a half-second or maybe a bit less.
A Linux-specific answer would be fine.
As other people said, you can’t do it with the standard built-in Bash commands.
It is possible to create your own command to limit the elapsed time for a command (distinct from the actual CPU time used by the command; see also the comments from Rob, for which, thanks!). I’ve previously published on SO code to do timeouts after an integer number of elapsed seconds (so it is not directly an answer to your problem). However, you could (fairly) easily adapt the code to use sub-second resolution on the elapsed time and the POSIX facilities. The core interface is:
To handle your requirements, it would simply need to recognize sub-second times and call the appropriate functions; it affects the internals far more than the externals.
Theoretically (according to POSIX 2008), you should use the
timer_create(),timer_settime()andtimer_gettime()functions. These provide detailed control over which signal should be generated when the timer expires, etc. However, not all systems provide them (for example, MacOS X 10.6.7 does not document them). These use the headers<time.h>and<signal.h>, andstruct itimerspec(andstruct sigevent).However, in the short term, you may need to use the functions
getitimer()andsetitimer()instead, which POSIX 2008 marks as obsolescent (but which are available on MacOS X 10.6.7). These use the header<sys/time.h>andstruct itimerval.To be portable, you may need to autoconfigure your program to deal with both these; you might also need to deal with Windows (which will undoubtedly provide an alternative interface). You probably fall back on
alarm()and whole-second granularity timing if neither of the fine resolution signalling timer systems is available. Choose thetimer_*functions over the*itimerfunctions if both are present – or simply test fortimer_*first and use them if available.Be aware that if you set a timeout of 0.5 seconds with these functions, they guarantee a minimum elapsed time of 0.5 seconds – but it may be arbitrarily longer than the time because of scheduling issues. Note that there is a
timer_getoverrun()to help establish how long the overrun was.