1

I would like to run a never-ending command (such as play a mp3 file in loop) for a period of length T.

I put the command in a shell script my.sh:

#! /bin/bash
vlc /path/to/my.mp3 # will play the file in loop till I terminate it.

and then I try to run it

my.sh & 
pid="$!" # need to get the pid of the vlc process.
sleep 2.5h
kill $pid 

It seems that it kills only the process running the shell script, not the process running the command in the script. How can I kill the process running the command in the script?

Thanks.

Tim
  • 101,790

2 Answers2

2

The command timeout does that. For example:

timeout 10m vlc /path/to/my.mp3

will kill it after 10 minutes. See the man page for more options (for example -k to send a kill signal to make sure the program does no longer run).

0

Love timeout

An alternative solution, close to the original post:

my.sh:

#!/bin/bash
vlc /path/to/my.mp3 & # ADDED & <- will play the file in loop till I terminate it.
pid=$!
sleep 10
kill $pid

chmod:

chmod +x my.sh

Run it:

./my.sh &