0

I working on openwrt and I want to run a python script on bootup. For this, I have written my script and added it to /etc/rc.local. After bootup, when i do ps xa, I can find the script running but nothing prints on screen. The steps I have followed are below -

bootpython.py - python script that needs to run on bootup

i=0

while(i<100):
        print("HELLO \n")
        i+=1

script.sh - Script added to rc.local

This script checks if there's wlan0 in ifconfig and only then, it runs the python code. After reboot, there's saved.txt file created and it has HELLO written in it. But /usr/bin/python3 /root/bootpython.py & doesn't print anything on screen. If i run the script without booting the system (/root/script.sh), it prints HELLO on screen. What am i missing here?

a=0

while [ $a -lt 100 ]
do
        if ( ifconfig | grep wlan0 )
        then
                a=`expr $a + 1`
                /usr/bin/python3 /root/bootpython.py > /root/saved.txt &
                /usr/bin/python3 /root/bootpython.py &
                break
        fi
done

/etc/rc.local

# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

/root/script.sh &

exit 0
Sourabrt
  • 2,126
  • 2
  • 8
  • 22
  • That's because the script output isn't linked to your terminal, it started in background by the system, even before you log in, and I suppose it's output isn't sent to anywhere, or maybe you can find it in `/var/log/messages` or `/var/log/boot.log` – Havenard Jun 20 '21 at 09:30
  • @Havenard: But then how do i print my output onto the terminal? – Sourabrt Jun 21 '21 at 02:51
  • 1
    I suppose the easiest way is to put it in `/root/.bashrc` instead of `/etc/rc.local`, so it will run in your terminal as you log in instead of running on system init, this way you should see the script's output. – Havenard Jun 21 '21 at 04:17
  • 1
    There's a way to run it in system init and then make it detect when the root log in and dump the output to it's `/dev/tty*` but that's a rabbit hole you'll probably want to avoid... – Havenard Jun 21 '21 at 04:19
  • 1
    @Sourabrt try this module: curses "https://docs.python.org/3/library/curses.html". – Victor Lee Jun 21 '21 at 07:12
  • 1
    As an aside, `if ( ifconfig | grep wlan0 )` should be `if ifconfig | grep -q wlan0` – tripleee Jun 21 '21 at 07:50

0 Answers0