Skip to content

Instantly share code, notes, and snippets.

@ilyar
Last active November 20, 2024 09:22
Show Gist options
  • Save ilyar/c2dcb48d674590bf36891ea81937f406 to your computer and use it in GitHub Desktop.
Save ilyar/c2dcb48d674590bf36891ea81937f406 to your computer and use it in GitHub Desktop.
System V init script for tonutils-reverse-proxy

System V init script for tonutils-reverse-proxy

1. Install tonutils-reverse-proxy

If you haven’t installed tonutils-reverse-proxy, follow the instructions docs.ton.org/develop/dapps/tutorials/how-to-run-ton-site, for example:

curl -fsSL -o tonutils-reverse-proxy https://github.com/ton-utils/reverse-proxy/releases/latest/download/tonutils-reverse-proxy-linux-amd64
chmod +x tonutils-reverse-proxy
mv tonutils-reverse-proxy /usr/local/bin/

Make sure the binary works:

tonutils-reverse-proxy --help

2. Configure the settings folder

mkdir -p /etc/tonutils-reverse-proxy

3. Create a System V init script

To manage tonutils-reverse-proxy, create a script at /etc/init.d/tonutils-reverse-proxy:

nano /etc/init.d/tonutils-reverse-proxy

Insert the following code:

#!/bin/bash
# /etc/init.d/tonutils-reverse-proxy
# System V init script for tonutils-reverse-proxy

### BEGIN INIT INFO
# Provides:          tonutils-reverse-proxy
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts tonutils-reverse-proxy
# Description:       A reverse proxy for TON decentralized sites.
### END INIT INFO

NAME="tonutils-reverse-proxy"
BIN="/usr/local/bin/tonutils-reverse-proxy --domain your-domain.ton"
PIDFILE="/var/run/$NAME.pid"
LOGFILE="/var/log/$NAME.log"

start() {
    echo "Starting $NAME..."
    if [ -f $PIDFILE ]; then
        echo "$NAME is already running."
    else
        cd /etc/tonutils-reverse-proxy || exit 1
        nohup $BIN > $LOGFILE 2>&1 &
        echo $! > $PIDFILE
        echo "$NAME started."
    fi
}

stop() {
    echo "Stopping $NAME..."
    if [ -f $PIDFILE ]; then
        kill $(cat $PIDFILE) && rm -f $PIDFILE
        echo "$NAME stopped."
    else
        echo "$NAME is not running."
    fi
}

status() {
    if [ -f $PIDFILE ]; then
        echo "$NAME is running. PID: $(cat $PIDFILE)"
    else
        echo "$NAME is not running."
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac
exit 0

4. Make the script executable

Grant execute permissions to the script:

chmod +x /etc/init.d/tonutils-reverse-proxy

5. Register the service

Add the service to startup:

update-rc.d tonutils-reverse-proxy defaults

6. Manage the service

You can now use the following commands:

Start:

service tonutils-reverse-proxy start

Stop:

service tonutils-reverse-proxy stop

Restart:

service tonutils-reverse-proxy restart

Check status:

service tonutils-reverse-proxy status

7. Logs and debugging

Logs will be saved to /var/log/tonutils-reverse-proxy.log. You can view them using:

tail -f /var/log/tonutils-reverse-proxy.log

Now your tonutils-reverse-proxy is set up as a service, and you can manage it via System V init.

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