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

My Perl daemon-like process has to run in a foreground terminal with 'interactive pauses' which allow the user to break in and stop it gracefully with contextual exit messaging. For this scenario, so far the user has also had to fire the process in the aforementioned terminal, but it would be sweet to have it fired instead by a scheduler.

I wonder if there is a technique or module in Perl to force such a process into an ordinary terminal for a user should the process itself be started via a scheduler (fcron in this case)? (No problem if unable to be embedded in the daemon's code, a parent controlling script would be ok).

It beats me, but how about the higher-mindeds!

Replies are listed 'Best First'.
Re: cron with perl
by aquarium (Curate) on Nov 08, 2010 at 22:03 UTC
    rather than the user breaking into the program to stop it, perhaps just a daemon that periodically (say once a minute) checks an input queue where you can have a client script send some sort of commands into. then the daemon upon reading a stop command takes the proper set of steps to terminate without other direct action by user.
    the hardest line to type correctly is: stty erase ^H

      mmm .. not sure I follow. The issue is only for the scheduler to be able to get the daemon process started EXACTLY AS IF IT WAS THE USER WHO STARTED IT (complete with user-console for stdout reporting).

      It works fine already when started by a user, who may cancel continuation on the basis of feedback on display in the console .. ie via stdout.

      A similar process is also schedule-run in the background with Carp'd logging -- but that, of course, does not permit the stdout-interactivity in a console that provides the cancellation opportunity required here.

      Ideally maybe a more sophisticated cron-tool would provide this functionality, but that does not seem to be on the horizon, and I just wondered if it could be achieved from the inside so to speak?

Re: cron with perl
by kcott (Archbishop) on Nov 08, 2010 at 22:08 UTC

    You could probably use something along the lines of apachectl which has features to control an Apache server similar to what you seem to want here.

    The general idea is that the daemon process stores its PID in a file which a script can subsequently access and send signals to.

    Take a look at perlipc which discusses signals (including a section on handling signals in daemons).

    -- Ken

      the builtin ipc mechanisms for each platform...it can be done, but is often a buggy/hard road unless very experienced. i would advocate instead the daemon get requests via a tcp/ip socket. there's already plenty of example daemon code out there (which is short and sweet) and you can concentrate on application logic instead of worrying about getting (semaphore/message queue/rpc/other tight coupled) ipc right. an example of this sort of setup is oracle OEM and tomcat manager application. anyhow, just a thought, as it also scales better and you can have the "client" connect from other machine(s).
      the hardest line to type correctly is: stty erase ^H

        Thanks.

        The application indeed doesn't warrant a "buggy/hard road unless very experienced".

        I will have to research the unfamiliar tcp/ip socket route. It occurred to me that this type of functionality would surely be in sufficient demand for something to exist, if only I could find the right terms in which to express the problem.

        Thanks again.

      Yes, I did pre-investigate perlipc docs, but apart from much being over (or in this case 'under') my head there, I got the impression of it being incorrectly targeted to my purposes of 'higher-level' user-console-interactivity .. ie that most ipc functionality targets 'lower-level' background detection and signal-handling. For instance, I didn't think it enabled me to spawn child processes that escaped the stdout-less environment assigned to cron? However, please correct me if I am wrong there. (My reply above to aquarium may clarify this a bit).

      I will take a look at apachectl. Thanks for the tip.

      Sorry, something going amiss. Last was in reply to kcott.

Re: cron with perl
by tod222 (Pilgrim) on Nov 09, 2010 at 04:54 UTC

    Are you on *nix or Windows? It's not impossible, but on *nix it will be tricky to have cron start the daemon while connected to a real terminal, as cron just doesn't work the way you'd like. You probably would have to write a wrapper script for starting it under cron and connecting your daemon's I/O to the user's console.

    For writing a wrapper, take a look at Net::Server. It handles all the low-level I/O for you, allowing you to write just the high level code.

    It has several different ways of operating, so one may be right for your application. It takes care of things like PID files and backgrounding the process.

    I recently implemented a simple server using Net::Server and found it had some useful examples. In my case examples/samplechat.pl was handy to examine.

      Eventually both *nix and Windows, but just *nix at present, deploying a KDE-Konsole (for the user-useful extra things like 'Save History' etc).

      And this seems to be definitely on the right track ..."as cron just doesn't work the way you'd like".

      I will come back when Net::Server docs swallowed.

      Huge thanks for the pointer.

        UPDATE

        Found a temporary OS-local solution under KDE using 'dcop' tool via shellscript wrapper call in a display-tweaked cron environment.

        Still to play with Net::Server, but more hopreful of a cross-platform solution in that direction.

        The discussion proved very useful. Thanks all.