PriyankPa
1/28/2017 - 10:17 PM

pgAgent daemon

pgAgent daemon

http://www.ekho.name/2012/03/pgagent-debianubuntu.html

Tested on Ubuntu 16.04
1. Place file "pgagent" (script given below) at /etc/init.d/pgagent 
2. Place file "pgagent.conf" at /etc/pgagent.conf
3. sudo chmod 755 /etc/init.d/pgagent
4. sudo update-rc.d pgagent defaults (run this every time after you update the script)
5. sudo service pgagent start
#!/bin/sh
#
# start/stop pgagent daemon.

### BEGIN INIT INFO
# Provides:          pgagent
# Required-Start:    $network $local_fs postgresql
# Required-Stop:     $network $local_fs postgresql
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO

. /lib/lsb/init-functions

path2bin="/usr/bin/pgagent"

if ! test -f $path2bin; then
    log_failure_msg "$path2bin not found"
    exit 1
fi

RUN_AS=""
CONNECT_STRING=""
LOGLEVEL=0
LOGFILE="/var/log/pgagent.log"

if [ -f /etc/default/pgagent ]; then
  . /etc/default/pgagent
elif [ -f /etc/pgagent.conf ]; then
  . /etc/pgagent.conf
fi

if [ "$CONNECT_STRING" = "" ]; then
    log_failure_msg "CONNECT_STRING not specified"
    exit 1
fi

opts="--quiet --oknodo --exec $path2bin"

case "$1" in
    start)
	log_begin_msg "Starting PgAgent daemon..."
	if pidof $path2bin > /dev/null; then
		log_begin_msg "Already running"
		log_end_msg 0
		exit 0
	fi
	if [ "$RUN_AS" != "" ]; then
		opts="-c $RUN_AS $opts"
		if [ ! -f "$LOGFILE" ]; then touch $LOGFILE; fi
		chown $RUN_AS $LOGFILE
	fi
	OPTIONS="-l $LOGLEVEL -s $LOGFILE $CONNECT_STRING"
	start-stop-daemon --start $opts -- $OPTIONS
	log_end_msg $?
	;;
    stop)
	log_begin_msg "Stopping PgAgent daemon..."
	start-stop-daemon --stop $opts
	log_end_msg $?
	;;
    force-reload)
	$0 restart
	;;
    restart)
	$0 stop
	$0 start
	;;
    status)
	if ! pidof $path2bin > /dev/null; then
		log_success_msg "PgAgent isn't running"
		exit 3
	fi
	log_success_msg "PgAgent running"
	exit 0
	;;
    *)
	log_success_msg "Usage: /etc/init.d/pgagent {start|stop|force-reload|restart|status}"
	exit 1
	;;
esac

exit 0
CONNECT_STRING="host=127.0.0.1 port=5432 user=postgres"
LOGLEVEL=2
RUN_AS=postgres:postgres