1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What does it take to get this script to run?

Discussion in 'Tomato Firmware' started by Bird333, Jan 15, 2013.

  1. Bird333

    Bird333 Network Guru Member

    Router: RT-N66U
    Firmware: Shibby tomato-K26USB-1.28.RT-N5x-MIPSR2-104-AIO-64K

    My other thread has disappeared. :) I have been trying for days now to get this entware startup script (rc.unslung) to run on this router. I still am not sure if this script is not running or if the transmission script that it starts is not running (if someone can help determine that I would be grateful). I have tried starting the script in various places in the gui with various 'sleep XX' commands and most of the time it doesn't start. I have created a script ('05-entware.autorun') and put it in the /opt/etc/config directory as this page suggests http://infodepot.wikia.com/wiki/TomatoUSB_-_Script_Name_Extensions and it still is not working. Here is the script I created to try and start the other script.
    Code:
    root@TomatoUSB:/opt/etc/config# cat 05-entware.autorun
    #!/bin/sh
    
    /opt/etc/init.d/rc.unslung start
    Here is the rc.unslung script that I am ultimately trying to run
    Code:
    root@TomatoUSB:/opt/etc/init.d# cat rc.unslung
    #!/bin/sh
    
    # Start/stop all init scripts in /opt/etc/init.d including symlinks
    # starting them in numerical order and
    # stopping them in reverse numerical order
    
    #logger "Started $0${*:+ $*}."
    
    ACTION=$1
    CALLER=$2
    
    if [ $# -lt 1 ]; then
        printf "Usage: $0 {start|stop|restart|reconfigure|check|kill}\n" >&2
        exit 1
    fi
    
    [ $ACTION = stop -o $ACTION = restart -o $ACTION = kill ] && ORDER="-r"
    
    for i in $(/opt/bin/find /opt/etc/init.d/ -perm '-u+x' -name 'S*' | sort $ORDER ) ;
    do
        case "$i" in
            S* | *.sh )
                # Source shell script for speed.
                trap "" INT QUIT TSTP EXIT
                #set $1
                #echo "trying $i" >> /tmp/rc.log
                . $i $ACTION $CALLER
                ;;
            *)
                # No sh extension, so fork subprocess.
                $i $ACTION $CALLER
                ;;
        esac
    done
    Help!
     
  2. jerrm

    jerrm Network Guru Member

    .autorun scripts go in the root of the drive.

    /???/etc/config files should have an extension of .fire, .wanup, .shut, and so on. Re-read the link you posted.
     
  3. Bird333

    Bird333 Network Guru Member

    Just so I'm clear, if the drive mounts on /opt then I need to put the script in /opt and not /?
     
  4. Bird333

    Bird333 Network Guru Member

    Tried the script (05-entware.autorun) in /opt. It didn't work. I couldn't try in '/' because it is a read-only file system.
     
  5. jerrm

    jerrm Network Guru Member

    .autorun scripts should go in the root of the partition, so assuming the root of the partition is mounted at /opt, then yes, it should go in /opt.

    I assume you have made the script executable (chmod +x)? Does it work from the command line?

    I personally haven't used .autorun scripts in a long time, placing any of my auto-magic scripts in /???/etc/config, so can't vouch if the latest releases properly run the script, but would think we would have heard about it if they were broken.

    In general, I shy away from the auto-magic scripts. I'd rather see a single line entry calling "/opt/???/my-script.sh" in the gui's firewall (or init, wan-up, shutdown) script section, just as a visual reminder a script is being run.
     
  6. Bird333

    Bird333 Network Guru Member

    Yes the script is executable. Yes it works from the command line. I have tried putting it in the USB 'run after mount' field, in the 'init' field and in wan-up. No place does it start consistently. Maybe 1 out of 5 reboots does it seem to start transmission. I guess I'll try it in the firewall gui, but somehow I just don't think it is gonna help.
     
  7. Bird333

    Bird333 Network Guru Member

    Ok the script runs from the firewall gui (well at least the 8 times I rebooted). What does that tell us about what is going on with this firmware?
     
  8. koitsu

    koitsu Network Guru Member

    It tells us absolutely nothing, because it's very difficult to troubleshoot this kind of thing without something like serial console or some kind of constant non-interrupted access where you can see what's going on immediately after init/busybox starts up.

    Running it via the CLI means $PATH may be different than during boot-up, so things like sort and printf in the script you used may resolve to something like /opt/whatever/bin/sort from the CLI rather than at boot-up they may resolve to /bin/sort. Get the picture?

    Also, the script you've shown does nothing other than run other scripts. So the problem may be one of those other scripts and not the one you pasted.

    Welcome to troubleshooting and scripting 101. :)
     
  9. Bird333

    Bird333 Network Guru Member

    Well here is the transmission script that gets started by the rc.unslung script.
    Code:
    root@TomatoUSB:/opt/etc/init.d# cat S88transmission
    #!/bin/sh
    
    ENABLED=yes
    PROCS=transmission-daemon
    ARGS="-g /opt/etc/transmission"
    PREARGS=""
    DESC=$PROCS
    PATH=/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    
    . /opt/etc/init.d/rc.func
    So you are telling me there is no way to figure out what is going on here? I don't want to run this from the firewall gui. It should work from the USB gui or at least the init. I would think the developer Shibby would want to find out why it's not working.
     
  10. jerrm

    jerrm Network Guru Member

    How are you mounting the drive? If automount is enabled, the .autorun script may be running before the binding to /opt occurs.

    create a 00.autorun like this:
    Code:
    #!/bin/sh
     
    log=/tmp/autorun.log
    echo `date` >> $log
    echo $0 $@  >> $log
    echo >> $log
    df >> $log
    echo >> $log
    echo path $PATH >> $log
    echo >> $log
    echo >> $log
    cat $log | logger -t autorun
    
    See if it runs consistently and what the of the logfile contents are. Note the log will be deleted with each boot.
     
  11. jerrm

    jerrm Network Guru Member

    Init is probably the wrong place - too many things including USB and WAN not necessarily working yet.

    It's not working for you - several boots tonight on a couple of different Shibby 102 boxes and autorun is working consistently here. If it were a general autorun issue I think we would have had more postings.
     
  12. Bird333

    Bird333 Network Guru Member

    Ok a couple of discoveries. Yes your script seems to consistently run. First I put the command '/opt/00.autorun' in the USB 'run after mounting' field not thinking about it should run automatically in the /opt directory so it was actually being run twice. This was the output
    Code:
    root@TomatoUSB:/tmp# cat autorun.log
    Wed Dec 31 18:00:53 CST 1969
    /opt/00.autorun /opt
     
    Filesystem          1K-blocks      Used Available Use% Mounted on
    /dev/root                8512      8512        0 100% /
    tmpfs                  127864      200    127664  0% /tmp
    devfs                  127864        0    127864  0% /dev
    /dev/sda2            755941400 232189800 485350580  32% /opt
     
    path /sbin:/bin:/usr/sbin:/usr/bin:/opt/sbin:/opt/bin
     
     
    Wed Dec 31 18:00:58 CST 1969
    /opt/00.autorun
     
    Filesystem          1K-blocks      Used Available Use% Mounted on
    /dev/root                8512      8512        0 100% /
    tmpfs                  127864      232    127632  0% /tmp
    devfs                  127864        0    127864  0% /dev
    /dev/sda2            755941400 232189800 485350580  32% /opt
     
    path /sbin:/bin:/usr/sbin:/usr/bin:/opt/sbin:/opt/bin
    The only difference is the top output (the one that got automatically run from /opt) is that it has a second '/opt' on the '/opt/00.autorun /opt' line and the other one doesn't have the second '/opt'. The second thing I discovered is that transmission started on a couple of occasions but I didn't have the 'rc.unslung' script added anywhere in the gui. It resides in /opt/etc/init.d with the S88transmission script. So scripts in that directory must be automatically run sometimes. It seems that it runs with the time is correct on the router. Both times when transmission started this was the output of the 00.autorun script
    Code:
    root@TomatoUSB:/tmp# cat autorun.log
    Tue Jan 15 23:39:32 CST 2013
    /opt/00.autorun /opt
     
    Filesystem          1K-blocks      Used Available Use% Mounted on
    /dev/root                8512      8512        0 100% /
    tmpfs                  127864      388    127476  0% /tmp
    devfs                  127864        0    127864  0% /dev
    /dev/sdb2            755941400 232189816 485350564  32% /opt
     
    path /sbin:/bin:/usr/sbin:/usr/bin:/opt/sbin:/opt/bin
    Notice the date and time. Anybody have any clues to what is happening here with this information?

    EDIT: Another thing I just noticed is that transmission is running when my USB drive is detected on /dev/sdb2 instead of sda2. That probably doesn't matter but I am just noting it.
     
  13. jerrm

    jerrm Network Guru Member

    Classic problem with the autorun scripts - you can't be sure when they are going to run.

    It looks like when the script runs early, some service the daemon depends on is not up yet. It could be bombing out just because of the time, maybe the WAN isn't up, maybe DNS isn't up, etc.

    A loop to test and see when things are ready before calling rc.unslung would probably be work, or just leave it in the firewall section or as a .fire script (or maybe wanup).

    I wonder why the init.d scipts are only being called sometimes, I wasn't aware that was ever the case. They don't get called when I test. Are you sure there is nothing in /opt/etc/config, GUI, or other .autorun scripts?

    I would think if they were being called by the system, they would be called consistently regardless of when the drive was mounted, even if transmission may not work because of dependencies if called too early.

    Try copying (cp -p) the 00.autorun script to /opt/etc/init.d/S50Test and see if it runs always, sometimes or never.
     
  14. Bird333

    Bird333 Network Guru Member

    Good call on the /opt/etc/config. I did still have the 05-entware.autorun script still there from earlier testing. Maybe that is what was starting transmission occasionally. I have deleted it now. I'll put your 00.autorun script in init.d to test. The script doesn't work in WANUP. The firewall section is the only place in the gui I have found where it works every time. Do you know how to create the loop you mentioned?
     
  15. Bird333

    Bird333 Network Guru Member

    Well I just rebooted after removing that script and transmission started go figure. It does seem like /opt/etc/init.d scripts are run sometimes. I'll put your script in there now to test.
     
  16. Bird333

    Bird333 Network Guru Member

    Your S50test script runs every time from the init.d directory. Here is the log output
    Code:
    root@TomatoUSB:/tmp# cat autorun.log
    Wed Dec 31 18:00:52 CST 1969
    /opt/etc/init.d/S50Test start
     
    Filesystem          1K-blocks      Used Available Use% Mounted on
    /dev/root                8512      8512        0 100% /
    tmpfs                  127864      144    127720  0% /tmp
    devfs                  127864        0    127864  0% /dev
    /dev/sda2            755941400 232189856 485350524  32% /opt
     
    path /sbin:/bin:/usr/sbin:/usr/bin:/opt/sbin:/opt/bin
    Where is the word 'start' coming from in the log for your script? I am starting to think the transmission script is running but transmission is not starting most of the time for some reason. There is a 'stop_transmission.sh' script that is always in the /tmp directory. I think it might somehow be created by the S88transmission in init.d. Here is what it looks like
    Code:
    root@TomatoUSB:/tmp# cat stop_transmission.sh
    #!/bin/sh
    killall -KILL transmission-daemon
    logger "Transmission daemon successfully stoped"
    sleep 2
    /usr/bin/btcheck addcru
     
  17. Bird333

    Bird333 Network Guru Member

    More weirdness. :) I switched my USB drive to my E3000 where transmission runs every time. Now the 'S50test' script you had me put in /opt/ete/init.d doesn't run. Transmission starts fine as usual. My usb drive on this router initially mounts on /tmp/mnt/opt and then I have this command running in the init gui
    Code:
    /bin/mount -o bind /tmp/mnt/opt /opt
    If I add my drive info to the /etc/fstab file and issue this command
    Code:
    nvram setfile2nvram /etc/fstab
    and
    Code:
    nvram commit
    to make it permanent then reboot; my usb drive mounts directly to /opt. When this happens, your S50test script runs but transmission doesn't start. Also there is a 'start_transmission.sh' script in /tmp that I don't see when using my N66u. Here are its contents
    Code:
    #!/bin/sh
    sleep 10
    if [ ! -d "/tmp/mnt/opt/data/torrents/.incomplete" ]; then
    mkdir /tmp/mnt/opt/data/torrents/.incomplete
    fi
    if [ ! -d "/tmp/mnt/opt/data/torrents/.settings" ]; then
    mkdir /tmp/mnt/opt/data/torrents/.settings
    fi
    mv /tmp/settings.json /tmp/mnt/opt/data/torrents/.settings
    rm /tmp/mnt/opt/data/torrents/.settings/blocklists/*
    wget http://list.iblocklist.com/?list=bt_level1 -O /tmp/mnt/opt/data/torrents/.settings/blocklists/level1.gz
    gunzip /tmp/mnt/opt/data/torrents/.settings/blocklists/level1.gz
    EVENT_NOEPOLL=1; export EVENT_NOEPOLL
    /tmp/mnt/opt/bin/transmission-daemon -g /tmp/mnt/opt/data/torrents/.settings
    logger "Transmission daemon successfully started"
    sleep 2
    /usr/bin/btcheck addcru
    It seems if a script is saved with the USB drive mounted in a certain way it won't work if the drive is mounted a different way.
     
  18. jerrm

    jerrm Network Guru Member

    What I think may be happening...

    start_transmission.sh is only generated when the bittorrent client is checked enabled in the gui.

    On the E3000 it looks like you have transmission enabled in the gui AND have set the custom paths to the automount paths (/tmp/mnt/...) for the /opt versions of the executables and data directories. The generated-from-the-gui start_transmission.sh is starting transmission and not the init.d scripts. When you put the mount in fstab, the partition was no longer auto-mounted and "/tmp/mnt/opt/bin/transmission-daemon" is no longer there.

    The init.d test script is a question though. If I had to guess, I'd say there is still a script somewhere calling rc.unslung that wasn't cleaned up. The init.d script are not run on my units whether I auto mount or mount via fstab. I've never seen anywhere that tomato calls /opt/etc/init.d on it's own, but I could be wrong.
     
  19. Bird333

    Bird333 Network Guru Member

    I'll check my E3000 transmission gui but I thought it was disabled. I'll check for rc.unslung but I'm pretty sure I got rid of all of them unless the firmware is ignoring the '#' I am putting in front of the commands.:)
    :)You know that still doesn't explain your S50test script. It is only in the init.d directory and it gets run. So tomato must be running scripts there.
     
  20. jerrm

    jerrm Network Guru Member

    If rc.unslung is being called, then it is calling the the S50test script. The fact the "start" parameter is there suggests the test script is being called from rc.unslung.

    If tomato is calling scripts in the directory, then it is only calling S* scripts or only rc.unslung, otherwise the test would run twice. I doubt it would be that specific, but wouldn't completely rule it out.
     
  21. Bird333

    Bird333 Network Guru Member

    What line in your script is responsible for outputting 'start' and '/opt' in the autorun.log file?
     
  22. Bird333

    Bird333 Network Guru Member

    What line in your script is responsible for outputting 'start' and '/opt' in the autorun.log file?
     
  23. jerrm

    jerrm Network Guru Member

    Code:
    echo $0 $@  >> $log
    $0 is the name of the current script as it was called.
    $@ contains all the command line parameters.

    The .autorun scripts are passed the mount point as the 1st parameter.

    rc.unslung passes the parameter it was called with ("start" in this instance) to the scripts it finds in init.d.
     
  24. Bird333

    Bird333 Network Guru Member

    So does that mean '/opt' was used to call the script earlier in the thread?
     
  25. jerrm

    jerrm Network Guru Member

    Yes, the mount point of the partition was at /opt, so when tomato called 00.autorun it appended the mount point as the first parameter.

    At this point there are multiple options that should work:
    1: Enable the Bittorent GUI and set the paths appropriately - easiest, but probably least portable if you switch from shibby.
    2: Use a (an?) .autorun script with either a loop to check system status or a sufficiently long but dumb sleep. My guess is if the time has been set you will be good, but expect some trial and error. If the time has been set, then you know the WAN is up and DNS is working.
    3: From the gui init, wanup, or usbmount sections, call a non-.autorun version of a script that does the same as option 2.
    4: You know from testing it works from the firewall script point, call the script from the firewall script gui or a .fire config script.

    All of the above are viable and acceptable. Any three people will probably have three different opinons on which they would choose and why.

    You're wanting something to work that I have never seen documented as a feature. Even if there is something in the code that is sometimes doing what you want, it isn't reliable. It's not worth fighting, go with what you know will work.

    EDIT: Init and wanup are probably both too soon to reliably keep all your code in a drive hosted script. They could still work, but you would need to have enough script in the gui to at least make sure the drive is mounted and your script is available.
     
  26. Bird333

    Bird333 Network Guru Member

    I want to have all my entware scripts to run. Getting rc.unslung starting reliably seems to be the way. Shouldn't transmission work without turning on the gui?
     
  27. jerrm

    jerrm Network Guru Member

    Barring typos (I haven't tested), this should work placed at the end of the gui init, wanup, or firewall sections or alternatively into an .autorun script. It may start things a little later than you wish, it checks for the sysup flag which means the system startup is otherwise pretty much complete.

    Code:
    #!/bin/sh
     
    A=30
    while [ $A -gt 0 ] ; do
      A=$(( $A - 1 ))
      if [ -f /var/notice/sysup ] ; then  break; fi
      sleep 10
    done
    /opt/etc/init.d/rc.unslung start
    
     
  28. Bird333

    Bird333 Network Guru Member

    Thanks for the script. It doesn't work all the time in 'init' but I put it in /opt/etc/config as a '.usbmount' script and it seems to be working.
     
  29. koitsu

    koitsu Network Guru Member

    The problem with putting anything under Init (and sometimes even WAN Up) that uses USB is that most of the USB enumeration hasn't completed by the time the script gets started. There's lots of posts about this. That's what the "sleep" is supposed to help solve, but the value given to "sleep" is flaky at best and I don't recommend it -- people will say "it works fine with 20", then a week later find that "it's no longer working" when in fact it's just pure chance whether or not 20 seconds is enough time. They change it to "30" then suddenly it's fine again, then 2 weeks later it doesn't work, rinse lather repeat.

    This is why why the automount stuff is a correct/proper solution -- that way, a script gets run automatically when the filesystem is actually mounted, which is (obviously) way past USB bus enumeration (else how would you mount a filesystem on a USB device if the bus hasn't been fully enumerated? :) ). It guarantees the underlying filesystem is already accessible/up/available.

    There are other caveats to the Init/Firewall/WAN Up stuff pertaining to timing as well, but that's outside of this thread topic.
     
  30. Bird333

    Bird333 Network Guru Member

    Well I had the line in the 'run after mounting' box in the 'USB and NAS' screen but it still didn't work reliably. I figured the USB drive would be up at that point since it does say 'run after mounting' :) but it still didn't always work.
     
  31. jerrm

    jerrm Network Guru Member

    If it was a .autorun script or the in the "run after mount" gui prompt then the script was being executed. The problem when calling from these points was the drive was mounted and the scripts called transmission-daemon before other network services the daemon needs were up . The transmission daemon not starting is not the same as the script that calls it not running.
     
    koitsu likes this.
  32. leandroong

    leandroong Addicted to LI Member

    Tomato Firmware 1.28.0000 MIPSR1-104 K26 USB BTgui-VPN
    No problem with entware. My router play music on boot-up automatically and no issue with samba or transmission.
    Try putting this init script that I learned from tomatousb.org from ray
    Code:
    echo '#!/bin/sh
    # So.....lets just sleep until the sysup signal or we've been up for 5 minutess.
    A=600
    while [ $A -gt 0 ] && [ $(cut -f1 -d"." /proc/uptime) -lt 300 ]  ; do
    A=$(( $A - 1 ))
    if [ -f /var/notice/sysup ] ; then  break; fi
    sleep 1
    done
     
    
    Hope this helps, I even have my USB camera driver loaded.

     
  33. jerrm

    jerrm Network Guru Member

    Be aware the gui "run after mounting" and .usbmount scripts can be called multiple times. They are called each time a usb partition is mounted. It looks like you only have a single partition currently, so it probably doesn't matter right now, but be aware of it if you add another partition or drive your scripts need to gracefully handle being called repeatedly.

    Most startup scripts properly using the Entware rc.unslung/rc.func model will probably be OK, but I would probably add a check before it got that far.

    The usb mount scripts are passed the current partition mount point as the 1st parameter (even the gui generated "run after mounting" script) which is an easy thing to use as a limiting condition.
     
  34. jerrm

    jerrm Network Guru Member

    I think that's where my snippet started life as well, although my version has evolved some, and what I gave the Bird333 was a de-evolved, simplified version.

    But just to be sure - are you using this simply as a pause at the start of the script, and then going on to call your startup processes in the script, or are the Entware init.d processes being called directly from tomato somehow? The Bird333 seems to think the init.d scripts were sometimes being called automatically, which I have never seen.
     
  35. leandroong

    leandroong Addicted to LI Member

    Purpose of the script is to wait the system to stabilize. No, I dont have anything under "run before mounting"
     
  36. leandroong

    leandroong Addicted to LI Member

    You can edit the init script I post to add script that you want to be run after router system stabilize.
     
  37. jerrm

    jerrm Network Guru Member

    OK, so you're init script looks something like below?
    Code:
    #!/bin/sh
    # So.....lets just sleep until the sysup signal or we've been up for 5 minutess.
    A=600
    while [ $A -gt 0 ] && [ $(cut -f1 -d"." /proc/uptime) -lt 300 ]  ; do
    A=$(( $A - 1 ))
    if [ -f /var/notice/sysup ] ; then  break; fi
    sleep 1
    done
     
    #System initialized, now start my services
    /opt/mypath/mysambascript.sh
    /opt/mypath/mycamerascript.sh
    /opt/mypath/mytranmissionscript.sh
     
  38. leandroong

    leandroong Addicted to LI Member

    #!/bin/sh
    # So.....lets just sleep until the sysup signal or we've been up for 5 minutess.
    A=600
    while [ $A -gt 0 ] && [ $(cut -f1 -d"." /proc/uptime) -lt 300 ] ; do
    A=$(( $A - 1 ))
    if [ -f /var/notice/sysup ] ; then break; fi
    sleep 1
    done
    /opt/mpd-start.sh
    /opt/camera-start.sh
     
  39. jerrm

    jerrm Network Guru Member

    OK, that's what I thought. You are explicitly starting any processes you need. Tomato is not calling the /opt/etc/init.d scripts itself in some auto-magic way.
     
  40. leandroong

    leandroong Addicted to LI Member

    yes, you maybe right. Regarding transmission start, Shibby FW takes care of it.
     
  41. Bird333

    Bird333 Network Guru Member

    I don't think this anymore. :) I had a hidden '.autorun' script that I think was causing the script to run.
     
  42. Bird333

    Bird333 Network Guru Member

    Why don't you just start 'rc.unslung' instead of starting each script separately?
     

Share This Page