Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

If and How is it possible to, in Terminal, to get an application to run for 30 seconds, before being force closed, and echoing the logs of that process?

share|improve this question
    
@JacobVlijm It would ideally run as a shell script due to the portability of the language, mainly as the intended area of usage is a CI service. –  ir-g Apr 27 at 14:18
1  
@ir-g you can use the timeout command. e.g. timeout 30s command –  mcantsin Apr 27 at 14:21
    
;) @ir-g you're welcome! –  mcantsin Apr 27 at 14:28

2 Answers 2

up vote 16 down vote accepted

You might want to use the timeout command.

timeout -k 10s 30s command

which will run the command for 30s and kill it after 10s if still running. - Check the manpage for more options.

share|improve this answer
1  
Thanks very much for this, super easy to use. –  ir-g Apr 27 at 14:43
1  
@ir-g you're welcome. You might also want to check out the watch command, which is similar and also very easy to use in the command line. –  mcantsin Apr 27 at 14:47
2  
Thanks for that pointer, watch strikes me as a very interesting command. Could come in handy... –  ir-g Apr 27 at 15:03

Here are two ways (but the timeout command suggested by mcantsin is probably better):

  1. Launch the command in the background, that way its PID is saved in $! and you can use that to kill it after the specified time:

    command &
    sleep 30 && kill $!
    
  2. Launch the command and use pkill or killall to kill it. CAUTION: This assumes that only one command with that name is running, I am using firefox as an example:

    firefox &
    sleep 30 && pkill firefox
    

I have no idea what you mean by "the logs of that process" but a process's standard error can be saved to a file with command 2> logfile.txt.

share|improve this answer

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.