p4dtg_instance_init.template #10

  • //
  • guest/
  • perforce_software/
  • sdp/
  • main/
  • Server/
  • Unix/
  • p4/
  • common/
  • etc/
  • init.d/
  • p4dtg_instance_init.template
  • View
  • Commits
  • Open Download .zip Download (5 KB)
#!/bin/bash
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------
#
# P4DTG server init script

# p4dtg Init Script.
# chkconfig: - 90 30
# description: P4DTG Start Up Script

export TMP="$P4TMP"
declare OpMode="${1:-usage}"
export OS_INIT_MECHANISM="${OS_INIT_MECHANISM:-Unset}"
declare -i UseSystemd=0
declare ServiceName=
declare StopFile=
declare ErrorFile=
declare MovedErrorFile=

# Load SDP controlled shell environment.
# shellcheck disable=SC1091
source /p4/common/bin/p4_vars REPL_SDP_INSTANCE ||\
   { echo -e "\\nError: Failed to load SDP environment for instance REPL_SDP_INSTANCE.\n"; exit 1; }

# shellcheck disable=SC1091
source "$P4CBIN/ps_functions.sh" ||\
   { echo -e "\\nError: Failed to load SDP ps_functions.sh lib.\n"; exit 1; }

if [[ $(id -u) -eq 0 ]]; then
   exec su - "$OSUSER" -c "$0 $*"
elif [[ $(id -u -n) != "$OSUSER" ]]; then
   echo "$0 can only be run by root or $OSUSER"
   exit 1
fi

cd "$P4DTG_ROOT" ||\
   { echo -e "\\nError: Could not cd to P4DTG_ROOT directory [$P4DTG_ROOT]."; exit 1; }

StopFile="$P4DTG_ROOT/repl/stop-$P4DTG_CFG"
ErrorFile="$P4DTG_ROOT/repl/err-$P4DTG_CFG"

# Determine if systemd is configured for this instance and config.
if [[ -n "$(command -v systemctl)" ]]; then
   ServiceName="${P4DTGBIN##*/}"
   if [[ -n "$(systemctl is-enabled "$ServiceName" 2>/dev/null)" ]]; then
      UseSystemd=1
      MustUseSystemdMsg="\\nError: It appears an attempt was made to start or stop the $ServiceName\\nservice without using systemd. When a systemd unit file is configured, only the\\nsystemd init mechanism can be used to start and stop the service, so that\\nsystemd maintains a more reliable indication of the status of the service. Run\\nas root or as $OSUSER with sudo. For example:\\n\\n\\tsudo systemctl start $ServiceName\\nsudo systemctl stop $ServiceName\\n\\tsystemctl status $ServiceName\\n\\nNote that the 'status' check does not require sudo.\\n\\nThe systemd unit file is:\\n\\n$(systemctl cat "$ServiceName")"
   else
      UseSystemd=0
   fi
fi

# See how we were called.
case "$OpMode" in
   (start)
      if [[ "$UseSystemd" -eq 1 && "$OS_INIT_MECHANISM" != "systemd" ]]; then
         echo -e "$MustUseSystemdMsg"
         exit 1
      fi

      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -n "$p4dtg_pids" ]]; then
         echo -e "Error: ${P4DTGBIN} is already running as pids: $p4dtg_pids."
         exit 1
      fi

      [[ -e "$StopFile" ]] && rm -f "$StopFile"

      if [[ -e "$ErrorFile" ]]; then
         if [[ "${2:-Unset}" == "-f" ]]; then
            MovedErrorFile="$P4DTG_ROOT/repl/MOVED_FILE_err-$(date +'%Y%m%d-%H%M')"
            mv -f "$ErrorFile" "$MovedErrorFile"
            echo -e "\\nWarning: P4DTG Error File detected and moved aside to: $MovedErrorFile\\nStarting anyway due to -f.\\n"
         else
            echo -e "\\nError: Refusing to start because a P4DTG error file was found:\\n\\t$ErrorFile\\nReview this file.\\nTo start P4DTG after reviewing this file, try again with -f which will the error file aside first, i.e.: $0 start -f\\n"
            exit 1
         fi
      fi

      # If running with systemd, run the process in the foreground and let systemd handle the
      # process forking. Otherwise, start the service in the background with '&'.
      echo -e "\\nStarting P4DTG with: $P4DTGBIN $P4DTG_CFG $P4DTG_ROOT"
      if [[ "$UseSystemd" -eq 1 ]]; then
         $P4DTGBIN "$P4DTG_CFG" "$P4DTG_ROOT"
      else
         $P4DTGBIN "$P4DTG_CFG" "$P4DTG_ROOT" &
      fi
   ;;

   (stop)
      if [[ "$UseSystemd" -eq 1 && "$OS_INIT_MECHANISM" != "systemd" ]]; then
         echo -e "$MustUseSystemdMsg"
         exit 1
      fi

      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -z "$p4dtg_pids" ]]; then
         echo -e "Error: ${P4DTGBIN} is NOT running.\n"
         exit 1
      fi

      echo -e -n "\\nShutting down P4DTG instance P4DTG_CFG ..."
      cd "$P4DTG_ROOT/repl" || { echo -e "\\n\\nError: Could not cd to $P4DTG_ROOT/repl\\n"; exit 1; }
      touch "stop-$P4DTG_CFG" || { echo -e "\\n\\nError: Could not touch stop file: stop-$P4DTG_CFG\\n"; exit 1; }
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      while [[ -n "$p4dtg_pids" ]]; do
         sleep 2
         echo -n "."
         p4dtg_pids=$(get_pids "$P4DTGBIN")
      done
      echo -e "\\n\\nShutdown of P4DTG confirmed.\\n"
   ;;

   (restart)
      $0 stop
      sleep 1
      $0 start
   ;;

   (status)
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -n "$p4dtg_pids" ]]; then
         echo "${P4DTGBIN} is running as pids: $p4dtg_pids."
         exit 0
      else
         echo "${P4DTGBIN} is NOT running."
         exit 1
      fi
   ;;

   (*)
      echo -e "\\nUsage: $0 {start [-f] | stop | status | restart}\n"
      exit 1
   ;;
esac

exit 0
# Change User Description Committed
#10 29143 C. Thomas Tyler Released SDP 2022.1.29141 (2022/10/29).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#9 27761 C. Thomas Tyler Released SDP 2020.1.27759 (2021/05/07).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#8 27331 C. Thomas Tyler Released SDP 2020.1.27325 (2021/01/29).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#7 22185 C. Thomas Tyler Released SDP 2017.2.22177 (2017/05/17).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
#6 20519 C. Thomas Tyler Released SDP 2016.1.20491 (2016/09/09).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev',
with selective removal of work-in-progress files.
#5 20481 C. Thomas Tyler Released SDP 2016.1.20460.

          Copy Up using 'p4 copy -r -b perforce_software-sdp-dev',
          with selective removal of changes related to work-in-progress files.
#4 20353 C. Thomas Tyler Released SDP 2016.1.20348.
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev',
with selective removal of changes related to work-in-progress
changes.
#3 18961 C. Thomas Tyler Released: SDP/MultiArch/2016.1/18958 (2016/04/08).
#2 15856 C. Thomas Tyler Replaced the big license comment block with a shortened
form referencing the LICENSE file included with the SDP
package, and also by the URL for the license file in
The Workshop.
#1 10148 C. Thomas Tyler Promoted the Perforce Server Deployment Package to The Workshop.