#!/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/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('/media/record') || 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; } }