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.
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. | [reply] |
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 :-))
| [reply] [d/l] [select] |
|
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....
| [reply] |
|
| [reply] |
|
|
|
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.
| [reply] |
|
| [reply] |
|
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.
| [reply] |
|
|
| [reply] |
Re: Perl script as daemon
by afoken (Chancellor) on Apr 27, 2009 at 20:51 UTC
|
| [reply] |
|
|