Perl cron工作继续运行

时间:2012-09-28 01:55:43

标签: perl cron arduino command-line-arguments crontab

我目前正在使用一个cron作业来创建一个perl脚本,告诉我的arduino循环我的aquaponics系统并且一切都很顺利,除了perl脚本不按预期die

这是我的cron工作:

*/15 * * * * /home/dburke/scripts/hal/bin/main.pl cycle

以下是我的perl脚本:

#!/usr/bin/perl -w
# Sample Perl script to transmit number
# to Arduino then listen for the Arduino
# to echo it back

use strict;
use Device::SerialPort;
use Switch;
use Time::HiRes qw ( alarm );
$|++;
# Set up the serial port
# 19200, 81N on the USB ftdi driver
my $device = '/dev/arduino0';
# Tomoc has to use a different tty for testing
#$device = '/dev/ttyS0';

my $port = new Device::SerialPort ($device)
    or die('Unable to open connection to device');;
$port->databits(8);
$port->baudrate(19200);
$port->parity("none");
$port->stopbits(1);

my $lastChoice = ' ';
my $signalOut;
my $args = shift(@ARGV);
# Parent must wait for child to exit before exiting itself on CTRL+C
if ($args eq "cycle") {
                open (LOG, '>>log.txt');
                print LOG "Cycle started.\n";
                my $stop = 0;
                sleep(2);
                $SIG{ALRM} = sub {
                        print "Expecting plant bed to be full; please check.\n";
                        $signalOut = $port->write('2'); # Signal to set pin 3 low
                        print "Sent cmd: 2\n";
                        $stop = 1;
                };
                $signalOut = $port->write('1'); # Signal to arduino to set pin 3 High
                print "Sent cmd: 1\n";
                print "Waiting for plant bed to fill...\n";
                print LOG "Alarm is being set.\n";
                alarm (420);
                print LOG "Alarm is set.\n";
                while ($stop == 0) {
                        print LOG "In while-sleep loop.\n";
                        sleep(2);
                }
                print LOG "The loop has been escaped.\n";
                die "Done.";
                print LOG "No one should ever see this.";
    }
    else {
        my $pid = fork();
        $SIG{'INT'} = sub {
            waitpid($pid,0) if $pid != 0; exit(0);
        };

        # What child process should do
        if($pid == 0) {
            # Poll to see if any data is coming in
            print "\nListening...\n\n";
            while (1) {
                my $incmsg = $port->lookfor(9);
                # If we get data, then print it
                if ($incmsg) {
                    print "\nFrom arduino: " . $incmsg . "\n\n";
                }
            }
        }
        # What parent process should do
        else {
                    sleep(1);
                    my $choice = ' ';
                    print "Please pick an option you'd like to use:\n";
                    while(1) {
                        print " [1] Cycle  [2] Relay OFF  [3] Relay ON  [4] Config  [$lastChoice]: ";
                        chomp($choice = <STDIN>);
                        switch ($choice) {
                                case /1/ {
                                        $SIG{ALRM} = sub {
                                                        print "Expecting plant bed to be full; please check.\n";
                                                        $signalOut = $port->write('2'); # Signal to set pin 3 low
                                                        print "Sent cmd: 2\n";
                                                        };
                                        $signalOut = $port->write('1'); # Signal to arduino to set pin 3 High
                                        print "Sent cmd: 1\n";
                                        print "Waiting for plant bed to fill...\n";
                                        alarm (420);
                                        $lastChoice = $choice;
                                }
                                case /2/ {
                                        $signalOut = $port->write('2'); # Signal to set pin 3 low       
                                        print "Sent cmd: 2";
                                        $lastChoice = $choice;
                                }
                                case /3/ {
                                        $signalOut = $port->write('1'); # Signal to arduino to set pin 3 High
                                        print "Sent cmd: 1";
                                        $lastChoice = $choice;
                                }
                                case /4/ {
                                        print "There is no configuration available yet. Please stab the developer.";
                                }
                            else        { print "Please select a valid option.\n\n";}
                        }
                   }
           }
}

当我运行ps -ef时,我找到以下输出:

dburke   15294 15293  0 14:30 ?        00:00:00 /bin/sh -c /home/dburke/scripts/hal/bin/main.pl cycle
dburke   15295 15294  0 14:30 ?        00:00:00 /usr/bin/perl -w /home/dburke/scripts/hal/bin/main.pl cycle

不应该只有一个过程吗?即使它收到循环参数并且fork在循环参数的if块之外,它是否正在分叉?

知道为什么它不会从声明die "Done.";中消失?它从命令行运行良好,并解释'循环'参数。当它在cron中运行时运行正常,但是,进程永远不会死亡,而且每个进程都不会继续循环系统,它似乎以某种方式循环,因为它很快就会增加系统负载。

如果您想了解更多信息,请询问。谢谢你们!

1 个答案:

答案 0 :(得分:0)

看起来问题是我的脚本最初将cycle块封装在fork内部,由于某种原因,我不知道,是否正在打开一个进程(可能是孩子?) 。将cycle块从叉子中取出已经纠正了这个问题。现在它在指定的时间运行并在循环完成后正确死亡,使我的cpu负载更有用。 :)

谢谢大家对我的问题发表评论。您的建议帮助我解决了这个问题。