Search code examples
pythonpython-3.xlinuxopenwrt

Run Python program on bootup/reboot, on separate partition in OpenWrt Linux


On OpenWrt Linux, I put a startup script in the /etc/init.d folder, and enabled it. The script is fine, and looks as follows:

#!/bin/sh /etc/rc.common

# Automatically place an "S91canpy" symlink in /etc/rc.d/ once we enable it
# This means it will start right after /etc/rc.d/90openvpn
START=91

# This is what will run when this service starts
start() { 
    # Run the process in the background (&), and direct errors to a log file
    sh /user/start_everything.sh >/var/log/init_canpy.log 2>&1 &
}

# This is what will run when the service stops
stop() {
    echo "no stop function set up yet"
}

# This is what will run when the service restarts
restart() {
    # Run the process in the background (&), and direct errors to a log file
    sh /user/start_everything.sh >/var/log/init_canpy.log 2>&1 &
}

The /user/start_everything.sh script it calls looks as follows:

#!/bin/sh

# Run the "find_config_data.py" Python program 
/data/venv/bin/python3.6 /user/canpy/find_config_data.py

The problem is that /data is on a separate hard drive partition, so init.d has problems finding it. The error I get is as follows:

line 4: /data/venv/bin/python3.6: not found

My main partition only has 20 MB of space left on it, so I must install Python 3.6 and its libraries onto the /data partition, which has 2.5 GB of space.

How can I make init.d find my Python binary at /data/venv/bin/python3.6 ? I absolutely must have this Python program run every time Linux boots/reboots. THANKS!!

The following is my partition setup:

root@FATBOX:/tmp/log# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root               476.2M    456.0M     20.2M  96% /
devtmpfs                512.0K         0    512.0K   0% /dev
tmpfs                   247.7M    116.0K    247.6M   0% /tmp
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/mmcblk0p3            2.7G     50.2M      2.5G   2% /data

Solution

  • I figured out an alternative solution, which gets the job done, and ensures my apps continue running (added benefit).

    I schedule a process checker script to run every minute in cron:

    #!/bin/sh
    
    # Checking if /user/canpy/app.py is running
    ps | grep -v grep | grep /user/canpy/app.py
    if [ $? -eq 0 ]; then
        echo "/user/canpy/app.py is running."
    else
        # If it is not running, start it!
        echo "/user/canpy/app.py is not running. Starting it now"
        /data/venv/bin/python3.6 /user/canpy/app.py >/var/log/main_app_canpy.log 2>&1 &
    fi
    

    Here is the crontab located at /etc/crontabs/root:

    # Schedule check_running.sh to run every minute
    * * * * * sh /user/check_running.sh >/var/log/check_running_canpy.log 2>&1 &
    

    Cheers, Sean