Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

The proper way to execute echo aabbcc >> file in backround

by young_monk_love_perl (Novice)
on Dec 06, 2013 at 10:07 UTC ( [id://1065948]=perlquestion: print w/replies, xml ) Need Help??

young_monk_love_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi ,

I am trying to execute :

system("while true; do echo aabbcc >> $temp_file; sleep 2; done &"); system("tail -f $temp_file >> $file &");

And then catch the events with the AnyEvent framework. It works but my system calls creates 2 zombie process when I kill my main program. The while and tail -f commands are still executing because the system calls are executing in an other group than my main script. How can I fix this ?

Thank you for your help. Regards

I finally got it working by using :

$childPid = open SON, "-|"; sub sigtrap() { print "\n"; print("------ Killing all childrens then exeting ... --- +---\n"); if(defined($childPid) && ! ($childPid == 0)){ killfam 'KILL', ($childPid); } print "\n"; print "Exiting ... \n"; exit(1); } if ($childPid) { #parent system("while true; do echo aabbcc >> $temp_file; sleep 2; done"); }else { print "[$$]: Child...\n"; system("tail -f $temp_file >> $file &"); exit(0); }

I finally understood that i needed to use a different process for each infinite running task and that if I wanted to do this in a non blocking mode I should do it on a fork.

Thank you all for your help

Replies are listed 'Best First'.
Re: The proper way to execute echo aabbcc >> file in backround
by karlgoethebier (Abbot) on Dec 06, 2013 at 12:55 UTC
    "How can I fix this?"

    You can't, it does what you typed. Works as designed ;-)

    Try something like perl -e 'system("tail -f foo &"); END{qx(killall tail)};'

    I will be punished for this.

    BTW, why do you do this?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Hi, thank you for your answer,

      can't I kill the system call process by pid and call it using fork ? By doing additional research I understood that my first code was poorly designed and that I should use a separate thread, could you provide me an example doing a separate thread and then killing it by using a clean way like Signals or PID please ?

Re: The proper way to execute echo aabbcc >> file in backround
by Preceptor (Deacon) on Dec 07, 2013 at 13:12 UTC

    What are you actually trying to accomplish here though? Writing out files in the background whilst the script is running? Because it looks like the result of what you're trying to do, is to write 'aabbcc' to $tempfile and $file every 2s.

    Without wanting to second guess too much, have you considered using threading? E.g.:

    my $finished = 0 : shared; sub write_to_file { until ( $finished ) { open ( my $temp_fh, ">>", $temp_file ); print $temp_fh "aabbcc\n"; close ( $temp_fh ); } } #main bit of code here... $finished = 1; foreach my $thread ( threads -> list() ) { $thread -> join(); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1065948]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found