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.

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
do
backGround job commands 
done
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
add comment

3 Answers

up vote 1 down vote accepted

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

#!/bin/bash

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

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. :( http://en.wikipedia.org/wiki/SIGKILL#SIGKILL

EDIT: More Info on SIGHUP : http://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html#index-hangup-signal-2859

EDIT: Video with Test.Out file generation http://www.youtube.com/watch?v=qP0zClZZZHc

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
show 5 more comments

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

 #!/bin/bash
 trap "echo Exiting" SIGINT SIGTERM
 read        

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
add comment

You might want the at command:

at 3 hours <<END
backGround job commands 
END
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
add comment

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.