Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Shellcmd that doesn't block (non-blocking) and allows boot to continue

    Development
    3
    9
    2.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • luckman212L
      luckman212 LAYER 8
      last edited by

      I'm running 22.01 dev snapshots on a 6100 and wanted to use the Shellcmd package to execute a script at boot that fixes the LEDs. So it needs to wait ~30s for the boot to complete otherwise the setting gets trampled by the led_normalize() at the bootup complete stage of the main rc script.

      I tried various invocations like

      (sleep 30 ; echo 0 >/dev/led/blue3; )&
      #or
      /bin/sh -c 'sleep 30; echo 0 >/dev/led/blue3' &
      #or
      /root/my-led-setting-script.sh & 
      

      None of those worked- all of them blocked and halted the boot until the script finished. Is this a limitation of the way the Shellcmd package (or php?) calls out to the shell? I ended up punting and using a script in /usr/local/etc/rc.d/ but even the Netgate docs recommend not doing that.

      I searched and didn't find much about this, except for r/PFSENSE: Non-blocking shellcmd? —which didn't get a definitive answer.

      GertjanG 1 Reply Last reply Reply Quote 0
      • GertjanG
        Gertjan @luckman212
        last edited by Gertjan

        @luckman212

        See here.

        Now you know the PHP exec() is used, you can, look her https://www.php.net/manual/fr/function.exec.php and try out what has been proposed over there.

        @luckman212 said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:

        a script in /usr/local/etc/rc.d/ but even the Netgate docs recommend not doing that.

        But they mentioned it ;)
        That method give you the most control.

        No "help me" PM's please. Use the forum, the community will thank you.
        Edit : and where are the logs ??

        luckman212L 2 Replies Last reply Reply Quote 0
        • luckman212L
          luckman212 LAYER 8 @Gertjan
          last edited by

          @gertjan Thanks. I'll paste the important bit below to save anyone else a click:

          0c97ac3f-c801-478c-b971-458164c09505-CleanShot 2021-11-29 at 13.28.03@2x.png

          so I assume maybe the fix would be to make the shellcmd something like /bin/sh -c 'sleep 30; echo 0 >/dev/led/blue3' >/dev/null

          1 Reply Last reply Reply Quote 0
          • luckman212L
            luckman212 LAYER 8 @Gertjan
            last edited by

            Still can't get it to work 😕

            If I enter an interactive php shell with php -a and then run

            exec("/bin/sh -c '/bin/sleep 5; echo 1 >/dev/led/amber3' >/dev/null &");
            

            it does return control immediately and also turns on the LED after 5s. So that works.

            But, the same command in the Shellcmd package doesn't work. I thought maybe it was because the string gets escaped e.g. > becomes >, but when I tested a little patch, adding htmlspecialchars_decode() inside the exec() it did not change anything... 🤷

            GertjanG 1 Reply Last reply Reply Quote 0
            • GertjanG
              Gertjan @luckman212
              last edited by

              @luckman212

              What about placing the commands in a script file "test.sh" :

              #!/usr/local/bin/bash
              exec("/bin/sh -c '/bin/sleep 5; echo 1 >/dev/led/amber3' >/dev/null &");
              

              Make the script file executable with

              chmod +x test.sh
              

              and then use

              /root/test3.sh
              

              as the early start command ?

              No "help me" PM's please. Use the forum, the community will thank you.
              Edit : and where are the logs ??

              luckman212L 1 Reply Last reply Reply Quote 0
              • luckman212L
                luckman212 LAYER 8 @Gertjan
                last edited by

                @gertjan The top code snippet you wrote there has the wrong shebang (exec() is php, not bash). But aside from that, this is basically what I've done—put the commands in a script at /usr/local/etc/rc.d/

                The problem with that is it requires manually copying the scripts, making sure they are backed up/restored etc. It would be nice to be able to use Shellcmd since everything is contained within the config.xml...

                GertjanG 1 Reply Last reply Reply Quote 0
                • GertjanG
                  Gertjan @luckman212
                  last edited by

                  @luckman212 said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:

                  The top code snippet you wrote there has the wrong shebang (exec() is php, not bash).

                  The PHP exec() function executes a command.
                  Not a binary command like 'sleep' or '/bin/sleep' (the command + path) but a command shell script file.
                  The 'language interpreter' of the script files is .... not PHP. It's bash. So the sheban is bash.

                  @luckman212 said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:

                  The problem with that is it requires manually copying the scripts, making sure they are backed up/restored etc. It would be nice to be able to use Shellcmd since everything is contained within the config.xml...

                  I understand. It would be best if everything was self contained in the config.xml.

                  I'm using several self written script files - PHP, bash, perl, python, that I placed in the /root/ folder - and should be put there manually when I need to rebuild the system.
                  Rebuilding my pfSense is something I do onces in a decade, or so. So I installed the Notes package (a must have) where I put the files, with comments, etc, so I will fin things when needed, in 2030 or so.

                  I use also

                  8f261ac9-42b5-49c7-8bc1-ac3bf568cd3d-image.png

                  No "help me" PM's please. Use the forum, the community will thank you.
                  Edit : and where are the logs ??

                  JeGrJ 1 Reply Last reply Reply Quote 0
                  • JeGrJ
                    JeGr LAYER 8 Moderator @Gertjan
                    last edited by JeGr

                    @gertjan said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:

                    Rebuilding my pfSense is something I do onces in a decade, or so. So I installed the Notes package (a must have) where I put the files, with comments, etc, so I will fin things when needed, in 2030 or so.

                    Why not install and use the "Filer" package? I mean that's what it's there for, isn't it? ;)
                    Also it saves everything in config.xml for later use and restores if after a new installation. So no need for manually backing up and writing every script down in the Notes section?

                    Also filer and shellcmd got pretty well hand-in-hand :)

                    Cheers
                    \jens

                    Don't forget to upvote 👍 those who kindly offered their time and brainpower to help you!

                    If you're interested, I'm available to discuss details of German-speaking paid support (for companies) if needed.

                    luckman212L 1 Reply Last reply Reply Quote 1
                    • luckman212L
                      luckman212 LAYER 8 @JeGr
                      last edited by

                      @jegr Never used the backup or filer packages although I've always been vaguely aware of them. Somehow I thought they were unmaintained for years and broken on recent versions of pfSense but I guess that's wrong. I'll check it out (as soon as pkg is fixed 😳 )

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post
                      Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.