aboutsummaryrefslogtreecommitdiff
path: root/network/electrs/rc.electrs
blob: f915ae69b31be4562649c7025170b5c282fa6f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh
#
# rc.electrs - Initscript for electrs - Electrum Server in Rust
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

PRGNAM=electrs
USER=electrs
CONFDIR=/etc/$PRGNAM
LOGDIR=/var/log/$PRGNAM
PIDFILE=/var/run/$PRGNAM/$PRGNAM.pid

# Start electrs
electrs_start() {
  echo -e "\nStarting $PRGNAM..."
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE" 2>/dev/null) 2>/dev/null; then
    echo -e "$PRGNAM already up\n"
    exit 1
  fi
  runuser -u "$USER" -- sh -c "savelog -n -c 7 $LOGDIR/$PRGNAM.log"
  runuser -u "$USER" -- sh -c "$PRGNAM --skip-default-conf-files \
  --conf $CONFDIR/config.toml >$LOGDIR/$PRGNAM.log 2>&1 & echo \$! >$PIDFILE"
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
    echo -e "$PRGNAM started\n"
  else
    echo -e "$PRGNAM failed to start\n"
    exit 1
  fi
}

# Stop electrs
electrs_stop() {
  if [ -f "$PIDFILE" ]; then
    if kill -0 $(cat "$PIDFILE") 2>/dev/null; then
      echo -e "\nShutting down $PRGNAM...\n"
      kill $(cat "$PIDFILE")
        while kill -0 $(cat "$PIDFILE") 2>/dev/null; do
          sleep 1
        done
      rm -f "$PIDFILE" 2>/dev/null
    else
      echo -e "\n$PIDFILE exists, but $PRGNAM appears down\nMaking Sure..."
      pkill -u "$USER" "$PRGNAM"
        if [ $? -eq 0 ]; then
          echo -e "$PRGNAM was killed\n"
        else
          echo -e "$PRGNAM was not running\n"
        fi
      echo -e "Removing $PIDFILE\n"
      rm -f "$PIDFILE"
    fi
  else
    echo -e "\n$PIDFILE not found. Trying 'pkill'"
    pkill -u "$USER" "$PRGNAM"
      if [ $? -eq 0 ]; then
        echo -e "$PRGNAM was killed\n"
      else
        echo -e "$PRGNAM was not running\n"
     fi
  fi
}

# Restart electrs
electrs_restart() {
  electrs_stop && electrs_start
}

# Check status of electrs
electrs_status() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
    echo -e "\n$PRGNAM is up (PID: $(cat $PIDFILE))\n"
  else
    echo -e "\n$PRGNAM is down\n"
  fi
}

# Define options available
case "$1" in
  start)
  electrs_start
  ;;
  stop)
  electrs_stop
  ;;
  restart)
  electrs_restart
  ;;
  status)
  electrs_status
  ;;
  *)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
esac

exit 0