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.

I want to call some application from script. I use an application which doesn't allow any parameters in CLI but I need to test it in two different monitors and screen resolutions so I have two different configuration files. To call needed version I use simple script (name it notebook.sh):

#!/bin/sh

rm /home/user/.app/config.ini
cp /home/user/.app/config.ini.1366x768 /home/user/.app/config.ini
sleep 1
/usr/local/bin/app &

or second version with cp line changed.

OK, it's simple. But when I quit this app (Ctrl+Q built-in function) it still runs. Calling ps command I see app and notebook.sh in processes list.

My question is: how should notebook.sh script be written to terminate/kill app and script totally? Something like this piece of code:

if [ "$1" = "start" ]; then /usr/bin/mplayer -fs -osdlevel 0 $2 ; fi
if [ "$1" = "stop" ]; then killall mplayer ; fi
share|improve this question

1 Answer 1

I offer you a simpler but uglier solution. :-)
You can kill a process after an amount of time with a package called timelimit:

sudo apt-get install timelimit

(on a terminal)

The -t option of timelimit is the number of seconds a process will stay alive: after that it will be killed (brutally) with the SIGKILL signal (if you did not specify other signals with the -S option).

timelimit OPTIONS command arguments

Edit: IF you want to use a "professional" solution, use trap:

trap 'kill $(jobs -p)' EXIT

It will execute the command in the commas after the "EXIT" signal.
To test it:trap "yes test" EXIT ... then use the exit command. Bye.

share|improve this answer
    
Thank you. 'Trap' command was exactly what I need but 'timelimit' is also interesting for other things. –  stalker Oct 7 '13 at 21:17
    
I'm happy for you! ---> other questions are always welcome. ;-) –  Lorenzo Ancora Oct 8 '13 at 15:45

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.