Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash script with doing lot of things called script.sh:

#!/bin/bash
#It
#Is
#Doing
#Things

Is there a way that I be able to get the proccess of script and then kill it after 5 minutes?

Like:

#!/bin/bash
#Now here give pid of script.sh then kill it after 5 minutes
#It
#Is
#Doing
#Things

update

Imagin I have this script:

#!/bin/bash
while :
do
      echo "line printed"
done

If we use these answers it doesn't show the output...

I want out put be printed and after 5 minutes cntrl + c signal kill the process of bash...

Is this good idea?

PID=$$
kill -SIGINT $PID

Where should be put?

share|improve this question
3  
mywiki.wooledge.org/XyProblem ;) –  n.st 14 hours ago
add comment

3 Answers

You get the PID with PID=$$.

What you want may be most easily achieved with the command timeout.

But you can run a background process, too:

(sleep $TIMEOUT && kill "$PID") &
share|improve this answer
    
Pardon but there is a problem..while my script is running it shows me output..but in your case this is not do that..Imagin you have infinitive while loop that echo sth...when I do what you said it doesn't show me output and my machine hangs... –  MortezaLSC 11 hours ago
    
@MortezaLSC I have given you two suggestions. What exactly have you done? –  Hauke Laging 8 hours ago
    
see Gnouc's answer, This is my script and please see my update as well..Thank you very much –  MortezaLSC 8 hours ago
    
You see...It brings my script to background...while my script is running it shows sth in terminal runtime.. –  MortezaLSC 8 hours ago
1  
I think you still need to add & at the end to actually send it to the background. The brackets only start a subshell. –  Darkhogg 5 hours ago
add comment

The timeout utility that is a part of GNU coreutils does it for you:

timeout 5m bash script.sh

would terminate the script after 5 minutes of execution.

share|improve this answer
    
Pardon but there is a problem..while my script is running it shows me output..but in your case this is not do that..Imagin you have infinitive while loop that echo sth...when I do what you said it doesn't show me output and my machine hangs... –  MortezaLSC 11 hours ago
add comment

You can use a check condition:

#!/bin/bash

START=$(date +%s)

while [[ $(($(date +%s) - $START)) -ne 300 ]]
do
    #do something here
done

echo QUIT

Explanation

  • date +%s get the time in seconds since epoch, we save it to START variable, mark start time of script.
  • [[ $(($(date +%s) - $START)) -ne 300 ]]: we get current time (date +%s again) subtract to start time (which is saved in START variable).
  • If the result is not equal 300 (5 minutes), script continue running,
  • If the result is equal 300, meaning script has run 5 minutes since start time, we quit the while loop, script ends.
share|improve this answer
    
Thank you..My script has infinitive loop...if I put my whole script into your while loop do you think it works? Thank you –  MortezaLSC 8 hours ago
    
I think you must use my while loop instead of your infinitive loop to make it works. Can you give more details about your script? –  Gnouc 8 hours ago
    
+1 This is my script –  MortezaLSC 8 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.