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

Hi ..I have an urgent requirement to implement below logic:
-Create a daemon
-Daemon reads i/p file containing list of hosts on which checks are to be executed and picks up 10 hosts at a time.
-Daemon runs ping command via scp on remote host (a new thread is started for this host with some PID). If this check fails, the thread simply exits and doesn't proceed with the remaining checks.
-In case ping works fine, the thread initiates other checks one by one each of which invoke a wrapper script on the remote host. The wrapper script in turn chooses appropriate script to run based on the host architecture and platform.
-Output of all the checks is logged in a log file on the remote host. Warnings / Errors may be logged in a separate file for convenience.
-Daemon parallely initiates threads on remaining servers as well. At any given time, not more than 10 threads will be active.
-Once the daemon has completed one cycle of polling, eror log from every node will be conslidated and a mail will be sent.

I am not sure of how to implement above logic. All I know is how to create a basic daemon. Here is a code:
if($pid=fork()){ #This is parent# #do some work# exit 0; } elsif(!defined $pid){ #fork failed# } else{ #this is the child that will run as daemon# setpgrp(0,0); chdir('/'); #implement some additional logic }

Could someone please provide me a basic framework in which I can implement my logic??
Any help is appreciated.

Replies are listed 'Best First'.
Re: How to fork multiple processes in a daemon
by ELISHEVA (Prior) on Sep 18, 2009 at 10:14 UTC
    I am not sure of how to implement above logic. All I know is how to create a basic daemon

    Are you unsure of just how to handle the daemon after creation or do you also need help with its various tasks, as well?

    • read ip file
    • split file into chunks of 10 ips each

    • run ping command via scp
    • invoke wrapper script on remote host
    • send output from remotely run logging scripts back to locally run script
    • collect all logging output into a single file
    • sort logging output by type (warning or error) and send to separate files

    If you are still learning how to do each of these tasks, , I would suggest that you start by writing working code that does each of the subtasks. Even if you are familiar with the pieces, I'm not convinced "create a daemon" is your first step.

    Daemons can be tricky to debug, so making sure the tasks done by the daemon work on their own is important. Adding parallel processes forked off of a main process only increases the complexity. Only when you are sure that you know how to write working code for each task, only then combine it all into one working script that forks off processes and sends then.

    Having working code for each of the subtasks will also give you a better idea of exactly what data needs to go in and out of each subtask. That will make it easier to assemble the pieces, using a tool like Parallel::ForkManager or POE.

    Even when you are ready to assemble the pieces, you should write your program so that it can be run in non-daemon mode first. Make sure it works when you trigger it manually. Then work on the wrapper code to install it as a daemon. There are a lot of things that can go wrong with daemons (permissions, for example). Unless you have previously debugged your code in non-daemon mode, it can be difficult to tell which things are due to broken code and which are due to the interaction with the environment.

    Best, beth

      Thanks for the advice Beth :) ..i'll soon write a prototype code ..let me know if you see any flaws.
      As for forking, I have no other option as I have to poll 1000+ servers.
Re: How to fork multiple processes in a daemon
by Anonymous Monk on Sep 18, 2009 at 09:40 UTC