Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl daemon

Status
Not open for further replies.
May 3, 2002
633
US
Why is this not running as a daemon? It works for another script I wrote, but it does a system call to a shell script, but this is not running the dsmadmc command continuously. Any ideas? Thanks.

#!/usr/bin/perl

# load required modules
use strict;
use POSIX qw(setsid);
use POSIX qw(strftime);

# set constants
my $NodeName = `uname -n`;

# flush the buffer
$| = 1;

# daemonize the program
&daemonize;

# infinite loop
while(1) {
my $val = `dsmadmc -id=user -pass=pass "SELECT count (volume_na
me) as Scratch FROM libvolumes WHERE status='Scratch'" | tail -4 | head -1 | awk
'{print $1}'`;
if ($val eq 0) { exit 0 }
elsif ($val eq 1) { exit 1 }
elsif ($val eq 2) { exit 2 }
elsif ($val eq 3) { exit 3 }
elsif ($val eq 4) { exit 4 }
elsif ($val eq 5) { exit 5 }
else { exit 99 }
}

sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDERR, '/dev/null' or die "Can' 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: $!";
}
 
I've not done this in a while - but I thought you had to close STDOUT, STDIN and STDERR rather than opening just STDIN and ERR to /dev/null Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
It's not running continuously because you explicitly tell the child to exit in this block
Code:
        if ($val eq 0) { exit 0 }
        elsif ($val eq 1) { exit 1 }
        elsif ($val eq 2) { exit 2 }
        elsif ($val eq 3) { exit 3 }
        elsif ($val eq 4) { exit 4 }
        elsif ($val eq 5) { exit 5 }
        else { exit 99 }
The child will execute your system call and then exit somewhere in this if-block. If you want it to run continuously then you will have to fork off another child inside the while-loop to execute the syscall and keep the original child alive and looping.

jaa
 
doh! LOL

yep - whatever the return value it will exit

I still think I'm right about the close on STDOUT etc though. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top