Search code examples
pythonlinuxunixcronngrok

Starting ngrok and python app on startup


I am currently running ngrock and a python app concurrently on a specific port to text my raspberry pi, and have it respond accordingly to my message via Twilio. Each time my raspberry pi boots up, or reboots, I need to manually start the services again with ./ngrok http 5000 and python /path/to/file/app.py. To avoid that, I edited my cron jobs as follows, and wrote a script called startService.py. However, it doesn't seem to be functioning properly, as I do not receive answers to texts after reboot. Any ideas?

Cron:

# m h  dom mon dow   command
*/5 * * * * python /rasp/system/systemCheck.py
@reboot python /Rasp/system/twilio/startService.py &

startService.py

import os
os.system('/./ngrok http -subdomain=ABC123  5000')
os.system('python /Rasp/system/twilio/starter/app.py')

Solution

  • After multiple failed attempts I have seem to come up with a working system. I first had to authorize the root user to use my ngrok account by doing the following:

    sudo su
    ./ngrok authtoken {{Insert Your Auth Token Here}}
    exit
    

    Then, I created ngrokLauncher.sh and appLauncher.sh as shown.

    ngrokLauncher.sh:

    #!/bin/sh
    # launcher.sh
    
    cd /
    ./ngrok http -subdomain={{My Reserved Subdomain}} 5000 &
    cd /
    

    appLauncher.sh:

    #!/bin/sh
    # launcher.sh
    
    cd /Storage/system/twilio/starter
    python app.py &
    cd /
    

    Then, I modified the file permissions so they would be able to be run on startup sudo chmod 755 ngrokLauncher.sh && sudo chmod 755 appLauncher.sh. Lastly, I edited the Cron Jobs as follows:

    Crontab:

    # m h  dom mon dow   command
    */5 * * * * python /SKYNET/system/systemCheck.py
    @reboot sh /Storage/ngrokLauncher.sh >/Storage/logs/cronlog 2>&1
    @reboot sh /Storage/appLauncher.sh >/Storage/logs/cronlog 2>&1
    

    Then after running sudo reboot, the system restarted and I received a response to my sms from the python app.