Page 1 of 1

How to capture the shutdown event inconsole [SOLVED]

Posted: Thu Mar 29, 2007 3:46 am
by SkyManager
My app is running in console mode and I need to do some clean up when the app is being killed.
Does anybody know how to catch the app shutdown event?

I have already searched the forum and only microsoft window based version of shutdown function is shown. How about Linux version?

Posted: Sat Mar 31, 2007 11:19 am
by Fred
You will have to install a signal handler on the "SIGTERM", then you can do your cleanup in the callback (look at the 'signal()' api).

Posted: Sun Apr 01, 2007 1:54 am
by SkyManager
I cannot find any information about "SIGTERM" or "signal()" from the help manual.

Could you please give me an example?

Posted: Sun Apr 01, 2007 1:00 pm
by walker
here's a short example.... be aware that the SIGKILL can't be catched....

@FRED: may the list of constants can be added to PB?

Code: Select all


; demo for the use of *nix signals from within PB
; can be used in GUI applications too
; 2007 walker
;-------------------------------------------------
#SIGHUP	    = 1
#SIGINT	    = 2
#SIGQUIT	=	3
#SIGILL		= 4
#SIGTRAP	=	5
#SIGABRT	=	6
#SIGBUS		= 7
#SIGFPE	    =	8
#SIGKILL	    =	9
#SIGUSR1	=	10
#SIGSEGV	=	11
#SIGUSR2	=	12
#SIGPIPE	    =	13
#SIGALRM	=	14
#SIGTERM	=	15
#SIGSTKFLT	= 16
#SIGCHLD	=	17
#SIGCONT	=	18
#SIGSTOP	=	19
#SIGTSTP		= 20
#SIGTTIN		= 21
#SIGTTOU	=	22
#SIGURG		= 23
#SIGXCPU	=	24
#SIGXFSZ	=	25
#SIGVTALRM=	26
#SIGPROF	=	27
#SIGWINCH	= 28
#SIGIO		= 29
#SIGPWR	=	30
#SIGSYS		=  31

ProcedureC on_killed_do(signum)
    
    PrintN( "killed")
    End
EndProcedure

signal_(#SIGINT,@on_killed_do())
signal_(#SIGQUIT,@on_killed_do())

OpenConsole()
PrintN("kill me from outside...")
Inkey()
CloseConsole()

End
Compile this little demo and start the exe from a terminal. Open a 2nd Term and do a ps -A to get the process number. Then do a kill -s SIGQUIT <number of your process> and see....

Posted: Sun Apr 01, 2007 8:25 pm
by SkyManager
Dear Fred,

Thank you for your help.
It works :P

Posted: Sun Apr 01, 2007 10:22 pm
by Fred
You can thanks walker, he did all the work ;).

Posted: Mon Apr 02, 2007 1:42 pm
by SkyManager
Thank you, walker.

Posted: Mon Apr 02, 2007 2:54 pm
by freak
The signal handling procedure should be defined as "ProcedureC"

Posted: Mon Apr 02, 2007 9:29 pm
by walker
you're absolutely right... (edited the above post..)