Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8619847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:26:21+00:00 2026-06-12T06:26:21+00:00

I’m currently using a cron job to have a perl script that tells my

  • 0

I’m currently using a cron job to have a perl script that tells my arduino to cycle my aquaponics system and all is well, except the perl script doesn’t die as intended.

Here is my cron job:

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

And below is my perl script:

#!/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";}
                        }
                   }
           }
}

When I run ps -ef I find the following output:

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

Shouldn’t there only be one process? Is it forking even though it received the cycle argument and the fork is outside of the cycle argument’s if block?

Any idea why it wouldn’t die from the statement die "Done.";? It runs fine from the command line and interprets the ‘cycle’ argument fine. When it runs in cron it runs fine, however, the process never dies and while each process doesn’t continue to cycle the system it does seem to be looping in some way due to the fact that it ups my system load very quickly.

If you’d like more information, just ask. Thanks guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:26:22+00:00Added an answer on June 12, 2026 at 6:26 am

    It looks as thought the issue was that my script originally encapsulated the cycle block inside of the fork which for some reason, unknown to me, was leaving a process open (possibly the child?). Taking the cycle block out of the fork has corrected the issue. Now it runs at the specified time and correctly dies after the cycle is complete leaving my cpu load for something more useful. 🙂

    Thank you everyone who commented on my question. You suggestions helped me work through the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.