perlã§ãã¼ã¢ã³ãä½æãããã¨ã«ãªããããªã®ã§ã
調ã¹ã¦ã¿ãã¨ã次ã®url㧠Proc::Daemon ãç´¹ä»ããã¦ããã®ã§ã試ãã«æ¸ãã¦ã¿ã¾ããã
http://perltips.twinkle.cc/perl/daemon.php
http://perltips.twinkle.cc/server/linux_daemon_startup_init_d.php
ã¨ãããããåä½ãã¦ãããã©ãã©ããªã®ããª?
#!/usr/local/bin/perl use strict; use utf8; use FindBin; use File::Spec; use lib File::Spec->catdir($FindBin::Bin, '../lib'); use Proc::Daemon; use Sing::DBI; use Sing::Model::User; use Data::Dumper; my $WORK_DIR = $FindBin::Bin; my $PID_FILE = 'send_daemon.pid'; my $SLEEP_INTERVAL = 3; my $DAEMON; main(@ARGV); exit(0); sub main { my ($action) = @_; $DAEMON = Proc::Daemon->new(work_dir=> $WORK_DIR,pid_file=> $PID_FILE); $action ||=""; if ($action eq "start"){ #daemonã®èµ·å #è¤æ°ã®èµ·åã¯ã§ãã¾ãã my ($pid_path,$pid) = get_pid_file(); if( -e $pid_path ){ print "$0 is already running. LOCK_FILE=$pid_path PID=$pid\n"; exit(1); } init(); run(); return; } if ($action eq "stop"){ #daemonã®åæ¢ unless( get_pid_file() ){ print "$0 is not running\n"; exit(1); } #action()ã®å¦çã®éä¸ã§killããã®ã¯æãã®ã§ #pid fileãåé¤ãããã¨ã§éæ¥ç?ã«killãã¾ã del_pid_file(); return; } print "usage: $0 [start|stop]\n"; return; } sub init { Proc::Daemon::Init( {work_dir=> $WORK_DIR,pid_file=> $PID_FILE}); #signaléä¿¡æã«å®è¡ããmethod $SIG{INT} = $SIG{HUP} = $SIG{QUIT} = $SIG{KILL} = $SIG{TERM} ='interrupt'; } sub interrupt { #signaléä¿¡æã«å®è¡ããã¾ã my ($sig) = @_; $SIG{$sig} = 'IGNORE'; return del_pid_file(); } sub run { while(1) { action(); #actionã®å 容ã¯ãèªç±ã«ç·¨éãã¦ãã ãã sleep($SLEEP_INTERVAL); #pidãã¡ã¤ã«ãåé¤ããã¦ããã°ãèªåèªèº«ãkillãã¾ã my ($pid_path,$pid) = get_pid_file(); if (not $pid_path ){ $DAEMON->Kill_Daemon(); return; } } } #ããã«å¥½ããªå¦çãæ¸ãã¦ä¸ãã sub action { } sub get_pid_file { #pid fileã®å称ã¨pidã®åå¾ my $pid_file_path = join('/',$WORK_DIR,$PID_FILE); return undef if not -e $pid_file_path; open(my $fh, $pid_file_path) or die "can't open $pid_file_path :$!"; my ($line) = <$fh>; close $fh or die "can't close $pid_file_path :$!"; my ($pid) = $line =~ /(\d+)/o; return ($pid_file_path,$pid); } sub del_pid_file { my $pid_file_path = join('/',$WORK_DIR,$PID_FILE); unlink $pid_file_path or die "can't unlink $pid_file_path :$!"; print "deleted $pid_file_path. daemon stop soon...\n"; } 1; __END__
åèã¾ã§ã«ã使ç¨å¯è½ãªã·ã°ãã«åã¯ã$Config{sig_name} ã§è¡¨ç¤º
使ç¨å¯è½ãªã·ã°ãã«åã¯ç°å¢ã«ãã£ã¦ç°ãªãã¨æãã¾ããã
使ç¨å¯è½ãªã·ã°ãã«åã¯$Config{sig_name}ã§ä¸è¦§è¡¨ç¤ºã§ãã¾ãã
#!/usr/local/bin/perl use strict; use warnings; use utf8; use Config; print $Config{sig_name},"\n";
å®è¡çµæ
[endo@colinux tmp]$ ./foo.pl ZERO HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS NUM32 NUM33 RTMIN NUM35 NUM36 NUM37 NUM38 NUM39 NUM40 NUM41 NUM42 NUM43 NUM44 NUM45 NUM46 NUM47 NUM48 NUM49 NUM50 NUM51 NUM52 NUM53 NUM54 NUM55 NUM56 NUM57 NUM58 NUM59 NUM60 NUM61 NUM62 NUM63 RTMAX IOT CLD POLL UNUSED [endo@colinux tmp]$
gihyo.jpã®Perl Hackers Hubãåèã«ãªããã?
â»2011/4/26追è¨
次ã®urlã«ããPerl Hackers Hubã§ã¯ãã第6åãUNIXããã°ã©ãã³ã°ã®åæï¼3ï¼ãã¨ãã¦
ã·ã°ãã«å¶å¾¡ãè¨è¼ããã¦ãã¾ããã
http://gihyo.jp/dev/serial/01/perl-hackers-hub/000603
ã·ã°ãã«åãããå¿ããã®ã§ãæ©ä¼ãããã°ãèªã¿è¿ãã¤ããã§ã
ã·ã°ãã«å | æå³ |
---|---|
SIGHUP | 主ã«è¨å®ãã¡ã¤ã«ã®åèªã¿è¾¼ã¿ãè¦æ±ããããã«ä½¿ç¨ |
SIGINT | å²ãè¾¼ã¿ãè¦æ±ï¼Ctrl-Cãæ¼ãããå ´åãªã©ï¼ |
SIGKILL | ããã»ã¹ãå¼·å¶çµäº |
SIGPIPE | éãããããã¤ããã½ã±ããã«æ¸ãè¾¼ããã¨ãã |
SIGALRM | alarmã§è¨å®ããæéãçµé |
SIGTERM | ããã»ã¹ã®çµäºãè¦è« |
SIGCHLD | åããã»ã¹ãçµäº |
KILLã·ã°ãã«ã¯ãIGNOREã§ãã©ããã§ãã¾ãã
ããå¿ããã®ã§ã¡ã¢
KILL ã·ã°ãã«ã STOP ã·ã°ãã«(TSTP ã§ã¯ãªã)ã®ãããªå¹¾ã¤ãã®ã·ã°ãã«ã¯ã ãã©ãããããã¨ãç¡è¦ãããã¨ãã§ãã¾ããã
http://perldoc.jp/docs/perl/5.8.8/perlipc.pod
apacheã使ã£ãdaemonå
http://www.usupi.org/sysad/094.html
âãã®urlã§ç´¹ä»ããã¦ãã¾ããã次ã®ãããªæ¹æ³ãããããã§ãã
CustomLog "|/usr/bin/rotatelogs /var/log/access.log 86400" \ combined
ã
SetEnvIf Request_URI "^arienai$" arienai CustomLog "|/usr/local/sbin/syslog2mail.sh" combined env=arienai
æµç³ã§ãããã¾ã