Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: The proper way to execute echo aabbcc >> file in backround

by young_monk_love_perl (Novice)
on Dec 06, 2013 at 13:18 UTC ( [id://1065975]=note: print w/replies, xml ) Need Help??


in reply to Re: The proper way to execute echo aabbcc >> file in backround
in thread The proper way to execute echo aabbcc >> file in backround

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 ?

  • Comment on Re^2: The proper way to execute echo aabbcc >> file in backround

Replies are listed 'Best First'.
Re^3: The proper way to execute echo aabbcc >> file in backround
by karlgoethebier (Abbot) on Dec 06, 2013 at 14:18 UTC

      I read those links, but the thing is that I have an infinite running process that i call with exec(my process) on a fork of my main script but the exec creates a new PID and I have no way the get that PID so i can't kill it properly ...

        It works but my system calls creates 2 zombie process when I kill my main program.

        If you just want a quick fix, try putting
        $SIG{CHLD}='IGNORE';
        in your main code. You may also need to put it in your forked children before you run the exec or system.

        If you want a cleaner solution, setup your script to get the $pids.

        You can use a different form rather than system or exec to get your pid. Also, there is the problem of getting the pid of the shell, which runs your command thru system or exec. You may need to use Proc::KillFam on the pid to get all it's children. See Best way to kill a child process

        #!/usr/bin/perl #When a program forks the fork returns the pid of the #child so the parent can use this to kill it at will. my $pid = fork(); print "pid $pid created\n"; if ( $pid == 0 ) { # child process so do stuff here # usually have an exit to ensure child # does not escape this if clause exit; } else { # parent process, waits a while sleep 3; # kills child if( kill 9, $pid ){ print "pid $pid killed\n"}; } # parent continues on here (as will child if not killed or exited)

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
        You should almost always be able to assign a pid(name) to the process, and even assign the location for the pidfile, itself. This will give you the "handle" you need to control the process in most any way you choose. :)

        Best wishes.

        --Chris

        UPDATE: from perlvar
        $PROCESS_ID $PID $$ The process number of the Perl running this script. Though you can set + this variable, doing so is generally discouraged, although it can be + invaluable for some testing purposes. It will be reset automatically + across fork() calls. Note for Linux and Debian GNU/kFreeBSD users: Before Perl v5.16.0 perl + would emulate POSIX semantics on Linux systems using LinuxThreads, a + partial implementation of POSIX Threads that has since been supersed +ed by the Native POSIX Thread Library (NPTL). LinuxThreads is now obsolete on Linux, and caching getpid() like this +made embedding perl unnecessarily complex (since you'd have to manual +ly update the value of $$), so now $$ and getppid() will always retur +n the same values as the underlying C library. Debian GNU/kFreeBSD systems also used LinuxThreads up until and includ +ing the 6.0 release, but after that moved to FreeBSD thread semantics +, which are POSIX-like. To see if your system is affected by this discrepancy check if getconf + GNU_LIBPTHREAD_VERSION | grep -q NPTL returns a false value. NTPL th +reads preserve the POSIX semantics. Mnemonic: same as shells.
        See also: How do I know my process id, from within perl script
        Hey. I'm not completely useless. I can be used as a bad example.
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found