Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 695  / 2 Years ago, sun, july 3, 2022, 12:47:41

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


More From » 12.04

 Answers
5

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


[#31445] Monday, July 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stantildlike

Total Points: 363
Total Questions: 135
Total Answers: 120

Location: Pitcairn Islands
Member since Fri, Dec 17, 2021
2 Years ago
;