From 39b0076bf1d2dac772c57fb47e051ae01350444f Mon Sep 17 00:00:00 2001 From: stan Date: Tue, 14 Jan 2003 12:37:47 +0000 Subject: [PATCH] Improvements to service and package handling. git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@315 e3e1d417-86f3-4887-817a-d78f3d33393f --- scripts/zm.z | 71 ++++++++++++++----------- scripts/zmpkg.pl.z | 128 ++++++++++++++++++++++++++------------------- 2 files changed, 115 insertions(+), 84 deletions(-) diff --git a/scripts/zm.z b/scripts/zm.z index e6abb8b0c..ecdae5fad 100755 --- a/scripts/zm.z +++ b/scripts/zm.z @@ -2,48 +2,59 @@ # description: Control ZoneMinder as a Service # chkconfig: 2345 99 99 +# Source function library. +. /etc/rc.d/init.d/functions + +prog=ZoneMinder command="@prefix@/bin/zmpkg.pl" -case "$1" in -'start') +start() { + echo -n "Starting $prog: " $command start - status=$? - if [ "$status" = "0" ]; then - touch /var/lock/subsys/zm - fi - ;; -'stop') + RETVAL=$? + [ $RETVAL = 0 ] && echo_success + [ $RETVAL != 0 ] && echo_failure + echo + [ $RETVAL = 0 ] && touch /var/lock/subsys/zm + return $RETVAL +} +stop() { + echo -n $"Stopping $prog: " $command stop - status=$? - if [ "$status" = "0" ]; then - rm -f /var/lock/subsys/zm - fi - ;; -'restart') - $command stop - status=$? - if [ "$status" = "0" ]; then - rm -f /var/lock/subsys/zm - fi - $command start - status=$? - if [ "$status" = "0" ]; then - touch /var/lock/subsys/zm - fi - ;; -'status') + RETVAL=$? + [ $RETVAL = 0 ] && echo_success + [ $RETVAL != 0 ] && echo_failure + echo + [ $RETVAL = 0 ] && rm -f /var/lock/subsys/zm +} +status() { result=`$command status` if [ "$result" = "running" ]; then echo "ZoneMinder is running" - status=0 + RETVAL=0 else echo "ZoneMinder is stopped" - status=1 + RETVAL=1 fi +} + +case "$1" in +'start') + start + ;; +'stop') + stop + ;; +'restart') + stop + start + ;; +'status') + status ;; *) echo "Usage: $0 { start | stop | restart | status }" - status=1 + RETVAL=1 ;; esac -exit $status +exit $RETVAL diff --git a/scripts/zmpkg.pl.z b/scripts/zmpkg.pl.z index 01a0d525a..6b8186cec 100755 --- a/scripts/zmpkg.pl.z +++ b/scripts/zmpkg.pl.z @@ -39,7 +39,7 @@ use constant ZM_PATH_LOGS => ""; use constant ZM_OPT_FAST_DELETE => ""; use constant ZM_OPT_X10 => ""; -use constant LOG_FILE => ZM_PATH_LOGS.'/zmpkg.log'; +#use constant LOG_FILE => ZM_PATH_LOGS.'/zmpkg.log'; use constant COMMAND_PATH => '@prefix@/bin/'; use constant WEB_USER => '@WEB_USER@/'; use constant VERBOSE => 0; # Whether to output more verbose debug @@ -64,62 +64,82 @@ if ( !$command || $command !~ /^(?:start|stop|restart|status)$/ ) exit( -1 ); } -my $log_file = LOG_FILE; -open( LOG, ">>$log_file" ) or die( "Can't open log file: $!" ); -open( STDOUT, ">&LOG" ) || die( "Can't dup stdout: $!" ); -select( STDOUT ); $| = 1; -open( STDERR, ">&LOG" ) || die( "Can't dup stderr: $!" ); -select( STDERR ); $| = 1; -select( LOG ); $| = 1; - -if ( $command =~ /^(?:stop|restart)$/ ) -{ - execute( COMMAND_PATH."/zmdc.pl shutdown" ); -} - -if ( $command =~ /^(?:start|restart)$/ ) -{ - execute( COMMAND_PATH."/zmfix" ); - - my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA ); - - my $sql = "select * from Monitors"; - my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); - my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() ); - while( my $monitor = $sth->fetchrow_hashref() ) - { - if ( $monitor->{Function} ne 'None' ) - { - execute( COMMAND_PATH."/zmdc.pl start zmc -d $monitor->{Device}" ); - if ( $monitor->{Function} ne 'Passive' ) - { - execute( COMMAND_PATH."/zmdc.pl start zma -m $monitor->{Id}" ); - } - } - execute( COMMAND_PATH."/zmdc.pl start zmfilter.pl -m $monitor->{Id} -e -1" ); - } - $sth->finish(); - - if ( ZM_OPT_FAST_DELETE ) - { - execute( COMMAND_PATH."/zmdc.pl start zmaudit.pl -d 900 -y" ); - } - if ( ZM_OPT_X10 ) - { - execute( COMMAND_PATH."/zmdc.pl start zmx10.pl -c start" ); - } - execute( COMMAND_PATH."/zmdc.pl start zmwatch.pl" ); -} - -if ( $command eq "status" ) -{ - print( execute( COMMAND_PATH."/zmdc.pl check" ) ); -} - sub execute { my $command = shift; my $su_command = "su @WEB_USER@ --shell=/bin/sh --command='$command'"; - print( "Executing: $su_command\n" ); + #print( "Executing: $su_command\n" ); return( qx( $su_command ) ); } + +#my $log_file = LOG_FILE; +#open( LOG, ">>$log_file" ) or die( "Can't open log file: $!" ); +#open( STDOUT, ">&LOG" ) || die( "Can't dup stdout: $!" ); +#select( STDOUT ); $| = 1; +#open( STDERR, ">&LOG" ) || die( "Can't dup stderr: $!" ); +#select( STDERR ); $| = 1; +#select( LOG ); $| = 1; + +my $status = execute( COMMAND_PATH."/zmdc.pl check" ); +my $retval = 0; + +chomp( $status ); +if ( $command =~ /^(?:stop|restart)$/ ) +{ + if ( $status eq "running" ) + { + execute( COMMAND_PATH."/zmdc.pl shutdown" ); + } + else + { + $retval = 1; + } +} + +if ( $command =~ /^(?:start|restart)$/ ) +{ + if ( $status eq "stopped" ) + { + execute( COMMAND_PATH."/zmfix" ); + + my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA ); + + my $sql = "select * from Monitors"; + my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() ); + while( my $monitor = $sth->fetchrow_hashref() ) + { + if ( $monitor->{Function} ne 'None' ) + { + execute( COMMAND_PATH."/zmdc.pl start zmc -d $monitor->{Device}" ); + if ( $monitor->{Function} ne 'Passive' ) + { + execute( COMMAND_PATH."/zmdc.pl start zma -m $monitor->{Id}" ); + } + } + execute( COMMAND_PATH."/zmdc.pl start zmfilter.pl -m $monitor->{Id} -e -1" ); + } + $sth->finish(); + + if ( ZM_OPT_FAST_DELETE ) + { + execute( COMMAND_PATH."/zmdc.pl start zmaudit.pl -d 900 -y" ); + } + if ( ZM_OPT_X10 ) + { + execute( COMMAND_PATH."/zmdc.pl start zmx10.pl -c start" ); + } + execute( COMMAND_PATH."/zmdc.pl start zmwatch.pl" ); + } + else + { + $retval = 1; + } +} + +if ( $command eq "status" ) +{ + print( $status."\n" ); +} + +exit( $retval );