Running startup scrpts

I am trying to run a startup script on my OpenWRT device. I went to /etc/rc.local and added:

logger -t rc-local "Running my service"
/usr/bin/python3 /root/myscript.py -m option1 -r option2 &

The problem is, that when I reboot the router and check the script, it is not running...

I had run ps | grep python and there is no process and also logread -e rc-local gives no output...

assume your custom commands were added before the exit 0 line ?

Yes, this is the whole rc.local:

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

. /lib/functions/gl_util.sh
remount_ubifs

logger -t rc-local "Running my service"
/usr/bin/python3 /root/myscript.py -m option1 -r option2 &

exit 0

Actually now I can see a log in logread. And after reboot I can see a process for a very short time:

ps | grep pyth
 6920 root     26020 R    /usr/bin/python3 /root/myscript.py -m option1 -r option2
13726 root      1108 S    grep pyth

and the it just dies. BUT - if I run it from terminal, it keeps running.

just for fun, install screen and run

screen "/usr/bin/python3 /root/myscript.py -m option1 -r option2" in rc.local

see if it works.

1 Like

Ha, that is a problem. opkg install screen returns:

Unknown package 'screen'.
Collected errors:
 * opkg_install_cmd: Cannot install package screen.

opkg update ?

Yeah, sorry, my mistake. Anyway, it is running under screen just fine:

ps | grep pyth
21915 root      1444 S    {screen} SCREEN /usr/bin/python3 /root/myscript.py -m option1 -r option2
21916 root     24776 S    /usr/bin/python3 /root/myscript.py -m option1 -r option2
23014 root      1108 R    grep pyth

screen -r to see if it's doing what it's supposed to, assuming there's actual output.

1 Like

Yes, screen -r gives me output and confirms everything works just fine.

you're not redirecting the output when running without screen, could be the issue, but i'm no expert in running stuff in the background, esp not python based stuff :wink:

Yes, I also suspect that. I tried to redirect output to /dev/null, but the problem persists...

Try a detached shell:

(/usr/bin/python3 /root/myscript.py -m option1 -r option2 )&

4 Likes

Or write to logger, something like:
(/usr/bin/python3 /root/myscript.py -m option1 -r option2 ) 2>&1 | logger -t $(echo $(basename $0) &

3 Likes

Hi, I tried both. The first one does not work (python dies after a while) and the second one also does not work.

I put another logger info after the command in rc.local (logger -t rc-local "My service activated"), so that I get the info when script is being invoked and when it should be activated.

In the first case I can see both logs (with logread -e rc-local), but in the second case it shows only the first log...

Unable to run more than 1 command from "Local Startup" input window in Luci - #5 by vgaetera