Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Perl script as daemon

by Anonymous Monk
on Apr 26, 2009 at 19:40 UTC ( [id://760174]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a script that needs to run constantly, and it performs other operations from the command line via backticks, and captures their output. This runs fine as an executable (linux), but when the same script is run as a daemon (service ... start) the commands are not run at all. Can anyone suggest a workaround for this as it must be run as a daemon... Thanks, Dave.

Replies are listed 'Best First'.
Re: Perl script as daemon
by moritz (Cardinal) on Apr 26, 2009 at 20:07 UTC
    There are many reasons why a program that works in terminal could stop working as a daemon:
    • Different environment variables, for example $PATH
    • Different user and thus missing permissions
    • Maybe some streams (STDIN, STDOUT, STDERR) are not opened for the daemon
    • Maybe one of the program it starts tries to talk to the terminal, and fails

    Your best bet is to start logging, and hope that the log file will reveal some information.

Re: Perl script as daemon
by Bloodnok (Vicar) on Apr 26, 2009 at 21:49 UTC
    As moritz points out, there are a number of ways that could prevent a script, that previously ran from CLI, from successfully running as a daemon.

    However, IMO, there are 2 questions to which the answers are not as clear as they could be WRT the running of the script as a daemon:

    • Does it start if run in background from the CLI, but not if run via cron(1M) or init(1M) ?
    • Does it just not start, or does it start and then, sometime later, halt ?

    If the answer to the first question is that it only appears to fail when run indirectly i.e. via cron(1M)/init(1M), that would tend to point to a difference/omission in run-time environment/permissions.

    OTOH, if the answer to the 2nd question is that it starts, but later, halts, that is indicative of the requirement (on behalf of the script) for standard device access preventing continued script execution - this can be, to some extent, tested by running the script from the CLI, but running the command in background under nohup(1) e.g. nohup command & ...

    • All output will be re-directed to hohup.out in the current working directory (thus all output is logged by default) and ...
    • The kernel will halt the job (and tell you via a job halted message to the console from which the command was issued) if it perceives that the job needs any of the standard devices to continue.

    A user level that continues to overstate my experience :-))
      Hi, Thanks Bloodnok, see the script itself runs away fine as a daemon, its just the lines that perform the backtick operations will not run at all, but they run fine and return output when the main script is just executed. Sorry if Im not making sense Ive only been at perl a few weeks....
        Try checking the standard variables $!/$? after the backticked (perlop) operation(s) to see what the error (if any) returned by the kernel is.

        A user level that continues to overstate my experience :-))
Re: Perl script as daemon
by igelkott (Priest) on Apr 26, 2009 at 20:08 UTC
    service ... start

    A bit vague but sounds like you're trying to run your daemon on windows. If so, check Turn a perl script into a Win32 Service and references therein.

    Otherwise, please show some code and/or error messages.

      Hmmm, just voted up your post then saw This runs fine as an executable (linux),... in the OP ... so, no Windoze there then, so we're talking an OS - not a pretend one ;-D

      A user level that continues to overstate my experience :-))

        Yep, saw that too but I guessed that the OP transferred the script to windows when I read "service ... start"

        Of course, now that I think about it, some (all?) modern linux distributions use a GUI for services so you're probably right and I tried to answer the wrong question. Certainly wouldn't be the first time.

      Some Linux distros (RH?) have a "service" command, too. But it is not standard and non-portable.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Perl script as daemon
by afoken (Chancellor) on Apr 27, 2009 at 20:51 UTC

    Use daemontools. (HOWTO)

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found