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):


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

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.

  • Thank you. 'Trap' command was exactly what I need but 'timelimit' is also interesting for other things. – stalker Oct 7 '13 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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