1

I am writing a perl script to parse, for example, /var/log/syslog.

The perl script triggers further subsequent tasks when particular events in the log appear. The log is parsed following the advice of this post:

Command line: monitor log file and add data to database

Which what I believe is the use of a pipe.

Now I'd like this script to forever run in the background.

This sounds like a daemon to me, and the daemon program referenced in the following question seems ideal:

How can I run a Perl script as a system daemon in linux?

But from this post, it seems clear that daemon's have no open file handles. So how can I have a daemon, or a perl script that becomes a daemon, that monitors a logfile?

Community
  • 1
  • 1
EMiller
  • 2,792
  • 4
  • 34
  • 55
  • 3
    daemons can have open file handles all they want, e.g. look at Apache. it opens/closes filehands by the millions to serve up files. What they mean is that daemons have no console input/output handles. – Marc B Sep 14 '12 at 00:56
  • Just so I understand, it can't accept input from the console or output from the console? Is that all? – EMiller Sep 14 '12 at 00:57
  • 1
    that's somewhat correct. because daemons won't always have a console. a daemon can output to a console when it's first starting up, but to truly go into the background it must disconnect stdin/stdout/stderr, otherwise it'd get shut down when the console/shell it started from exits. – Marc B Sep 14 '12 at 00:57
  • 1
    The [answer](http://stackoverflow.com/a/766412/1521179) you linked has all the answers – the important thing about a daemon is that it *detaches* itself — from stdin, stdout and stderr and the parent process, eg. `fork() and exit;` – amon Sep 14 '12 at 01:00
  • Yes, I saw "Close all open file descriptors." and assumed that meant my pipe would be closed too. I knew other daemons had to get around this, hence my confusion. – EMiller Sep 14 '12 at 01:01
  • 2
    For a daemon, you probably wouldn't pipe the logfile to stdin, but instead you would open it after the fork() – mmertel Sep 14 '12 at 02:57

2 Answers2

2

this doesn't answer your question, but is another route to consider which may or may not be appropriate for you:

rsyslog can execute a program when a certain message is logged

see Filter Conditions for setting up the up the trigger, Templates for formatting the output that's passed to the script, and Actions > Shell Execute for specifying the executable.

Be sure to read the security implications, and that ryslog blocks while the external program runs. But if your script runs reliably quickly, it may be an option.

carillonator
  • 4,715
  • 3
  • 29
  • 39
2

It sounds like what you want is a daemon. In that case the advise given in the second post you reference is the best practice. However, you do have other options like daemontools, which removes the fork complexity.

Daemons are allowed to have filehandles, but you should close STDIN, STDOUT, and STDERRR because you shouldn't really use them anymore. A lot of this has to do with the way fork works in *nix systems. Just open the pipe filehandle after your second fork, and you shouldn't have any issues.

gpapilion
  • 36
  • 1