Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Run a script upon entering runlevel 2

  1. #1
    Junior Member registered user
    Join Date
    Sep 2009
    Posts
    15

    Run a script upon entering runlevel 2

    Hello,

    I'm booting Knoppix 6.7.1 from DOK.
    The image on this media was created using a live CD.
    Then I changed syslinux.cfg in 6.7.1 and now knoppix enters runlevel 2 (text mode) by default.

    Upon entering this level I want to run my script.

    So I added:
    echo "My script"
    in /etc/rc.local

    But "My script" is not displayed.

    According to another thread in this forum I tried putting my script in :
    /opt/knx/knoppix/KNOPPIX/knoppix.sh

    But this does not work also.

    Can you help ?

    Thanks,
    Zvika.

  2. #2
    Junior Member registered user
    Join Date
    Sep 2009
    Posts
    15

    Solution: knoppix.sh

    Dear members,

    The script knoppix.sh should be put in the folder KNOPPIX (when the DOK is mounted under windows)

    Thanks,
    Zvika.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    20

    Inhibiting knoppix.sh

    Hi, Anyone know a way to inhibit execution of knoppix.sh with a boot command/cheatcode? This is useful when working on devices where you might not want your regular script to run.

  4. #4
    Moderator Moderator
    Join Date
    Nov 2010
    Location
    Germany/ Dietzenbach
    Posts
    1,124
    You cannot inhibit the script knoppix.sh, but you can decide within this script what to do.

    Append any cheatcode, for example
    Code:
    knoppix no_myscript
    In your script "knoppix.sh" test for existence of your cheatcode by
    Code:
    cat /proc/cmdline
    If the cheatcode is found, do nothing else; otherwise execute remaining of the script.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Good idea Werner, thanks for the suggestion!

    Here is my detailed explanation for novices, and a working code sample (tested on v6.7.1dvd), for those who wish to use this method:

    knoppix.sh is executed during startup by a block of code that can be seen at the end of /etc/init.d/knoppix-autoconfig. If one wishes to undertake a deep remastering, it is possible to modify this code to build a script-inhibiting cheatcode into the system.

    Alternatively, a conditional code block can be inserted at the start of knoppix.sh to parse /proc/cmdline, the kernel boot instructions, for a specified keyword, for example no_myscript. Use this keyword as you would a cheatcode, by typing knoppix no_myscript at the boot prompt, and the code shown below will suspend script execution; if the keyword is not entered, the script will execute as per normal.

    Code:
    string=$(cat /proc/cmdline);   # Assign output of 'cat' to variable 'string'.
       
    if [ "$string" != "${string/no_myscript}" ]; then   # Check for presence of keyword.
       echo "Stopping Script"
       exit
    else
       echo "Running Script"
    fi
    Note that knoppix.sh is not executed in Bash, but in a Bourne shell, so some more common Bash commands that check whether a variable contains certain text will not work properly; for instance, grep will not work, nor will [[ "$string" == *no_myscript* ]]. Hence my use of the clever method published by one ephemient on a thread at stackoverflow, and described by him as follows:

    ${var/search/replace} is $var with the first instance of search replaced by replace, if it's found (it doesn't change $var). If you try to replace [no_myscript] by nothing, and the string has changed, then obviously [no_myscript] was found.

  6. #6
    Moderator Moderator
    Join Date
    Nov 2010
    Location
    Germany/ Dietzenbach
    Posts
    1,124
    Note that knoppix.sh is not executed in Bash, but in a Bourne shell
    If you use within Knoppix the shebang-line
    Code:
    #!/bin/sh
    you'll get the Bash. '/bin/sh' is only a symlink to '/bin/bash'

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    I see, and appreciate your input; as a novice myself I welcome and encourage criticism and instruction from others who know their stuff.

    My comment was an inference based on the fact that certain alternative methods of checking for the presence of text in a string var worked in LXTerminal, but not in the lower runlevel root terminal at startup. Why is this the case?

    I noticed that the shebang-line in /etc/init.d/knoppix-autoconfig invokes #!/bin/busybox sh. Does knoppix.sh therefore run in Busybox/Almquist shell, rather than Bash or sh?

  8. #8
    Moderator Moderator
    Join Date
    Nov 2010
    Location
    Germany/ Dietzenbach
    Posts
    1,124
    While booting Knoppix only busybox is available (here '#!/bin/sh' is a symlink to busybox). After completion of bootprocess you can use Bash.

    Within the init-script of minirt.gz you can see, how KK solved the problem using the function "checkbootparam()"

    In the script knoppix.sh you can use a line like this:
    Code:
    cat /proc/cmdline | grep -q "no_myscript" && exit

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Yes, your code above works at boot, and is more elegant.

    My initial attempt with grep used a different syntax which worked in Bash but returned an error at boot, hence my incorrect statement that "grep will not work" in a post above; grep function is included in BusyBox and will indeed work at boottime.

  10. #10
    Senior Member registered user
    Join Date
    Sep 2006
    Posts
    801
    Quote Originally Posted by lapidu View Post
    Yes, your code above works at boot, and is more elegant.

    My initial attempt with grep used a different syntax which worked in Bash but returned an error at boot, hence my incorrect statement that "grep will not work" in a post above; grep function is included in BusyBox and will indeed work at boottime.
    Sure it is elegant! As regards busybox, I'm not so sure: As soon as main file system is mounted, I would think bash is available. So, Klaus K's reason for explicitly using /bin/busybox may, for example, be to have the same semantics during the whole init process. During minirt init, he has made only busybox available (other booting procedures stuff lots of core utilities into that image), and busybox does its things somewhat differently from standard gnu coreutils.

    Also, what commands busybox contains and not depends on the compilation. You may find that other versions can have more or fewer commands than this one, which I think may be the standard Debian version.

Page 1 of 2 12 LastLast

Posting Permissions

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