#!/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 # Load SDP controlled shell environment. 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; } 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 if [[ $? -ne 0 ]]; then echo "The P4DTG_ROOT directory [$P4DTG_ROOT] does not exist." exit 1 fi declare StopFile="$P4DTG_ROOT/repl/stop-$P4DTG_CFG" declare ErrorFile="$P4DTG_ROOT/repl/err-$P4DTG_CFG" declare MovedErrorFile= # See how we were called. case "${1:-status}" in start) 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 MovedFile="$P4DTG_ROOT/repl/MOVED_FILE_err-$(date +'%Y%m%d-%H%M')" mv -f "$ErrorFile" "$MovedFile" echo -e "\nWarning: P4DTG Error File detected and moved aside to: $MovedFile\nStarting anyway due to -f.\n" else echo -e "\Error: 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 echo -e "\nStarting P4DTG with: $P4DTGBIN $P4DTG_CFG $P4DTG_ROOT" $P4DTGBIN $P4DTG_CFG $P4DTG_ROOT & ;; stop) 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 touch stop-$P4DTG_CFG 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 | 29100 | C. Thomas Tyler |
Removed requirement for systemd *.service files to have open perms in *_base scripts and templates. Removed checks for open perms on *.service files in verify_sdp.sh. Fixed minor ShellCheck compliance issue. See also: HI-101: https://swarm.workshop.perforce.com/jobs/HI-101 [Submitting, then re-opening for post-commit final review]. |
||
#9 | 27722 | C. Thomas Tyler |
Refinements to @27712: * Resolved one out-of-date file (verify_sdp.sh). * Added missing adoc file for which HTML file had a change (WorkflowEnforcementTriggers.adoc). * Updated revdate/revnumber in *.adoc files. * Additional content updates in Server/Unix/p4/common/etc/cron.d/ReadMe.md. * Bumped version numbers on scripts with Version= def'n. * Generated HTML, PDF, and doc/gen files: - Most HTML and all PDF are generated using Makefiles that call an AsciiDoc utility. - HTML for Perl scripts is generated with pod2html. - doc/gen/*.man.txt files are generated with .../tools/gen_script_man_pages.sh. #review-27712 |
||
#8 | 27109 | C. Thomas Tyler |
Refined logic so that if a shutdown is attempted and there is no server.pid file, the 'fallback/2nd pass/assured shutdown' logic to do a 'p4 admin stop' files only if a 'p4 info' command can reach the server. Otherwise, the stop command is silently ignored. Refined exit code handling and fixed log interaciton issues. Fixed issue with systemd status not indicating successful startup. The systemd unit files now specify the Type as simple rather than forking, and defer to systemd to handle the forking. When systemd is used, the -d/--daemonsafe flags are removed from service start commands for p4p/p4broker/p4d, and for p4dtg the & is removed so the process is not started in the background. For compatibility with non-systemd systems, the -d/--daemonsafe flags are still applied when systemd is not in use. With this change, systemd's concept of whether the service is running should now be reliable. If you try to start without using systemd on a system for which a systemd unit file exists, an error is displayed -- a big, hopefully useful and informative error message. #review |
||
#7 | 21986 | C. Thomas Tyler |
Enhanced P4DTG init script: * Now removes 'stop' file on startup, eliminating a manual step. * Provides useful error message if a P4DTG error file is found. * Provides a '-f' flag to move P4DTG error file aside and start anyway, but with a good error message telling the user to review the error file first. (Typically errors are just related to p4d being down.) * Made 'stop' wait for and confirm shutdown. * Made 'stop' error out if already stopped. * Made 'start' error out if already started. * Made 'restart' robust (by making stop wait for confirmed shutdown). * Cosmetic and output tweaks. |
||
#6 | 20491 | C. Thomas Tyler |
Fixed an environment insulation bug in init scripts. More testing (and thinking) revealed that the only way to truly provide a guarantee of insulation from user-set defaults in ~perforce/.bashrc (etc.) is to pass SDP_INSTANCE as a parameter to the *_base scripts, so that the instance name is explicitly passed into the su/exec call (when run as root). This change also includes minor cleanup in init scripts and *_base scripts. |
||
#5 | 20448 | C. Thomas Tyler |
Fixed env bug seen when running init scripts as root. Updated *_base scripts and *_init script templates to a new standard. Goals: 1. Init scripts that use corresponding *_base scripts are minimized so that do nothing more than set SDP_INSTANCE and then call the corresponding *_base script. 2. The 'su' commands always pass $* instead of $1, deferring all processing to the *_base script. 3. The shell environment is now guaranteeds to have the same results regardless of whether it is called as 'root' or as the defined OSUSER. 4. The p4_vars file is always sourced exactly once. Two calls to 'source p4_vars' appear in some cases, one immediatley before the su/exec call, and another after the after the 'su/exec'. Only one or the other is sourced. 5. All init scripts have a reasonably consistent usage message. 6. All init scripts accept a 'status' argument. This change fixes a bug where 'p4broker_N_init status' run as the 'perforce' user would report many pids unrelated to Perforce if run as root, e.g. doing 'service p4broker_N_init status'. This also eliminates a potential issue where the 'perforce' user might source a p4_vars with a default instance in ~/.profile or ~/.bashrc, thus invalidating the instance specified when the user ran the init script as root. |
||
#4 | 20341 | C. Thomas Tyler |
Tweaked all init scripts to default to 'status' (rather than an ugly syntax error) of the user forgets the start/stop/status parameter, as was done previosly for the broker init script. Removed unnecessary 'source' call when calling p4<whateve>_base scripts. Cleaned up comments. |
||
#3 | 18852 | C. Thomas Tyler | Enhanced P4DTG config to be more SDP-ified. | ||
#2 | 16029 | C. Thomas Tyler |
Routine merge to dev from main using: p4 merge -b perforce_software-sdp-dev |
||
#1 | 10638 | C. Thomas Tyler | Populate perforce_software-sdp-dev. | ||
//guest/perforce_software/sdp/main/Server/Unix/p4/common/etc/init.d/p4dtg_instance_init.template | |||||
#1 | 10148 | C. Thomas Tyler | Promoted the Perforce Server Deployment Package to The Workshop. |