To elaborate on schkovich's first option, here's a simple modification to the System V /etc/init.d/postgresql
script.
So whenever postgres is sent "start/restart"/"reload"/"force-restart", I send the "starting-postgresql" event before the command is issued, and the "started-postgresql" after its finished. Similarly, with "stop", I send the "stopping-postgresql" event before the command and "stopped-postgresql" after its issued.
I'm only showing the final case block (case "$1" in [...] esac
at the bottom of the script. To make the changes clearer (in case future versions of postgres modify the system-V script, I've highlighted the lines that need to be inserted by putting them between #### BEGIN emit-upstart-event
and #### END emit-upstart-event
.
case "$1" in
start|stop|restart|reload)
if [ -z "`pg_lsclusters -h`" ]; then
log_warning_msg 'No PostgreSQL clusters exist; see "man pg_createcluster"'
exit 0
fi
#### BEGIN emit-upstart-event
case "$1" in
start|restart|reload)
initctl emit starting-postgresql
;;
stop)
initctl emit stopping-postgresql
;;
esac
#### END emit-upstart-event
for v in $versions; do
$1 $v || EXIT=$?
done
#### BEGIN emit-upstart-event
case "$1" in
start|restart|reload)
initctl emit started-postgresql
;;
stop)
initctl emit stopped-postgresql
;;
esac
#### END emit-upstart-event
exit ${EXIT:-0}
;;
status)
LS=`pg_lsclusters -h`
# no clusters -> unknown status
[ -n "$LS" ] || exit 4
echo "$LS" | awk 'BEGIN {rc=0} {if (match($4, "down")) rc=3; printf ("%s/%s (port %s): %s\n", $1, $2, $3, $4)}; END {exit rc}'
;;
force-reload)
#### BEGIN emit-upstart-event
initctl emit starting-postgresql
#### END emit-upstart-event
for v in $versions; do
reload $v
done
#### BEGIN emit-upstart-event
initctl emit started-postgresql
#### END emit-upstart-event
;;
*)
echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [version ..]"
exit 1
;;
esac
If you want to handle restarting with all the proper signals (by splitting restart into a stop and start commands) you can do:
case "$1" in
restart)
### HANDLING restart as a stop event and then a start event.
if [ -z "`pg_lsclusters -h`" ]; then
log_warning_msg 'No PostgreSQL clusters exist; see "man pg_createcluster"'
exit 0
fi
initctl emit stopping-postgresql
for v in $versions; do
stop $v || EXIT=$?
done
initctl emit stopped-postgresql
initctl emit starting-postgresql
for v in $versions; do
start $v || EXIT=$?
done
initctl emit started-postgresql
### END HANDLING restart separately
start|stop|reload)
if [ -z "`pg_lsclusters -h`" ]; then
log_warning_msg 'No PostgreSQL clusters exist; see "man pg_createcluster"'
exit 0
fi
#### BEGIN emit-upstart-event
case "$1" in
start|reload)
initctl emit starting-postgresql
;;
stop)
initctl emit stopping-postgresql
;;
esac
#### END emit-upstart-event
for v in $versions; do
$1 $v || EXIT=$?
done
#### BEGIN emit-upstart-event
case "$1" in
start|reload)
initctl emit started-postgresql
;;
stop)
initctl emit stopped-postgresql
;;
esac
#### END emit-upstart-event
exit ${EXIT:-0}
;;
status)
LS=`pg_lsclusters -h`
# no clusters -> unknown status
[ -n "$LS" ] || exit 4
echo "$LS" | awk 'BEGIN {rc=0} {if (match($4, "down")) rc=3; printf ("%s/%s (port %s): %s\n", $1, $2, $3, $4)}; END {exit rc}'
;;
force-reload)
#### BEGIN emit-upstart-event
initctl emit starting-postgresql
#### END emit-upstart-event
for v in $versions; do
reload $v
done
#### BEGIN emit-upstart-event
initctl emit started-postgresql
#### END emit-upstart-event
;;
*)
echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [version ..]"
exit 1
;;
esac
start on started posgre
andstop on stopped postgre
work for you? It will start/stop my-openerp always on started/stopped postgre and not only after upgrade. Please not that I don't know what is the exact name of PostgeSQL server Upstart configuration file.