Skip to content

Instantly share code, notes, and snippets.

@ekho
Last active August 10, 2020 12:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ekho/6636393 to your computer and use it in GitHub Desktop.
Save ekho/6636393 to your computer and use it in GitHub Desktop.
pgAgent daemon
http://www.ekho.name/2012/03/pgagent-debianubuntu.html
CONNECT_STRING="host=127.0.0.1 port=5432 user=postgres"
LOGLEVEL=2
RUN_AS=postgres:postgres
#!/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
@bankzitter01
Copy link

Hey,
I had an issue with the RUN_AS variable. This was behaving really weird: When executing the chown an '\r' was included. When executing the start-stop-daemon an '^M' was added. In the end i renamed the RUN_AS to RUN_AS1 and everything worked. I think RUN_AS could be a protected or used variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment