Newer
Older
#!/bin/sh
#
# dm-postgresql
#
# Starts the PostgreSQL server used for DM software
#
# Modified from the original RHEL postgresql init.d script
#
# chkconfig: 345 97 97
# description: controls Dm database server
### BEGIN INIT INFO
# Provides: dm-postgresql
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: controls Dm database server
### END INIT INFO
# PGVERSION is the full package version, e.g., 8.4.0
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# PGMAJORVERSION is major version, e.g., 8.4 (this should match PG_VERSION)
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
# Source function library.
. /etc/rc.d/init.d/functions
# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`
# Get config.
. /etc/sysconfig/network
# Find the name of the script
# NAME=`basename $0`
NAME="DM Database"
# For SELinux we need to use 'runuser' not 'su'
SU=su
if [ -x /sbin/runuser ]; then
SU=runuser
fi
# Check if we are root before running command
runCommand() {
_cmd="$@"
if [ `id -u` = 0 ]; then
$SU -l $PGUSER -c "_$cmd" >> $PGSTARTUPLOG 2>&1 < /dev/null
else
eval "$_cmd" >> $PGSTARTUPLOG 2>&1 < /dev/null
fi
}
# Set defaults for configuration variables
if [ -z $DM_ROOT_DIR ]; then
myDir=`dirname $0`
currentDir=`pwd` && cd $myDir/../..
export DM_ROOT_DIR=`pwd`
cd $currentDir
fi
DM_SETUP_FILE=$DM_ROOT_DIR/setup.sh
if [ ! -f $DM_SETUP_FILE ]; then
echo "Setup file $DM_SETUP_FILE does not exist."
exit 2
fi
. $DM_SETUP_FILE > /dev/null
PGENGINE=$PGROOT/bin
#PGUSER=dm
PGUSER=`whoami`
PGPORT=11136 # 111-DM
PGDATA=$PGROOT/data
PGRUNDIR=$DM_INSTALL_DIR/var/run
PGLOGDIR=$DM_INSTALL_DIR/var/log
PGSTARTUPLOG=$PGLOGDIR/postgresql
PGPIDFILE=$PGRUNDIR/postmaster.pid
PGLOCKFILE=$PGRUNDIR/postmaster.lock
mkdir -p $PGDATA && chown -R $PGUSER:$PGGROUP $PGDATA || exit 1
mkdir -p $PGRUNDIR && chown -R $PGUSER:$PGGROUP $PGRUNDIR || exit 1
mkdir -p $PGLOGDIR && chown -R $PGUSER:$PGGROUP $PGLOGDIR || exit 1
export PGDATA
export PGPORT
export LD_LIBRARY_PATH=$PGROOT/lib
# Check that networking is up.
# Pretty much need it for postmaster.
[ "${NETWORKING}" = "no" ] && exit 1
[ -f "$PGENGINE/postmaster" ] || exit 1
script_result=0
start() {
PSQL_START=$"Starting ${NAME} service: "
# Make sure startup-time log file is valid
if [ ! -e "$PGSTARTUPLOG" -a ! -h "$PGSTARTUPLOG" ]; then
touch "$PGSTARTUPLOG" || exit 1
chown $PGUSER:$PGGROUP "$PGSTARTUPLOG"
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
chmod go-rwx "$PGSTARTUPLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGSTARTUPLOG"
fi
# Check for the PGDATA structure
if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]; then
# Check version of existing PGDATA
if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ]; then
SYSDOCDIR="(Your System's documentation directory)"
echo
echo $"An old version of the database format was found."
echo $"You need to upgrade the data format before using PostgreSQL."
echo $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information."
exit 1
fi
# No existing PGDATA! Warn the user to initdb it.
else
echo
echo "$PGDATA is missing. Use \"$0 initdb\" to initialize the cluster first."
echo_failure
echo
exit 1
fi
echo -n "$PSQL_START"
cmd="$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &"
runCommand $cmd
sleep 2
pid=`pidof -s "$PGENGINE/postmaster"`
if [ $pid ] && [ -f "$PGDATA/postmaster.pid" ]; then
success "$PSQL_START"
touch $PGLOCKFILE
head -n 1 "$PGDATA/postmaster.pid" > $PGPIDFILE
echo
else
failure "$PSQL_START"
echo
script_result=1
fi
}
stop() {
echo -n $"Stopping ${NAME} service: "
cmd="export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; $PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast"
runCommand $cmd
ret=$?
if [ $ret -eq 0 ]; then
echo_success
else
echo_failure
script_result=1
fi
echo
rm -f $PGPIDFILE
rm -f $PGLOCKFILE
}
restart() {
stop
start
}
condrestart() {
[ -e $PGLOCKFILE ] && restart
}
condstop() {
[ -e $PGLOCKFILE ] && stop
}
reload() {
cmd="export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; $PGENGINE/pg_ctl reload -D '$PGDATA' -s"
runCommand $cmd
}
initdb() {
if [ -f "$PGDATA/PG_VERSION" ]; then
echo -n "Data directory is not empty!"
echo_failure
echo
script_result=1
else
echo -n $"Initializing database: "
if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]; then
mkdir -p "$PGDATA" || exit 1
chmod go-rwx "$PGDATA"
fi
# Clean up SELinux tagging for PGDATA
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
# Make sure the startup-time log file is OK, too
if [ ! -e "$PGSTARTUPLOG" -a ! -h "$PGSTARTUPLOG" ]; then
touch "$PGSTARTUPLOG" || exit 1
chown $PGUSER:$PGGROUP "$PGSTARTUPLOG"
chmod go-rwx "$PGSTARTUPLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGSTARTUPLOG"
fi
# Initialize the database
cmd="$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'"
runCommand $cmd
# Create directory for postmaster log
mkdir -p "$PGDATA/pg_log"
chown $PGUSER:$PGGROUP "$PGDATA/pg_log"
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
chmod go-rwx "$PGDATA/pg_log"
if [ -f "$PGDATA/PG_VERSION" ]; then
echo_success
else
echo_failure
script_result=1
fi
echo
fi
}
# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status postmaster
script_result=$?
;;
restart)
restart
;;
condrestart)
condrestart
;;
condstop)
condstop
;;
reload|force-reload)
reload
;;
initdb)
initdb
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop|reload|force-reload|initdb}"
exit 1
esac
exit $script_result