Thanks for the replies. Sorry I didn't explain myself very well. What I am trying to accomplish is the program to run continuously and also only exit if all code has been run through its entirety, so in my example it should only exit once it has reached the end of &test2 sub since that's where the exit 0 is being called.

The code below works but when I added the IF to check if a file exits it does not work as desired when it reaches the ELSE statement (else { print "$testFile1) when the lck file indeed exists. While that section of code loops until the lck file is removed if a ctrl-c is sent while that section of code is running then the script just gets stuck and does not continue to wait until the lck file is removed. So as long as the ctrl-c is hit outside that else statement section it works as desired.

With the code below which I removed the check of file existence the code work as desired. If I launch the script and hit ctrl-c then it only exits once it has reached the end of the test2 subroutine.

To answer you other questions: testFile1 will always exits in that location. lck file gets created by the script although right now I have no added code to have it removed as I am trying to get this one part working first. Thanks again for all the help.
#!/usr/bin/perl use strict; use warnings; use Fcntl qw(:flock); my $testFile1 = "/tmp/test.txt"; my $lck = "/tmp/l.lck"; my $wantToQuit = 0; while (1) { while ( !$wantToQuit ) { $SIG{INT} = 'waitThenExit'; my $pid = fork(); if ( !defined( $pid ) ) { die("can't fork sub proc\n"); } if ( $pid == 0 ) { print "Currently in pid==0 section, sleeping for 5 seconds +\n"; countDown(5); &test1; &test2; $SIG{INT} = 'DEFAULT'; } waitpid($pid,0); } exit 0 if $wantToQuit; } sub waitThenExit { print "\n\n\n===============WAIT SUB=================\n"; print "One moment, I just gotta finish this bit...\n"; $wantToQuit = 1; print "\n\n\n===============END WAIT SUB=================\n"; } sub test1 { print "\n\n\n===============TEST1 SUB=================\n"; print "Sleeping for 3 seconds In sub test1\n"; countDown(3); print "===============END TEST1 SUB=================\n"; } sub test2 { print "\n\n\n===============TEST2 SUB=================\n"; print "Sleeping for 3 secs In sub Test2\n"; sleep 3; print "Not done yet gotta sleep for another 15 seconds then we can + quit\n"; countDown(15); print "DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT\n"; print "===============END TEST2 SUB=================\n"; exit 0; } sub countDown { my $delay = shift; my $count = 0; while ( $count < $delay ) { print "$count\n"; sleep 1; $count++; } }
Output when script is run with no interuption
Currently in pid==0 section, sleeping for 5 seconds 0 1 2 3 4 ===============TEST1 SUB================= Sleeping for 3 seconds In sub test1 0 1 2 ===============END TEST1 SUB================= ===============TEST2 SUB================= Sleeping for 3 secs In sub Test2 Not done yet gotta sleep for another 15 seconds then we can quit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT

OUTPUT WHEN CTRL-C is hit right after the script has been launched
Currently in pid==0 section, sleeping for 5 seconds 0 1 ===============WAIT SUB================= One moment, I just gotta finish this bit... ===============END WAIT SUB================= 2 ===============WAIT SUB================= One moment, I just gotta finish this bit... ===============END WAIT SUB================= 3 4 ===============TEST1 SUB================= Sleeping for 3 seconds In sub test1 0 1 2 ===============END TEST1 SUB================= ===============TEST2 SUB================= Sleeping for 3 secs In sub Test2 Not done yet gotta sleep for another 15 seconds then we can quit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT ===============END TEST2 SUB=================

In reply to Re^2: Call Intercepts by perlnewbie9292
in thread Call Intercepts by perlnewbie9292

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.