从任何地方运行Perl代码(包括文件路径)

时间:2017-01-27 11:16:23

标签: perl daemon

我正在使用perl创建一个守护进程,我无法让脚本从任何地方运行(根据需要)。该脚本将进程ID写入文件,并记录输出。有效的代码如下,如果我尝试更改它,例如get pwd并在我的文件夹名称之前插入此错误,我得到的错误是没有文件或目录,例如我说$ dir = pwd然后将我的pid文件设置为$ dir / pid / conductor_daemon_test.pid。任何想法如何让它运行,无论位置如何?

#!/usr/bin/perl

use POSIX qw(setsid);
my $proc;
my $error;
my $file="conductor.pl";
my $pidfile=">./home/perl_daemon/conductor/pid/conductor_daemon_test.pid";
my$pid2check="/home/perl_daemon/conductor/pid/conductor_daemon_test.pid";
my $pid;

#Make it a daemon
$proc = Daemonize();

if(!$error){
    LogMessage("$file:PID $proc: Begin");
}

#Write Pid Information
if(!$error){
    if(-e $pid2check){
            LogMessage("$file : PID File $pid2check already exists. Exiting..");
            exit(0);
    }
    else {
            unless(open(FILE, $pidfile)){
                    $error = "Error opening file for writing ".$!;
            }
    }
    }
    if(!$error) {
    LogMessage("$file: PID $proc: Writing pid information to $pidfile");
    print FILE $proc ."\n";
    close(FILE);
     }

   my $EXIT = 0;
   $SIG{TERM} = sub{ LogMessage("Caught exit signal!\n");$EXIT=1};

   #Main loop of the Daemon
   while (!$error){

    sleep(100);
    LogMessage("Hello World");


    do { LogMessage("Graceful exit!\n"); exit } if $EXIT;

    }
    if($error){
    LogMessage("$file:PID $proc:Error $error");
    }

    LogMessage ("$file: PID $proc: END");enter code here

     exit(0);

     #
     #Subs
     #
     #####################################

     #       Daemonize
     #
     #####################################
     #
     #       Used to make this program a daemon
     #       Also to redirect STDIN, STDERR, STDOUT
    #       Returns PID
    #
    ########

sub Daemonize {
    #Ensure that the current directory is the working directory
    unless(chdir '/'){
            $error = "Can't chdir to /:$!";
    }
    #Ensure that the file mode mask is changed
    unless(umask 0){
            $error="Unable to umask 0";
    }
    #Ensure the STDIN is closed
    unless(open STDIN, '/dev/null'){
            $error="Can't read /dev/null:$!";
    }
    #All print statements will now be sent to our log file
    unless(open STDOUT, '>> /home/perl_daemon/conductor/log/conductor_daemon_test.log'){
            $error="Can't read /home/perl_daemon/conductor/log/conductor_daemon_test.log:$!";
    }
#All error messages will now be sent to our log file
    unless(open STDERR, '>>/home/perl_daemon/conductor/log/conductor_daemon_test.log'){
            $error="Can't write to /home/perl_daemon/conductor/log/conductor_daemon_test.log:$!";
    }

#Fork off the parent process
defined($pid = fork);
#Exit if $pid exists(parent)
exit(0) if $pid;

#As Child
#Create a new SID for the child process
setsid();
$proc = $$;
return($proc);
}


####
#
#       Log Message
#
#       Used to log messages
#
######

sub LogMessage {
    my $message = $_[0];
    print localtime()." $message\n";
 }

2 个答案:

答案 0 :(得分:1)

使用作为核心模块的FindBin,您可以轻松找到脚本的位置:

  

说明

     

找到脚本bin目录的完整路径以允许使用   相对于bin目录的路径。

我的建议是你可以为你的脚本创建一个模式,这样你就可以将所有内容放在同一个文件夹中,如:

daemon/script.pl
daemon/pid/ <- pid files
daemon/log/ <- logs 

所以你的代码就像这样:

use FindBin qw($RealBin);

my $pidfile=">.$RealBin/pid/conductor_daemon_test.pid";
my $pid2check="$RealBin/pid/conductor_daemon_test.pid";

您可以导出以下变量:

  

可出口的变量

 $Bin         - path to bin directory from where script was invoked
 $Script      - basename of script from which perl was invoked
 $RealBin     - $Bin with all links resolved
 $RealScript  - $Script with all links resolved

然后我猜你不再有位置问题了。

答案 1 :(得分:0)

似乎你没有创造出这样的“pid”。尝试使用pwd时的子目录。在尝试创建pid文件之前创建它。

另外:请使用词法文件句柄和3参数形式的open:open (my $fh, '>', $filename)而不是GLOB(FILE)和'>filename.pid'