sosedoff / pgweb

Cross-platform client for PostgreSQL databases
https://sosedoff.github.io/pgweb
MIT License
8.63k stars 732 forks source link

Startup script to be documented in wiki #208

Closed kblomqvist closed 7 years ago

kblomqvist commented 7 years ago

I made a startup script for pgweb binary. Feel free to put in wiki, maybe with Nginx reverse-proxy example (#84), which is still missing.

#!/bin/sh
### BEGIN INIT INFO
# Provides:          pgweb
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

# Installation instructions (originally written for Debian)
#
# Save this script into /etc/init.d/pgweb file, make it executable,
# and install it into the boot sequence:
#
#    chmod 755 /etc/init.d/pgweb
#    update-rc.d pgweb defaults
# 
# This script assumes that pgweb binary is located at /home/pgweb/, and that
# there's a bookmark 'server' in /home/pgweb/.pgweb/bookmarks/.
#

NAME="pgweb"
PIDFILE="/var/run/$NAME.pid"

USER="pgweb"  # Linux system user
SU="su $USER -s /bin/bash"

TIMEOUT=5  # Time in seconds to wait postgresql to show up

case "$1" in
  start)
    if [ -f $PIDFILE ]; then
        echo "Already running... cat $PIDFILE"
        exit 0
    fi

    # Wait postgresql to show up
    while ! test -f /var/run/postgresql/*main.pid
    do
        sleep 1
        TIMEOUT=`expr $TIMEOUT - 1`
        if test $TIMEOUT -eq 0; then
            exit 1
        fi
    done

    # Ready to start pgweb
    PID=`$SU -c '/home/pgweb/pgweb -s -b server >/dev/null & echo $!'`  # Note! Logs are lost.
    if [ -z $PID ]; then
        exit 1
    else
        echo $PID > $PIDFILE
    fi
    ;;
  stop)
    PID=`cat $PIDFILE`
    kill $PID && rm $PIDFILE
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop}"
    exit 1
    ;;
esac

exit 0
sosedoff commented 7 years ago

Thanks for the extra setup script. The only problem i see with it is with hardcoded path to the binary, usually stuff like that should be declared as a variable at the top.

kblomqvist commented 7 years ago

I was not able to solve how to use variables in sudo's -c command flag. Where's the original startup script?

sosedoff commented 7 years ago

Why would you need sudo to run pgweb? It does not start on port 80

kblomqvist commented 7 years ago

To run pgweb as pgweb user -- not as root.

sosedoff commented 7 years ago

Oh nvm i misread some of the config stuff. I'll take another look at this when i have time.

sosedoff commented 7 years ago

I added your example init.d config to the repo.

andreibancioiu commented 7 years ago

Here are some steps to run pgweb using systemd (I guess this only works on Ubuntu though):

touch /etc/systemd/system/pgweb.service
[Unit]
Description=pgweb
After=network.target

[Service]
User=webadmin
Group=www-data
WorkingDirectory=/home/webadmin/pgweb
ExecStart=/home/webadmin/pgweb/pgweb_linux_amd64 --listen=9090
[Install]
WantedBy=multi-user.target

Reference on how to create a systemd Unit File.

sosedoff commented 7 years ago

@andreibancioiu there's a systemd service file at https://github.com/sosedoff/pgweb/blob/master/examples/pgweb.service as well.