Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
proc
/
212
/
root
/
srv
/
admin
/
scripts
/
init.d
/
//proc/212/root/srv/admin/scripts/init.d/mongodb
#!/bin/bash # Enable errexit option: this explains why we almost never check return values. # We want this script to exit (with exitcode > 0) if anything goes wrong. set -e . /lib/start-lib.sh NAME=mongodb RUNDIR="$LOCAL_DIR/mongodb" PIDFILE="$RUNDIR/$NAME.pid" MONGODB_CONF="$CUSTOMER_DIR/.config/mongodb/mongodb.conf" MONGODB_OPTS="--config $MONGODB_CONF run" MONGODB_PATH="/opt/mongodb-${DB_VERSION}" DAEMON=${MONGODB_PATH}/bin/mongod LOCKFILE="$CUSTOMER_DIR/db/mongodb/mongod.lock" JOURNALDIR="$CUSTOMER_DIR/db/mongodb/journal" STARTTIME=1 DIETIME=10 # Time to wait for the server to die, in seconds # If this value is set too low you might not # let some servers to die gracefully and # 'restart' will not work # disable gandi-preload for the whole script unset LD_PRELOAD running_pid() { # Check if a given process pid's cmdline matches a given name pid=$1 name=$2 [ -z "$pid" ] && return 1 do_as $DB_USER [ ! -d /proc/$pid ] && return 1 cmd=`do_as $DB_USER cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1` # Is this the expected server [ "$cmd" != "$name" ] && return 1 return 0 } running() { # Check if the process is running looking at /proc # (works for all users) # No pidfile, probably no daemon present [ ! -f "$PIDFILE" ] && return 1 pid=`cat $PIDFILE` running_pid $pid $DAEMON || return 1 return 0 } upgrade_if_necessary() { local versionfile="$CUSTOMER_DIR/db/mongodb/paas-mongodb-version" local currentversion=$(do_as "$DB_USER" mongod --version | head -1) local upgrade_needed=0 echo "MongoDB current version: $currentversion" if [ -f "$versionfile" ]; then local previousversion=$(do_as "$DB_USER" cat "$versionfile") echo "MongoDB previous version: $previousversion" [ "$previousversion" != "$currentversion" ] && upgrade_needed=1 else echo 'No MongoDB version file' upgrade_needed=1 fi if [ "$upgrade_needed" -eq 1 ]; then echo "MongoDB upgrade is necessary" do_as "$DB_USER" "$DAEMON" --upgrade $MONGODB_OPTS || { echo 'MongoDB upgrade failed' exit 1 } echo "$currentversion" | do_as "$DB_USER" tee "$versionfile" \ > /dev/null echo 'MongoDB upgraded' fi } start_server() { # Remove the lockfile if the journal directory is empty or doesn't exist if [ -s $LOCKFILE ]; then if [ ! -d $JOURNALDIR ]; then echo "mongodb: no journal directory, removing lock file" do_as "$DB_USER" rm -f $LOCKFILE elif [ ! "$(ls -A $JOURNALDIR)" ]; then echo "mongodb: journal directory empty, removing lock file" do_as "$DB_USER" rm -f $LOCKFILE fi fi # Then, start the process using the wrapper do_as "$DB_USER" start-stop-daemon --start --quiet --pidfile $PIDFILE \ --chuid "$DB_USER" --exec $DAEMON -- $MONGODB_OPTS errcode=$? return $errcode } stop_server() { # Stop the process using the wrapper start-stop-daemon --stop --quiet --pidfile $PIDFILE \ --user $DB_USER \ --exec $DAEMON errcode=$? return $errcode } force_stop() { # Force the process to die killing it manually [ ! -e "$PIDFILE" ] && return if running ; then kill -15 $pid # Is it really dead? sleep "$DIETIME"s if running ; then kill -9 $pid sleep "$DIETIME"s if running ; then echo "Cannot kill $NAME (pid=$pid)!" exit 1 fi fi fi rm -f $PIDFILE } do_setup() { [ -d "$RUNDIR" ] || \ do_as "$DB_USER" install -d "$RUNDIR" } do_start() { # Check if it's running first if running ; then echo "apparently already running" exit 0 fi upgrade_if_necessary if start_server ; then # NOTE: Some servers might die some time after they start, # this code will detect this issue if STARTTIME is set # to a reasonable value [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time if running ; then echo "It's ok, the server started and is running" exit 0 else echo "It is not running after we did start" exit 1 fi else echo "Either we could not start it" exit 1 fi } do_stop() { if running ; then # Only stop the server if we see it running errcode=0 stop_server || errcode=$? exit $errcode else # If it's not running don't do anything echo "apparently not running" exit 0 fi } do_reload() { # "Reloading $NAME daemon: not implemented, as the daemon" # "cannot re-read the config file (use restart)." exit 0 } do_restart() { errcode=0 stop_server || errcode=$? # Wait some sensible amount, some server need this [ -n "$DIETIME" ] && sleep $DIETIME start_server || errcode=$? [ -n "$STARTTIME" ] && sleep $STARTTIME running || errcode=$? exit $errcode } do_status() { if running; then exit 0 else echo "$NAME apparently not running" exit 1 fi } case "$1" in start) echo "Starting $NAME" do_setup do_start ;; stop) echo "Stopping $NAME" do_stop ;; reload) do_reload ;; restart) echo "Restarting $NAME" do_restart ;; status) do_status ;; *) echo "Usage: $0 {start|stop|reload|status}" exit 1 ;; esac