Last active
September 12, 2023 18:31
-
-
Save v4r15/b246b81ec09c488ebecac4610d975456 to your computer and use it in GitHub Desktop.
Creates the necessary files for running pgagent as a service in ubuntu (or debian), and then starts the service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### | |
### It goes without saying that pgagent has to be installed in the system first | |
### On Ubuntu, after adding the PGDG apt repo, the installation can be done with | |
### sudo apt install pgagent | |
### | |
### generate the conf file in /etc | |
echo "#/etc/pgagent.conf" >> /etc/pgagent.conf | |
echo "CONNECT_STRING=\"user=postgres dbname=postgres\"" >> /etc/pgagent.conf | |
echo "LOGLEVEL=1 #warning" >> /etc/pgagent.conf | |
echo "LOGFILE=\"/var/log/postgresql/pgagent.log\"" >> /etc/pgagent.conf | |
echo "RUN_AS=postgres:postgres" >> /etc/pgagent.conf | |
### generate the init.d script in /etc/init.d | |
echo "#!/bin/sh" >> /etc/init.d/pgagent.sh | |
echo "#" >> /etc/init.d/pgagent.sh | |
echo "# start/stop pgagent daemon." >> /etc/init.d/pgagent.sh | |
echo "set -e" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "### BEGIN INIT INFO" >> /etc/init.d/pgagent.sh | |
echo "# Provides: pgagent" >> /etc/init.d/pgagent.sh | |
echo "# Required-Start: \$network \$local_fs postgresql" >> /etc/init.d/pgagent.sh | |
echo "# Required-Stop: \$network \$local_fs postgresql" >> /etc/init.d/pgagent.sh | |
echo "# Default-Start: S 2 3 4 5" >> /etc/init.d/pgagent.sh | |
echo "# Default-Stop: 0 1 6" >> /etc/init.d/pgagent.sh | |
echo "# Short-Description: pgAgent" >> /etc/init.d/pgagent.sh | |
echo "### END INIT INFO" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo ". /lib/lsb/init-functions" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "path2bin=\"/usr/bin/pgagent\"" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "if ! test -f \$path2bin; then" >> /etc/init.d/pgagent.sh | |
echo " log_failure_msg \"\$path2bin not found\"" >> /etc/init.d/pgagent.sh | |
echo " exit 1" >> /etc/init.d/pgagent.sh | |
echo "fi" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "RUN_AS=\"\"" >> /etc/init.d/pgagent.sh | |
echo "CONNECT_STRING=\"\"" >> /etc/init.d/pgagent.sh | |
echo "LOGLEVEL=0" >> /etc/init.d/pgagent.sh | |
echo "LOGFILE=\"/var/log/postgresql/pgagent.log\"" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "if [ -f /etc/default/pgagent ]; then" >> /etc/init.d/pgagent.sh | |
echo " . /etc/default/pgagent" >> /etc/init.d/pgagent.sh | |
echo "elif [ -f /etc/pgagent.conf ]; then" >> /etc/init.d/pgagent.sh | |
echo " . /etc/pgagent.conf" >> /etc/init.d/pgagent.sh | |
echo "fi" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "if [ \"\$CONNECT_STRING\" = \"\" ]; then" >> /etc/init.d/pgagent.sh | |
echo " log_failure_msg \"CONNECT_STRING not specified\"" >> /etc/init.d/pgagent.sh | |
echo " exit 1" >> /etc/init.d/pgagent.sh | |
echo "fi" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "opts=\"--quiet --oknodo --exec \$path2bin\"" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "case \"\$1\" in" >> /etc/init.d/pgagent.sh | |
echo " start)" >> /etc/init.d/pgagent.sh | |
echo " log_begin_msg \"Starting PgAgent daemon...\"" >> /etc/init.d/pgagent.sh | |
echo " if pidof \$path2bin > /dev/null; then" >> /etc/init.d/pgagent.sh | |
echo " log_begin_msg \"Already running\"" >> /etc/init.d/pgagent.sh | |
echo " log_end_msg 0" >> /etc/init.d/pgagent.sh | |
echo " exit 0" >> /etc/init.d/pgagent.sh | |
echo " fi" >> /etc/init.d/pgagent.sh | |
echo " if [ \"\$RUN_AS\" != \"\" ]; then" >> /etc/init.d/pgagent.sh | |
echo " opts=\"-c \$RUN_AS \$opts\"" >> /etc/init.d/pgagent.sh | |
echo " if [ ! -f \"\$LOGFILE\" ]; then touch \$LOGFILE; fi" >> /etc/init.d/pgagent.sh | |
echo " chown \$RUN_AS \$LOGFILE" >> /etc/init.d/pgagent.sh | |
echo " fi" >> /etc/init.d/pgagent.sh | |
echo " OPTIONS=\"-l \$LOGLEVEL -s \$LOGFILE \$CONNECT_STRING\"" >> /etc/init.d/pgagent.sh | |
echo " start-stop-daemon --start \$opts -- \$OPTIONS" >> /etc/init.d/pgagent.sh | |
echo " log_end_msg \$?" >> /etc/init.d/pgagent.sh | |
echo " ;;" >> /etc/init.d/pgagent.sh | |
echo " stop)" >> /etc/init.d/pgagent.sh | |
echo " log_begin_msg \"Stopping PgAgent daemon...\"" >> /etc/init.d/pgagent.sh | |
echo " start-stop-daemon --stop \$opts" >> /etc/init.d/pgagent.sh | |
echo " log_end_msg \$?" >> /etc/init.d/pgagent.sh | |
echo " ;;" >> /etc/init.d/pgagent.sh | |
echo " force-reload)" >> /etc/init.d/pgagent.sh | |
echo " \$0 restart" >> /etc/init.d/pgagent.sh | |
echo " ;;" >> /etc/init.d/pgagent.sh | |
echo " restart)" >> /etc/init.d/pgagent.sh | |
echo " \$0 stop" >> /etc/init.d/pgagent.sh | |
echo " \$0 start" >> /etc/init.d/pgagent.sh | |
echo " ;;" >> /etc/init.d/pgagent.sh | |
echo " status)" >> /etc/init.d/pgagent.sh | |
echo " if ! pidof \$path2bin > /dev/null; then" >> /etc/init.d/pgagent.sh | |
echo " log_success_msg \"PgAgent isn't running\"" >> /etc/init.d/pgagent.sh | |
echo " exit 3" >> /etc/init.d/pgagent.sh | |
echo " fi" >> /etc/init.d/pgagent.sh | |
echo " log_success_msg \"PgAgent running\"" >> /etc/init.d/pgagent.sh | |
echo " exit 0" >> /etc/init.d/pgagent.sh | |
echo " ;;" >> /etc/init.d/pgagent.sh | |
echo " *)" >> /etc/init.d/pgagent.sh | |
echo " log_success_msg \"Usage: \$0 {start|stop|force-reload|restart|status}\"" >> /etc/init.d/pgagent.sh | |
echo " exit 1" >> /etc/init.d/pgagent.sh | |
echo " ;;" >> /etc/init.d/pgagent.sh | |
echo "esac" >> /etc/init.d/pgagent.sh | |
echo "" >> /etc/init.d/pgagent.sh | |
echo "exit 0" >> /etc/init.d/pgagent.sh | |
### make the service shell file executable | |
sudo chmod +x /etc/init.d/pgagent.sh | |
### let the system know about it, enable it, start it and show the status | |
sudo systemctl daemon-reload | |
sudo update-rc.d pgagent.sh defaults | |
sudo update-rc.d pgagent.sh enable | |
sudo service pgagent start | |
sudo service pgagent status |
Tested and known to work on Ubuntu 18.04
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits go to
https://gist.github.com/peterneave/83cefce2a081add244ad7dc1c53bc0c3