Results 1 to 5 of 5

Thread: seemingly harmless script halting startup

  1. #1
    Join Date
    May 2009
    Location
    Australia
    Beans
    110
    Distro
    Ubuntu 10.04 Lucid Lynx

    seemingly harmless script halting startup

    Hey linux friends,

    I have a small 'server' at home that I use primarily as an NFS/webcache/seedbox/toy.

    I also run boinc as it spends most of its time fairly idle. It has an intel core 2 duo and I set boinc to use 1 core. So as to keep it responsive i wrote a script to monitor its 5 minute load average and suspend boinc if it goes above 1.5 and resume it if it falls below this.


    Code:
    #!/bin/bash
    
    log=/var/log/loadWatchDog
    date >> $log; echo "Beggining scrpt" >> $log
    threshold=1.49
    last=""
    
    while [ 1 ]; do
      sleep 5m
      loadavg=`uptime | awk '{print $10}'| awk -F \, '{print $1}'`
    
      result=`expr $loadavg \> $threshold`
      if [[ "$result" -eq "1" ]] && [[ $last != "off" ]] ; then
        date >> $log; echo "stopping" >> $log
        boinccmd --set_run_mode never
        last="off"
      fi
      result=`expr $threshold \> $loadavg`
      if [[ "$result" -eq "1" ]] && [[ $last != "on" ]]; then
        date >> $log; echo "starting" >> $log
        boinccmd --set_run_mode always
        last="on"
      fi
    done
    exit 0
    and i set it to run at startup by copying it into the /etc/init.d/ directory and running
    Code:
    sudo update-rc.d loadWatchDog.sh defaults
    When it is powered on and pluged into a monitor it shows the starting services but seems to get stuck at starting boinc (squid also appears to not be running) never reaching the login prompt, i can however ssh in. This problem goes away when i remove my script.

    Anyone have any ideas what i've done wrong?

  2. #2
    Join Date
    Sep 2009
    Location
    United States
    Beans
    113
    Distro
    Ubuntu

    Re: seemingly harmless script halting startup

    Your script has an infinite loop (obviously by design), but since it's invoked in init.d at startup, Linux waits for it to finish executing before displaying the login prompt... and it never finishes. You need to daemonize the script, which I think you do by forking to a new process and exiting, but I'll let somebody more bash-script-savvy than I tell you how to do that.

  3. #3
    Join Date
    May 2009
    Location
    Australia
    Beans
    110
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: seemingly harmless script halting startup

    Quote Originally Posted by WinstonChurchill View Post
    since it's invoked in init.d at startup, Linux waits for it to finish executing before displaying the login prompt
    Thats interesting i was not aware of that. Thank you Winston .

    Quote Originally Posted by WinstonChurchill View Post
    You need to daemonize the script, which I think you do by forking to a new process and exiting, but I'll let somebody more bash-script-savvy than I tell you how to do that.
    Is that at all possible in bash (i'v messed with fork() and execvp() in C) would simply calling another another bash script with an ampersand then exiting the first script work?

    Anyone out there with more information on making a more solid deamon or pointers in the right out there?

  4. #4
    Join Date
    Jun 2010
    Beans
    181

    Re: seemingly harmless script halting startup

    Quote Originally Posted by chrisbay90 View Post
    Is that at all possible in bash (i'v messed with fork() and execvp() in C) would simply calling another another bash script with an ampersand then exiting the first script work?

    Anyone out there with more information on making a more solid deamon or pointers in the right out there?
    Yes, it is possible, using such method:
    Code:
    (eval "exec <cmd_or_script_here>" &)
    exec will terminate eval forked in subshell - tricky, but works fine.

    You can look for examples in deamon scripts that are already installed

  5. #5
    Join Date
    May 2009
    Location
    Australia
    Beans
    110
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: seemingly harmless script halting startup

    Quote Originally Posted by tomazzi View Post
    Yes, it is possible, using such method:
    Code:
    (eval "exec <cmd_or_script_here>" &)
    exec will terminate eval forked in subshell - tricky, but works fine.

    You can look for examples in deamon scripts that are already installed
    I had made an attempt at checking some of the other deamon scripts but as evident by my above script i am a woeful noob at bash and couldn't make much sense of what to look for.

    Thanks a lot Tom, that's an excellent point in the right direction and should get me well under way.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •