3

I am trying to daemonize a perl process example test to syslog.

1) in the perl script it already log into rotate log file but I would like to log the daemonize for case of when things goes wrong before log get create 2) currently I am doing sudo /usr/sbin/daemonize -u User -a -e /home/users/me/log/log.log /home/users/me/test

It already working but the issue is that the log file will get big and the only way to handle the log file is if daemonize is stop.

So I am trying to log into syslog so I am doing

sudo /usr/sbin/daemonize -u User -a -e /dev/stderr /home/users/me/test | logger

this will have permission error when write to stderr. If I don't run as User it get log fine but for security reason I have to run my daemonize as User.

I am stuck any help is appreciate

my test sub main{ my $i = 0; for($i=0; $i < 10 ; $i++){ print "this is a test " . $i . " \n"; } }

main(@ARG);

Note I also tried the 2>&1 but does not work

smartmeta
  • 1,149
  • 1
  • 17
  • 38
user332951
  • 359
  • 1
  • 3
  • 18
  • 3
    http://stackoverflow.com/questions/766397/how-can-i-run-a-perl-script-as-a-system-daemon-in-linux - and here you go.. – Piotr Wadas Feb 16 '13 at 14:35
  • The other problem I suspect you have here (although I'm not sure exactly what /usr/sbin/daemonize does) is that you're piping the output to logger outside of the daemonize. I suspect it may close standard output (a standard thing to do when daemonizing), at which point you've lost the pipe to logger and logger will just exit. – rra Mar 17 '13 at 10:44
  • give a try to [immortal](https://immortal.run) `immortal -l your-command` – nbari Sep 07 '17 at 19:12

1 Answers1

2

You might do better to use Daemon::Daemonize from CPAN or something like it. It allows you to automatically redirect STDOUT and STDERR. It claims to handle all the setpgrp stuff you need.

I think if you combine this with using Sys::Syslog (also from CPAN) you can do all of what you need to do without out relying on /usr/sbin/daemonize and with a little more control over what is actually happening with your program.

You seem to have two distinct problems:

1) Daemonizing your program. 2) Logging to syslog.

Both are problems which are better solved directly in your Perl code. As well as Daemon::Daemonize there is Proc::Daemonize. I don't have an opinion on their relative merits, I've just used Daemon::Daemonize in the past. Either one would get your program running as a daemon.

For writing to syslog, using Sys::Sylog will do the heavy lifting for you. It's not a simple redirect, but it will allow you to send log messages and not worry about log rotation.

aikimcr
  • 332
  • 4
  • 14