0

I have a shell script thats running a docker container. I want the shell script to kill the container if it runs longer than x seconds.

How would I monitor the time in the shell script to kill it if the time the script has been running exceeds x seconds?

Nathan
  • 56
  • 7

1 Answers1

4

You could just use timeout.

timeout 10 ping 1.1.1.1 

It kills the process after 10s.

Tyfingr
  • 154
  • 1
  • 9
  • 1
    See the "Answer Well-Asked Questions" section in [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the bullet point regarding questions which "...have already been asked and answered many times before". – Charles Duffy Jul 19 '18 at 18:05
  • 1
    Also note that -- as described in [BashFAQ #68](https://mywiki.wooledge.org/BashFAQ/068) -- `timeout` is not built into bash, and thus not guaranteed to be available (or behave consistently -- there are at least four distinct known implementations) everywhere bash is installed. – Charles Duffy Jul 19 '18 at 18:08