How to call a Python script in a bash script?

Viewed 42

I've a simple script in /etc/init.d, named example (I'm on OpenWrt):

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
 
START=10
STOP=15
 
start() {        
        mkdir /tmp/test
}                 
 
stop() {          
        echo stop
        # commands to kill application 
}

I'm capable to start it at the boot (I verify that the test directory is created in tmp). But how to call a python program located in /mnt? I've tried:

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
 
START=10
STOP=15
 
start() {        
        python3 /mnt/program.py
}                 
 
stop() {          
        echo stop
        # commands to kill application 
}

but I had to reset the device because it couldn't boot...Why? What am I doing wrong?

2 Answers

Verify they are present before running

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
 
START=10
STOP=15
 
start() {
        if type python3
        then
            if [ -r /mnt/program.py ]
            then
                python3 /mnt/program.py # or use /path/to/python3
            else
                echo "error" >&2
            fi
        else
            echo "no python3" >&2
        fi
}                 
 
stop() {          
        echo stop
        # commands to kill application 
}

Check your python installation and python path, to extract which binary to call, either python or python3. You don't need functions start(), stop() in a bash script.

#!/bin/bash
python /mnt/program.py
Related