Hpux and bacula script

[root@hpux /]#  bacula status
/opt/bacula/bin/bacula-ctl-fd[22]: UNIX95=1;:  not found
bacula-fd is stopped
#! /bin/sh
#
# bacula-ctl-fd This shell script takes care of starting and stopping
#        the bacula File daemon.
#
#   This is pretty much watered down version of the RedHat script
#   that works on Solaris as well as Linux, but it won't work everywhere.
#
# description: The Leading Open Source Backup Solution.
#

PSCMD="UNIX95=1; ps -e -o pid,comm"
PS="ps"

#
# On Solaris, you may need to use nawk, or alternatively,
#  add the GNU binaries to your path, such as /usr/xpg4/bin
#
AWK=/usr/bin/awk

# All these are not *really* needed but it makes it
#  easier to "steal" this code for the development 
#  environment where they are different.
#  
BACFDBIN=/opt/bacula/sbin
BACFDCFG=/etc/opt/bacula
PIDDIR=/var/run
SUBSYSDIR=/var/run/subsys

FD_PORT=9102

FD_USER=
FD_GROUP=
Bacula="Bacula"
PIDOF=pidof    

# A function to stop a program.
killproc() {
   RC=0
   # Test syntax.
   if [ $# = 0 ]; then
      echo "Usage: killproc {program} {port} [signal]"
      return 1
   fi

   notset=0
   # check for third arg to be kill level
   if [ "$3" != "" ] ; then
      killlevel=$3
   else
      notset=1
      killlevel="-9"
   fi

   # Get base program name
   base=`basename $1`

   # Find pid.
   pid=`pidofproc $base $2`

   # Kill it.
   if [ "$pid" != "" ] ; then
      if [ "$notset" = "1" ] ; then
     if ${PS} -p "$pid">/dev/null 2>&1; then
         # TERM first, then KILL if not dead
         kill -TERM $pid 2>/dev/null
         sleep 1
         if ${PS} -p "$pid" >/dev/null 2>&1 ; then
         sleep 1
         if ${PS} -p "$pid" >/dev/null 2>&1 ; then
             sleep 3
             if ${PS} -p "$pid" >/dev/null 2>&1 ; then
             kill -KILL $pid 2>/dev/null
             fi
         fi
         fi
      fi
      ${PS} -p "$pid" >/dev/null 2>&1
      RC=$?
      [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
      #    RC=$((! $RC))
      # use specified level only
      else
     if ${PS} -p "$pid" >/dev/null 2>&1; then
        kill $killlevel $pid 2>/dev/null
        RC=$?
        [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
     fi
      fi
   else
      failure "$base shutdown"
   fi
   # Remove pid file if any.
   if [ "$notset" = "1" ]; then
      rm -f ${PIDDIR}/$base.$2.pid
   fi
   return $RC
}

# A function to find the pid of a program.
pidofproc() {
   pid=""
   # Test syntax.
   if [ $# = 0 ] ; then
      echo "Usage: pidofproc {program}"
      return 1
   fi

   # Get base program name
   base=`basename $1`

   # First try PID file
   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
      if [ "$pid" != "" ] ; then
     echo $pid
     return 0
      fi
   fi

   # Next try "pidof"
   if [ -x ${PIDOF} ] ; then
      pid=`${PIDOF} $1`
   fi
   if [ "$pid" != "" ] ; then
      echo $pid
      return 0
   fi

   # Finally try to extract it from ps
   pid=`${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`
   echo $pid
   return 0
}

status() {
   pid=""
   # Test syntax.
   if [ $# = 0 ] ; then
       echo "Usage: status {program} {port}"
       return 1
   fi

   # Get base program name
   base=`basename $1`

   # First try "pidof"
   if [ -x ${PIDOF} ] ; then
      pid=`${PIDOF} $1`
   fi
   if [ "$pid" != "" ] ; then
      echo "$base (pid $pid) is running..."
      return 0
   else
      pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 } 
        { if ((prog == $2) || (("(" prog ")") == $2) ||
         (("[" prog "]") == $2) ||
         ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
      if [ "$pid" != "" ] ; then
     echo "$base (pid $pid) is running..."
     return 0
      fi
   fi

   # Next try the PID files
   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
      if [ "$pid" != "" ] ; then
     echo "$base dead but pid file exists"
     return 1
      fi
   fi
   # See if the subsys lock exists
   if [ -f ${SUBSYSDIR}/$base ] ; then
      echo "$base dead but subsys locked"
      return 2
   fi
   echo "$base is stopped"
   return 3
}

success() {
   return 0
}

failure() {
   rc=$?
   return $rc
}

OS=`uname -s`

# if /lib/tls exists, force Bacula to use the glibc pthreads instead
if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
   export LD_ASSUME_KERNEL=2.4.19
fi

case "$1" in
   start)
      [ -x ${BACFDBIN}/bacula-fd ] && {
     echo "Starting the $Bacula File daemon"
     OPTIONS=''
     if [ "${FD_USER}" != '' ]; then
        OPTIONS="${OPTIONS} -u ${FD_USER}"
     fi

     if [ "${FD_GROUP}" != '' ]; then
        OPTIONS="${OPTIONS} -g ${FD_GROUP}"
     fi

     if [ "x${VALGRIND_FD}" = "x1" ]; then
        valgrind --leak-check=full ${BACFDBIN}/bacula-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bacula-fd.conf
     else
        ${BACFDBIN}/bacula-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bacula-fd.conf
     fi
      }
      ;;

   stop)
      # Stop the FD first so that SD will fail jobs and update catalog
      [ -x ${BACFDBIN}/bacula-fd ] && {
     echo "Stopping the $Bacula File daemon"
     killproc ${BACFDBIN}/bacula-fd ${FD_PORT}
      }
      ;;

   restart)
      $0 stop
      sleep 5
      $0 start
      ;;

   status)
      [ -x ${BACFDBIN}/bacula-fd ] && status ${BACFDBIN}/bacula-fd  ${FD_PORT}
      ;;

   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac
exit 0

Someone can help please?

Remove the semicolon:

PSCMD="UNIX95=1; ps -e -o pid,comm"

Also I guess you have to use eval here:

pid=`eval ${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`

Hi Linusolaradm1 ,

Check it out the syntax is wrongly mentioned in your script:

As there is a semicolon ( ; ) , marked in red, You need to remove it:

# UNIX95=1; ps -e -o pid,comm
ps: illegal option -- o
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]

Should be :

# UNIX95=1 ps -e -o pid,comm 
  PID COMMAND
    0 swapper
    1 init
    8 kmemdaemon
    9 ioconfigd
   10 ObjectThreadPool
   11 nfsktcpd
   12 autofskd
   13 lvmkd
   14 lvmkd
  #---------------------

You need to change 12th line:
From:

PSCMD="UNIX95=1; ps -e -o pid,comm"
  to     
PSCMD="UNIX95=1  ps -e -o pid,comm"

Thanks to all,but..doesn't work :frowning:

Your answer doesn't help us at all!

You have to be specific about how it doesn't work, what errors you got.

Sorry.
The same error on first message.

You would need to use eval to execute the contents of $PSCMD:

eval "$PSCMD"

It would be better to use a function instead, for example

pscmd(){
  UNIX95=1 ps -e -o pid,comm
}
1 Like

Thanks,with function no error

#! /bin/sh
#
# bacula-ctl-fd This shell script takes care of starting and stopping
#               the bacula File daemon.
#
#   This is pretty much watered down version of the RedHat script
#   that works on Solaris as well as Linux, but it won't work everywhere.
#
# description: The Leading Open Source Backup Solution.
#

pscmd(){
  UNIX95=1 ps -e -o pid,comm
}
PS="ps"

#
# On Solaris, you may need to use nawk, or alternatively,
#  add the GNU binaries to your path, such as /usr/xpg4/bin
#
AWK=/usr/bin/awk

# All these are not *really* needed but it makes it
#  easier to "steal" this code for the development 
#  environment where they are different.
#  
BACFDBIN=/opt/bacula/sbin
BACFDCFG=/etc/opt/bacula
PIDDIR=/var/run
SUBSYSDIR=/var/run/subsys

FD_PORT=9102

FD_USER=
FD_GROUP=
Bacula="Bacula"
PIDOF=pidof     

# A function to stop a program.
killproc() {
   RC=0
   # Test syntax.
   if [ $# = 0 ]; then
      echo "Usage: killproc {program} {port} [signal]"
      return 1
   fi

   notset=0
   # check for third arg to be kill level
   if [ "$3" != "" ] ; then
      killlevel=$3
   else
      notset=1
      killlevel="-9"
   fi

   # Get base program name
   base=`basename $1`

   # Find pid.
   pid=`pidofproc $base $2`

   # Kill it.
   if [ "$pid" != "" ] ; then
      if [ "$notset" = "1" ] ; then
         if ${PS} -p "$pid">/dev/null 2>&1; then
             # TERM first, then KILL if not dead
             kill -TERM $pid 2>/dev/null
             sleep 1
             if ${PS} -p "$pid" >/dev/null 2>&1 ; then
                 sleep 1
                 if ${PS} -p "$pid" >/dev/null 2>&1 ; then
                     sleep 3
                     if ${PS} -p "$pid" >/dev/null 2>&1 ; then
                         kill -KILL $pid 2>/dev/null
                     fi
                 fi
             fi
          fi
          ${PS} -p "$pid" >/dev/null 2>&1
          RC=$?
          [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
      #    RC=$((! $RC))
      # use specified level only
      else
         if ${PS} -p "$pid" >/dev/null 2>&1; then
            kill $killlevel $pid 2>/dev/null
            RC=$?
            [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
         fi
      fi
   else
      failure "$base shutdown"
   fi
   # Remove pid file if any.
   if [ "$notset" = "1" ]; then
      rm -f ${PIDDIR}/$base.$2.pid
   fi
   return $RC
}

# A function to find the pid of a program.
pidofproc() {
   pid=""
   # Test syntax.
   if [ $# = 0 ] ; then
      echo "Usage: pidofproc {program}"
      return 1
   fi

   # Get base program name
   base=`basename $1`

   # First try PID file
   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
      if [ "$pid" != "" ] ; then
         echo $pid
         return 0
      fi
   fi

   # Next try "pidof"
   if [ -x ${PIDOF} ] ; then
      pid=`${PIDOF} $1`
   fi
   if [ "$pid" != "" ] ; then
      echo $pid
      return 0
   fi

   # Finally try to extract it from ps
   pid=`${PSCMD} | grep $1 | ${AWK} '{ print $1 }' | tr '\n' ' '`
   echo $pid
   return 0
}

status() {
   pid=""
   # Test syntax.
   if [ $# = 0 ] ; then
       echo "Usage: status {program} {port}"
       return 1
   fi

   # Get base program name
   base=`basename $1`

   # First try "pidof"
   if [ -x ${PIDOF} ] ; then
      pid=`${PIDOF} $1`
   fi
   if [ "$pid" != "" ] ; then
      echo "$base (pid $pid) is running..."
      return 0
   else
      pid=`${PSCMD} | ${AWK} 'BEGIN { prog=ARGV[1]; ARGC=1 } 
            { if ((prog == $2) || (("(" prog ")") == $2) ||
                 (("[" prog "]") == $2) ||
                 ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
      if [ "$pid" != "" ] ; then
         echo "$base (pid $pid) is running..."
         return 0
      fi
   fi

   # Next try the PID files
   if [ -f ${PIDDIR}/$base.$2.pid ] ; then
      pid=`head -n 1 ${PIDDIR}/$base.$2.pid`
      if [ "$pid" != "" ] ; then
         echo "$base dead but pid file exists"
         return 1
      fi
   fi
   # See if the subsys lock exists
   if [ -f ${SUBSYSDIR}/$base ] ; then
      echo "$base dead but subsys locked"
      return 2
   fi
   echo "$base is stopped"
   return 3
}

success() {
   return 0
}

failure() {
   rc=$?
   return $rc
}

OS=`uname -s`

# if /lib/tls exists, force Bacula to use the glibc pthreads instead
if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
   export LD_ASSUME_KERNEL=2.4.19
fi

case "$1" in
   start)
      [ -x ${BACFDBIN}/bacula-fd ] && {
         echo "Starting the $Bacula File daemon"
         OPTIONS=''
         if [ "${FD_USER}" != '' ]; then
            OPTIONS="${OPTIONS} -u ${FD_USER}"
         fi

         if [ "${FD_GROUP}" != '' ]; then
            OPTIONS="${OPTIONS} -g ${FD_GROUP}"
         fi

         if [ "x${VALGRIND_FD}" = "x1" ]; then
            valgrind --leak-check=full ${BACFDBIN}/bacula-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bacula-fd.conf
         else
            ${BACFDBIN}/bacula-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bacula-fd.conf
         fi
      }
      ;;

   stop)
      # Stop the FD first so that SD will fail jobs and update catalog
      [ -x ${BACFDBIN}/bacula-fd ] && {
         echo "Stopping the $Bacula File daemon"
         killproc ${BACFDBIN}/bacula-fd ${FD_PORT}
      }
      ;;

   restart)
      $0 stop
      sleep 5
      $0 start
      ;;

   status)
      [ -x ${BACFDBIN}/bacula-fd ] && status ${BACFDBIN}/bacula-fd  ${FD_PORT}
      ;;

   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac
exit 0

But return this

bacula status
bacula-fd dead but pid file exists

but bacula is working

ps -ef|grep bacu|grep -v grep
    root  3303     1  0 12:28:53 ?         0:00 /opt/bacula/sbin/bacula-fd -v -c /etc/opt/bacula/bacula-fd.conf

OK, but that function does not seem to be called anywhere in the script. You would need to call that function instead of $PSCMD

1 Like

Ok,thanks for help