5

Is there a way to run a command for a specific amount of time and then kill it?

Example 1 :

I would like to play a song in cvlc for 1 minute and then stop. (song is longer than 1 min).

Example 2 :

I would like to run the command cmatrix for 30 seconds and then stop it.

1
  • timeout 900s APPNAME (replace 900 with the amount of seconds you want the app to run for. Oct 20, 2016 at 17:11

1 Answer 1

7

The timeout command is built for precisely this. To run your cvlc command for 1 minute:

timeout 1m cvlc-command

The time period can use the strings d for days, h for hours, m for minutes and s for seconds. If no time measurement letter is given, the default is seconds.

A useful option is --preserve-status which will make the timeout command exit with the controlled command's status code, even if it is killed. If this option isn't used, the status code will be that of the controlled command if no timeout occurred, or 124 if the command timed out.

Another useful option if you need to be very sure the service has stopped is to use --kill-after=<time> which will send another kill signal to the command if the command has not stopped by the new limit set by time

Not the answer you're looking for? Browse other questions tagged .