Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In case i run a script in terminal and it contains a background job , in case the user close the terminal as i guess the script with his all back ground tasks will be dead , i want something like an event when the job is about to be killed or terminated , to excecute some block of code for instance :

while sleep 3h
backGround job commands 
those above is the back ground job 

i want when i already pressed exit on the terminal who run the script in background and when this job is about to exist and killed to do some other commands before get terminated the whole script something like EVENTS in C# for example when something happen a block of code get executed i hope i explained myself abundantly clear this time

Thanks in advance :)

share|improve this question
You need screen i guess. screen will keep the process in the background and it will not be terminat even if the terminal is closed by the user. – Achu May 2 '13 at 12:44
THanks sir , but this is not what i want – Ahmed Zain El Dein May 2 '13 at 13:35
up vote 1 down vote accepted

The following script will append Caught to the Test.Out file, whenever it receives SIGINT, SIGTERM, SIGHUP


trap "echo 'Caught' >> Test.Out" SIGINT SIGTERM SIGHUP
while :
   sleep 1000

I like SIGHUP the most. It will be sent to the shell script whenever the terminal is closed.

But we cannot escape the KILL signal. :(

EDIT: More Info on SIGHUP :

EDIT: Video with Test.Out file generation

share|improve this answer
i am reading to understan what SIGHUP means thanks for answer – Ahmed Zain El Dein May 2 '13 at 13:54
Please accept the answer, if it works for you :) – thefourtheye May 2 '13 at 14:18
in case it will for sure i will accept it :) – Ahmed Zain El Dein May 2 '13 at 14:51
look sir , it works but in case i open using Ctrl+T to open terminal on my own then i drag and drop the file of the bashscript to run it by pressing Enter the file is created on pressing close by mouse but in case i double click on the file of the bashscript it open terminal on its own when i press to close it , it creates nothing .. so any further explanation ? :) – Ahmed Zain El Dein May 2 '13 at 15:42
Are you sure? It creates the file for me. – thefourtheye May 2 '13 at 16:08

In case you wan't to execute some code before being executed you could use trap:

 trap "echo Exiting" SIGINT SIGTERM

More info here.

share|improve this answer
i am reading to understan what SIGINT means thanks for answer – Ahmed Zain El Dein May 2 '13 at 13:53

You might want the at command:

at 3 hours <<END
backGround job commands 
share|improve this answer
this will end the background ask after those 3h , right ? – Ahmed Zain El Dein May 2 '13 at 13:53
No, it will launch the task 3 hours from now, I thought that's what you wanted. – glenn jackman May 2 '13 at 14:23
hank u it is nice piece of information really but it is not what i want :) – Ahmed Zain El Dein May 2 '13 at 14:49

Your Answer


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.