I use daemonize to create a daemon process and then write something to the file named log,
but when I ran it, it did not work.
Why could I not write to the log?
use POSIX;
sub daemonize{
chdir '/';
umask 0;
open STDIN,'/dev/null' || die "can not open /dev/null:$!";
open STDOUT,'>/dev/null' || die "can not open /dev/null:$!";
open STDERR,'>/dev/null' ||die "can not open /dev/null:$!";
defined ($pid=fork) || die "can not fork a process:$!";
exit if $pid;
setsid || die "can not create session:$!";
}
&daemonize;
open LOG,">>/dev02/ycq/test/log" ||die "can not open file:$!";
my $num=0;
while(1){
print LOG "$num\n";
sleep 1;
}
When using
open(or any other function or subroutine), you need to take into consideration the operator precedence. In your case, the||operator has higher precedence than the,(comma) operator.Your lines that look like this:
Actually mean this, when precedence is taken into consideration:
Since
"file.txt"is a true statement, it will never trigger thediestatement.What you need is either:
or
In the first case, the parens override the operator precedence, in the second case, the
oroperator has lower precedence than the comma operator.What all this means is that your
opencalls may have failed silently.