Posted on 2005-05-03
Last Modified: 2010-03-05
for (my $i = 0; $i < @files; $i++) {
    chomp (my $date = `date '+%m%d%y-%H:%M:%S'`);
    $file = $files[$i] . "_$date";
    `mv -f $path/$files[$i] $path/$file`;

#    exec("(/usr/local/bin/perl $path/ $file) ");

    $::SIG{INT} = 'IGNORE';  # c 'turn undead' zombies
    unless(fork) {
        open STDIN,  "</dev/null";
        open STDOUT, ">/dev/null";
        exec(" /usr/local/bin/perl  $path/ $file 2 > $path/output &");
        exit;  # This block only executes if we're the child, so exit when we're done.


i m running in background, but still when i run the above script, it erminates immidiately but  after that throws the output to prompt
Question by:shahrahulb
    LVL 16

    Accepted Solution

    -- It seems you are 'exec'ing over your parent, and not your child. The unless should be replaced by if.

    > unless(fork)

    doesnt distinguish between a failed fork and a parent process. You are better off using something like

    my $pid ;
    die("$! : Couldnt fork!!") unless defined($pid = fork) ;
    if($pid) {
    LVL 48

    Expert Comment

    While we're at it, let's Perlise your very shell like code:


    for (my $i = 0; $i < @files; $i++) {
       chomp (my $date = `date '+%m%d%y-%H:%M:%S'`);
       $file = $files[$i] . "_$date";
       `mv -f $path/$files[$i] $path/$file`;


    use POSIX qw(strftime);

    foreach my $file (@files) {
      my $date = strftime '+%m%d%y-%T',localtime;
      rename "$path/$file $path/${file}_$date" or warn "Can not rename $path/$file $!\n";

    BTW, you would be better using a sensible, unambiguous, universal date format, ie: YYYYMMDD


    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Capturing SMDR data into MySQL 4 400
    perl exit code 2 231
    collecting information 2 110
    iSeries PERL Scripts 7 117
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now