[mythtv-users] Error - w/script to restart Backend automatically on crash

A JM vbtalent at gmail.com
Thu Apr 26 14:40:49 UTC 2007


I'm having a bit of a problem implementing the following script to try
and automatically restart my SVN backend.

Anyone familiar with it can you give me some insight into what might
be wrong or if it's outdated?

I'm also not able to stop the script once it starts, ctl-x doesn't
stop it any thoughts on that as well?

AJM,


#!/usr/bin/perl -w

use strict;
my $DEBUG=0;

# Set this to the command to start mythbackend with. The sequence '2>&1' on the
# end ensures that both stdout and sterr go to the log file.
my $cmd = '/usr/local/bin/mythbackend --logfile
/var/log/mythtv/mythbackend.log --pidfile /var/run/mythbackend.pid >>
/var/log/mythtv/daemon.log 2>&1';

# Log any crashes
my $crashlog = '/var/log/myth.log';

# Change to the recordings directory to avoid a symlink deletion bug in Myth
chdir('/video/recordings') || die;

$SIG{CHLD} = 'IGNORE';
exit if( fork() );

$SIG{CHLD} = 'DEFAULT';
for(;;) {
    my $term=0;
    my $child=0;
    $SIG{TERM} = sub {
        $term=1;
        if( $child && $child != $$ ) {
            print ("Killing child $child\n") if $DEBUG;
            kill 15, $child
        }
        print "Parent $$ exiting\n";
        exit };
    if( $child = fork() ) { # parent
        wait;
        print ("Child exitied with ".int($?)."\n") if $DEBUG;
        exit 123 if $term || ($?>>8)==123;
        my $date = scalar localtime;
        `echo "$date: MYTHBACKEND CRASHED AGAIN! " >> $crashlog`
    }
    elsif( !defined $child ) {
        die "Fork failed: $!\n";
    }
    else {          # child
        system( $cmd );
        print ("Child exited with status ".int($?)."\n") if $DEBUG;
        my $sig = ($?>>8)&127;
        if( $? == -1 ) {
            print "Failed to execute\n";
        }
        elsif( $sig ) {
            print ("Child died with signal $sig\n") if $DEBUG;
            exit if $sig != 15; # allow relaunch
        }
        exit 123;
    }
}


More information about the mythtv-users mailing list