Простой демон на Perl

Простейший демон на perl.

Интернет уже полон подобными примерами, но так таки не буду отставать =)


#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(setsid mktime);
use IO::Handle;
use Cwd;
use File::Spec::Functions;
use FindBin;
use lib "$FindBin::Bin/../lib";
use mysql;
use mylocal;


my $debug  = 0;   # ne vklyuchat - rabotaet kak govno
my $dirBin = "/usr/local/etc/switches/bin/";
my $dirTmp = "/usr/local/etc/switches/tmp/";
my $dirLog = "/usr/local/etc/switches/log/";
my $nagLog = "";

my $lockFile  = "/var/run/myDaemon.lock";
my $pidFile   = "/var/run/myDaemon.pid";
my $logFile   = "/usr/local/etc/switches/log/myDaemon-error.log";
my $debFile   = "/usr/local/etc/switches/log/myDaemon-debug.log";
my $testFile  = "/usr/local/etc/switches/tmp/myDaemon-test.log";
my $pid;
my $daemon;
my $cmd = "";
my @pids;


my $sec  = 0;
my $min  = 0;
my $hour = 0;
my $day  = 10;
my $mon  = 2 - 1;
my $year = 2010 - 1900;
my $wday = 0;
my $yday = 0;

my $unixtime = mktime( $sec , $min , $hour , $day , $mon , $year , $wday , $yday );
my $time = localtime( $unixtime );

################################################################################################



open DF , '>>' , $debFile;

$cmd = $ARGV[0];
chomp $cmd if defined $cmd;
usage() if !defined $cmd || length $cmd == 0;

if( $cmd eq "start" ){ start(); }
elsif( $cmd eq "stop" ){ stop(); }
elsif( $cmd eq "status" ){ status(); }
elsif( $cmd eq "help" ){ help(); }
else{ usage(); }

close DF;

exit 1;

################################################################################################

sub usage{
    print "\nUsage $0";
    print "Daemon rules: start || stop || status || help";
    print "";
    exit 0;
}


sub start{
    print DF "$time\tStartig daemon" if $debug;
    if ( -e $pidFile ) { print "Pid file exists: $pidFile"; exit 2; }
    print "PID: ".POSIX::getpid();
    print "$time\tWorking with Nagios log: $nagLog\ndebug is $debug\nWorking from dir is $dirBin\nPid is $pidFile\nTest file: $testFile" if $debug;

#    open STDIN,  '/dev/null'                   or die "Can't read /dev/null: $!";
    open STDOUT, '>>' , $logFile                or die "Can't write to $logFile: $!";
    open STDERR, '>>' , $logFile                or die "Can't read $logFile: $!";

    chdir $dirBin               or die "Can't chdir to $dirBin $!";
    defined( $pid = fork )      or die "Can't fork: $!";
    exit if $pid;
    POSIX::setsid() or die "Can't start new session";
    $0 = 'myDaemon';
    umask 0;

    open PID , '>' , $pidFile or die "Can't open $pidFile";
    print PID $$;
    close PID;

    open FH , '>>' , $testFile;
    $| = 1;
    while (1) {
        print FH time() if $debug;
        FH->autoflush( 1 );
        sleep 5;
    }
    close FH;

}

sub stop{
    print DF "$time\tTrying to kill daemon" if $debug;
    if ( !-e $pidFile ) { print "Pid file not exists: $pidFile"; exit 4; }
    open PID , '<' , $pidFile or die "Can't open $pidFile";
    @pids = ;
    close PID;
    kill 'HUP' , @pids;
    print "Killed: @pids ";
    unlink $pidFile;
    print DF "$time\tDaemon with pids @pids killed" if $debug;

}


sub status{
    print DF "$time\tGetting status" if $debug;
    open PID , '<' , $pidFile or die "Daemon not running, because can't open $pidFile";
    @pids = ;
    close PID;
    print "Daemon seems running, pids: @pids ";
    print DF "$time\tDaemon running with pids: @pids killed" if $debug;
}

 arviol.ru, 2006-2018

Докер -- Сильно. Выгодно. Надежно