How to capture the shutdown event inconsole [SOLVED]

Linux specific forum
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

How to capture the shutdown event inconsole [SOLVED]

Post 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?
Last edited by SkyManager on Sun Apr 01, 2007 8:53 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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).
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Post by SkyManager »

I cannot find any information about "SIGTERM" or "signal()" from the help manual.

Could you please give me an example?
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post 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....
Last edited by walker on Mon Apr 02, 2007 9:28 pm, edited 1 time in total.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Post by SkyManager »

Dear Fred,

Thank you for your help.
It works :P
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You can thanks walker, he did all the work ;).
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Post by SkyManager »

Thank you, walker.
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

The signal handling procedure should be defined as "ProcedureC"
quidquid Latine dictum sit altum videtur
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

you're absolutely right... (edited the above post..)
Post Reply