Some users need to start the service with the system. This article will explain how to start the Ngrok client automatically under Linux.

This article supports Ubuntu, Raspberry PI, Centos7 and below, and Debian.

If start-stop-daemon is not installed on Centos, see installing start-stop-daemon on Centos

steps

  • 1. Download the client
  • 2. Write scripts
  • 3. Test scripts
  • 4. Set boot

Ngrok client download

1. Download the client

I don’t even have to do that because everybody knows how to do it. After the download is complete, move the client execution file to the /use/local/bin directory and grant the execution permission.

sudo mv sunny /usr/local/bin/sunny
sudo chmod +x /usr/local/bin/sunny
Copy the code

2. Write startup scripts

sudo vim /etc/init.d/sunny
Copy the code
/etc/init.d/sunny starts the script code
#! /bin/sh -e
### BEGIN INIT INFO
# Provides: ngrok.cc
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: autostartup of ngrok for Linux
### END INIT INFO

NAME=sunny
DAEMON=/usr/local/bin/$NAME
PIDFILE=/var/run/$NAME.pid

[ -x "$DAEMON"] | |exit 0

case "The $1" in
  start)
      if [ -f $PIDFILE ]; then
        echo "$NAME already running..."
        echo -e "\033[1;35mStart Fail\033[0m"
      else
        echo "Starting $NAME..."
		start-stop-daemon -S -p $PIDFILE -m -b -o -q -x $DAEMONId - clientid tunnel | |return 2
        echo -e "\033[1;32mStart Success\033[0m"
    fi
    ;;
  stop)
        echo "Stoping $NAME..."
        start-stop-daemon -K -p $PIDFILE -s TERM -o -q || return 2
        rm -rf $PIDFILE
        echo -e "\033[1;32mStop Success\033[0m"
    ;;
  restart)
    $0 stop && sleep 2 && $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit1;;esac
exit 0

Copy the code

Replace [tunnel ID] in the code with your own tunnel ID

3. Test executable code

sudo chmod 755 /etc/init.d/sunny
sudo /etc/init.d/sunny start
sudo /etc/init.d/sunny start    # start
sudo /etc/init.d/sunny stop     # stop
sudo /etc/init.d/sunny restart  # to restart
Copy the code

4. Set boot

Ubuntu, Raspberry PI, Debian series systems

cd /etc/init.d
sudo update-rc.d sunny defaults 90    # Add boot boot
sudo update-rc.d -f sunny remove  # Cancel boot
Copy the code

Centos 7 The following oss are available

sudo chkconfig --add sunny 	Add system service
sudo chkconfig --del sunny	Delete system services
sudo chkconfig --list		# Check system services
sudo chkconfig sunny on 	Set boot up
sudo chkconfig sunny off 	Set to unboot
service sunny start 		# start
service sunny stop 			# close
service sunny restart 		# to restart
Copy the code

Centos install start – stop – daemon

wget http://developer.axis.com/download/distribution/apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz
tar -xzvf apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz
Then go to the unzipped path and go to the directory where start-stop-daemon.c is located
cc start-stop-daemon.c -o start-stop-daemon
cp start-stop-daemon /usr/bin/start-stop-daemon
Copy the code

IT’s not getting kicked