diff --git a/perl-xCAT/xCAT/Utils.pm b/perl-xCAT/xCAT/Utils.pm index c59db5e57..bd47dcfb2 100644 --- a/perl-xCAT/xCAT/Utils.pm +++ b/perl-xCAT/xCAT/Utils.pm @@ -3896,30 +3896,55 @@ sub servicemap{ my $path=undef; my $postfix=""; my $retdefault=$svcname; + my $svcmgrcmd; if($svcmgrtype == 0){ + #for sysvinit $path="/etc/init.d/"; + $svcmgrcmd="service"; }elsif ($svcmgrtype == 1){ - $path="/usr/lib/systemd/system/"; + #for systemd + #ubuntu 16.04 replace upstart with systemd, + #all the service unit files are placed under /lib/systemd/system/ on ubuntu + #all the service unit files are placed under /usr/lib/systemd/system/ on redhat and sles + #$path delimited with space + $path="/usr/lib/systemd/system/ /lib/systemd/system/"; $postfix=".service"; + $svcmgrcmd="systemctl"; # $retdefault=$svcname.".service"; }elsif ($svcmgrtype == 2){ $path="/etc/init/"; $postfix=".conf"; + $svcmgrcmd="initctl"; + } + + #check whether service management command is available + system "type $svcmgrcmd >/dev/null 2>&1"; + if($? != 0){ + return undef; } - + my @paths=split(" ",$path); my $ret=undef; if($svchash{$svcname}){ - foreach my $file (@{$svchash{$svcname}}){ - if(-e $path.$file.$postfix ){ - $ret=$file; - last; + foreach my $file (@{$svchash{$svcname}}){ + foreach my $ipath (@paths){ + if(-e $ipath.$file.$postfix ){ + $ret=$file; + last; + } } - } + + if(defined $ret){ + last; + } + } }else{ - if(-e $path.$retdefault.$postfix){ - $ret=$retdefault; - } + foreach my $ipath (@paths){ + if(-e $ipath.$retdefault.$postfix){ + $ret=$retdefault; + last; + } + } } return $ret; diff --git a/xCAT/postscripts/xcatlib.sh b/xCAT/postscripts/xcatlib.sh index b96f8dee5..fa238a852 100755 --- a/xCAT/postscripts/xcatlib.sh +++ b/xCAT/postscripts/xcatlib.sh @@ -278,11 +278,11 @@ function servicemap { local svcmgrtype=$2 local svclistname= - # if there are more than 1 possible service names for a service among - # different os distributions and os releases, the service should be - # specified with structure - # INIT_(general service name) = "list of possible service names" - # + # if there are more than 1 possible service names for a service among + # different os distributions and os releases, the service should be + # specified with structure + # INIT_(general service name) = "list of possible service names" + # INIT_dhcp="dhcp3-server dhcpd isc-dhcp-server"; INIT_nfs="nfsserver nfs-server nfs nfs-kernel-server"; @@ -303,19 +303,37 @@ function servicemap { local path= local postfix="" + local svcmgrcmd= local retdefault=$svcname local svcvar=${svcname//[-.]/_} if [ "$svcmgrtype" = "0" ];then + #for sysvinit path="/etc/init.d/" + svcmgrcmd="service" elif [ "$svcmgrtype" = "1" ];then + #for systemd #retdefault=$svcname.service - path="/usr/lib/systemd/system/" + #ubuntu 16.04 replace upstart with systemd, + #all the service unit files are placed under /lib/systemd/system/ on ubuntu + #all the service unit files are placed under /usr/lib/systemd/system/ on redhat and sles + #path should be delimited with space + path="/usr/lib/systemd/system/ /lib/systemd/system/" postfix=".service" + svcmgrcmd="systemctl" elif [ "$svcmgrtype" = "2" ];then + #for upstart path="/etc/init/" postfix=".conf" + svcmgrcmd="initctl" fi - + + #check whether the service management command exists + type $svcmgrcmd >/dev/null 2>&1 + if [ $? -ne 0 ];then + echo "" + return + fi + svclistname=INIT_$svcvar local svclist=$(eval echo \$$svclistname) @@ -325,12 +343,15 @@ function servicemap { for name in `echo $svclist` do - if [ -e "$path$name$postfix" ];then - echo $name - break - fi + for ipath in `echo $path`;do + if [ -e "$ipath$name$postfix" ];then + echo "$name" + return + fi + done done - + + echo "" } #some special services cannot be processed in sysVinit, upstart and systemd framework, should be process here... @@ -466,7 +487,7 @@ function restartservice { elif [ -n "$svcjob" ];then initctl status $svcjob | grep stop if [ "$?" = "0" ];then - cmd= "initctl start $svcjob" + cmd="initctl start $svcjob" else cmd="initctl restart $svcjob" fi