Just set a signal handler that sets a flag and in the endless loop exit gracefully if it is time....
my $EXIT = 0; $SIG{INT} = sub{ warn "Caught Zap!\n"; $EXIT = 1 }; while (1) { print "Looping\n"; sleep 2; do{ warn "Graceful exit!\n"; exit } if $EXIT; }
Send this a ^C and it will exit gracefully. You can set signal handlers for other signals if desired using syntax like $SIG{INT} = $SIG{HUP} = sub{...} You can't catch a SIGKILL as the OS needs some untrappable way to kill code.
cheers
tachyon
In reply to Re: shutdown gracefully - kill signal trap
by tachyon
in thread shutdown gracefully - kill signal trap
by SiLiK
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |