I try to create a demon with proc:: daemon, but for now, I can not complete it. My script is launched, but I wanted to create a pid file containing the number of processes assigned by the system. Can you give an example to get to do that?
I try to create a demon with proc:: daemon, but for now, I can not complete it. My script is launched, but I wanted to create a pid file containing the number of processes assigned by the system. Can you give an example to get to do that?
There's certainly better but:
You can be "demonizing" your hand process, in which case you can do whatever you want with the pid by setsid (), starting from this example from perlipc :
Code:use POSIX 'setsid'; sub daemonize { chdir '/' or die "Can't chdir to /: $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; }
I do not find this solution robust. I think you are on Linux/Unix, then Proc:: Daemon is for the daemon and it works. It handles everything correctly.
Then, if you want to retrieve its pid, I recommend the Proc:: ProcessTableYou module that can read all the running processes on your server and using a regex, you can get your script running in daemon.
Apparently it works this way:
The problem is that when I run my script, I send a log to a file that is found in the pid file also (basically I have the pid monitor my log):Code:# daemonize my $pid_file = '/var/run/daemon.pid'; Proc::Daemon::Init; open(PID,$pid_file); my $pid = <PID>; close PID; if ($pid and kill 0 => $pid) { &set_log($log,"Pid present and reactive","info"); die;} else { open(PID,"> $pid_file"); print PID $$; close PID; }
the set_log uses Log:: Handler:Code:# declaration of the connection my $server = IO::Socket::INET->new( Proto => 'tcp', LocalAddr => $serv_ip, LocalPort => $port, Listen => SOMAXCONN, Reuse => 1 ); die "Unable to start the server" unless $server; &set_log($log,"Server $serv_name is ready to receive clients","info");
and the result in the pid file:Code:sub set_log { my ($log,$logmsg,$level) = @_; $log->$level("$logmsg"); }
I'll try with setsid to see what happens, but it is a pity as Proc:Daemon done it all by himself.Code:21233aoû 07 13:33:10 [INFO] Test Server is ready to receive clients
By hand, I'd like that (I'm not sure I was clear earlier).
In fact this is exactly what Proc:DaemonCode:use POSIX qw(setsid); chdir '/' or die "Can't chdir to /: $!"; umask 0; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!"; open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; $pid = setsid or die "Can't start a new session: $!"; open my $fh, '>>', 'path/to/logfile' or die "Can't open log file:$!"; print $fh "$pid\n"; close $fh; while(1) { # ... }
Code:perldoc -q daemon How do I fork a daemon process? If by daemon process you mean one that’s detached (disassociated from its tty), then the following process is reported to work on most Unixish systems. Non-Unix users should check their Your_OS::Process module for other solutions. · Open /dev/tty and use the TIOCNOTTY ioctl on it. See tty for details. Or better yet, you can just use the POSIX::setsid() function, so you don’t have to worry about process groups. · Change directory to / · Reopen STDIN, STDOUT, and STDERR so they’re not connected to the old tty. · Background yourself like this: fork && exit; The Proc::Daemon module, available from CPAN, provides a function to perform these actions for you.
Bookmarks