1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Perl Service keeps dieing

Discussion in 'Programming' started by bookatechie, Oct 25, 2008.

  1. #1
    Hello!

    I'm made a little perl script that runs as a service and does some stuff in the database every ten seconds but if for some reason it can not connect to the mysql db it dies. I would just want it to ignore the problem and try again next time.
    Please help!

    #!/usr/bin/perl
    
    # Perl modules
    use strict;
    use POSIX qw(setsid);
    use Mysql;
    
    # Flush the buffer
    $| = 1;
    
    # daemonize the program
    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: $!";
    open STDERR, '>>/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: $!";
    umask 0;
    
    # Config variales
    my $host = "xxx";
    my $database = "xxx";
    my $user = "xxx";
    my $pw = "xxx";
    my $sql = "some SQL goes here";
    
    while(1) {
        my $dbh = Mysql->connect($host, $database, $user, $pw);
        $dbh->query($sql);
        undef $dbh;
        #wait 10 seconds
        sleep(10);
    }
    Code (markup):

     
    bookatechie, Oct 25, 2008 IP
  2. Nitroshock

    Nitroshock Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might have better luck setting the script to run once and then setting it up as a cron job... although a cron job running every ten seconds could get annoying (or disabled by your host).

    You might also try some error trapping using 'eval'. Here's some untested changes to get you started on that route:

    
    while(1) {
        my $dbh;
        eval { $dbh = Mysql->connect($host, $database, $user, $pw) };
        unless ($@) {    # Execute query unless there is an error connecting to Mysql
            $dbh->query($sql);
        }
        undef $dbh;
        #wait 10 seconds
        sleep(10);
    }
    
    Code (markup):
    You can also add an 'else' clause and print '$@' to STDERR if you want to see why the db connection fails. (But this will fill up your error log if you have a lot of failed connections)

    Another way to do it would be to do a conditional and/or 'ref' test on the $dbh variable to ensure it points to a valid mysql connection. This would go after the connection attempt but before the query is executed.

    Hope this gives you some ideas.

    -Nitro
     
    Nitroshock, Oct 25, 2008 IP
  3. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #3
    Your connect may be failing so next line it crashes.
     
    it career, Oct 29, 2008 IP
  4. StarDOOM

    StarDOOM Banned

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How would he fix this. I've had this problem.
     
    StarDOOM, Oct 29, 2008 IP
  5. bookatechie

    bookatechie Peon

    Messages:
    225
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It works! Yay! Thanks a lot!
     
    bookatechie, Nov 4, 2008 IP