Kill and restart ngrok within a shell script
13:23 09 Aug 2018

I need to kill and restart my ngrok server every 24 hours, so I thought about using a cronjob to run a shell script. The issue I am facing is that when I restart ngrok within the shell script, it starts it within that given shell session.

How can I go about starting ngrok in a different session, so that I can continue to do other checks within the same script?

The code I have so far:

# grabs the PID for the current running ngrok
ngrok_pid=$(pgrep ngrok)
echo "Current ngrok PID = ${ngrok_pid}"

# kills ngrok
kill_ngrok_pid=$(kill -9 $ngrok_pid)

# get exit status code for last command
check=$?

# check if the exit status returned success
if [ $check -eq 0 ]; then
    # re-start ngrok
    $(./ngrok http 5000 &)
    # do more checks below...
else
    echo "NO ngrok PID found"
fi
shell sh ngrok