Ho uno script in esecuzione sullo sfondo del mio server Linux e vorrei cogliere i segnali, come il riavvio o qualsiasi cosa che avrebbe ucciso questo script e invece salvare tutte le informazioni Importante prima di poter realmente uscire.Come posso rilevare un segnale KILL o HUP o User Abort?
Credo che la maggior parte di quello che ho bisogno di prendere è, SIGINT, SIGTERM, SIGHUP, SIGKILL.
Come catturare uno di questi segnali e farlo eseguire una funzione di uscita altrimenti continua a eseguire qualunque cosa stia facendo?
pseudo codice Perl:
#!/usr/bin/perl
use stricts;
use warnings;
while (true)
{
#my happy code is running
#my happy code will sleep for a few until its breath is back to keep running.
}
#ops I have detected an evil force trying to kill me
#let's call the safe exit.
sub safe_exit()
{
# save stuff
exit(1);
}
pseudo codice php:
<?php
while (1)
{
#my happy code is running
#my happy code will sleep for a few until its breath is back to keep running.
}
#ops I have detected an evil force trying to kill me
#let's call the safe exit.
function safe_exit()
{
# save stuff
exit(1);
}
?>
http://stackoverflow.com/questions/5180539/quick-perl-sigint-handler – daxim