in reply to daemon with Perl on Linux

Ok, I added the following subroutine to my main script. Maybe it sounds funny, but where exactly in the main script I should run this subroutine? My script starts with "use" thingies, then global variables, command arguments (%ARGV), and the rest subroutines and socket connections.
sub daemonize { # Change current working directory to root chdir("$logroot") or die "Cannot change to $logroot: $!"; # Set umask to 0 umask(0); # Detach STD file handles from the terminal open (STDIN,"/dev/null") or die "Cannot read to /dev/null: +$!"; open (STDOUT,">>$stoutp") or die "Cannot write to $stoutp: +$!"; open (STDERR,">>$sterr") or die "Cannot write to $sterr: $! +"; # PID forking defined(my $pid = fork) or die "Cannot fork: $!"; exit if $pid; setsid or die "Cannot set session ID: $!"; }
By the way, is there any way to assign specific PID for the process? The thing is that I need to start three processes from the same script to listen on three different ports. I need to check if these processes are running using cron.