Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    [DSF Extension] Exec On MCode (was: Shutdown SBC)

    Scheduled Pinned Locked Moved
    DSF Development
    22
    117
    10.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.
    • wilrikerundefined
      wilriker @Greg_the_maker
      last edited by wilriker

      @Greg_the_maker I have to correct myself. In case of an error it will indeed return the output of stdout and stderr combined as an error message. But since we are in short-lived world things can be changed quickly and thus:

      Release v5.1.2

      This release adds a new flag -returnOutput to be able to send the collected output of a successful run back to DCS. Prior this was only the case if the execution of the command failed.
      Note that this cannot be combined with -execAsync.

      Download

      As usual get it from GitHub Releases page.

      Manuel
      Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
      with probably always latest firmware/DWC (incl. betas or self-compiled)
      My Tool Collection

      Greg_the_makerundefined oozeBotundefined 2 Replies Last reply Reply Quote 4
      • Greg_the_makerundefined
        Greg_the_maker @wilriker
        last edited by

        @wilriker Fantastic! Works exactly as desired. Have my upvote!

        1 Reply Last reply Reply Quote 0
        • oozeBotundefined
          oozeBot @wilriker
          last edited by

          @wilriker Could you please provide a simple example of using the -returnOutput flag? Thanks

          wilrikerundefined 1 Reply Last reply Reply Quote 0
          • wilrikerundefined
            wilriker @oozeBot
            last edited by wilriker

            @oozeBot said in [DSF Extension] Exec On MCode (was: Shutdown SBC):

            @wilriker Could you please provide a simple example of using the -returnOutput flag? Thanks

            Simple example:

            execonmode -mCode 7777 -command "whoami" -returnOutput
            

            will not just confirm successful completion of the command but as part of this return the output of whoami (that returns the user execonmcode is started as, so probably root) so you can see it e.g. in DWC.

            Manuel
            Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
            with probably always latest firmware/DWC (incl. betas or self-compiled)
            My Tool Collection

            oozeBotundefined 2 Replies Last reply Reply Quote 0
            • oozeBotundefined
              oozeBot @wilriker
              last edited by

              @wilriker Thanks. So the return is echo'ed in DWC, correct? Could the return value be evaluated within conditional gCode?

              garethkyundefined 1 Reply Last reply Reply Quote 0
              • garethkyundefined
                garethky @oozeBot
                last edited by garethky

                @oozeBot said in [DSF Extension] Exec On MCode (was: Shutdown SBC):

                @wilriker Thanks. So the return is echo'ed in DWC, correct? Could the return value be evaluated within conditional gCode?

                I was thinking about this today. Currently there are no variables so there is nothing to write the result back to. (there is a result int from the last code but it can't contain other data types) I think we might have to wait for variables and then we could write the value back to a global.

                When that happens... it should be possible to set up an MCode that stores and retrieves values from a file on the SD card. So we could do what M500 does but with key/value semantics of M409:

                M7500 K"some.key" V"The value"
                M7500 K"some.other.key" V0.05
                

                and then get them back in a global with:

                M7500 K"some.key"
                echo {global.M7500_value}    ; or whatever you want to name it!
                

                and stored on disk as JSON:

                "some": {
                    "key": "The Value"
                    "other": {
                        "key": 0.05
                    }
                }
                
                1 Reply Last reply Reply Quote 1
                • EMRosaundefined
                  EMRosa
                  last edited by

                  Great work! This is incredibly useful and appreciated!

                  Is there a way to pass quotes through the -command argument?
                  For example:

                  execonmcode --command "sudo pkill -fx "blah, blah, blah"" -mCode 7720 -execAsync
                  

                  I've tried single quotes, escape characters, and changing it to an argument passed through the m-code command;

                  execonmcode --command "sudo pkill -fx %F" -mCode 7720 -execAsync
                  

                  and then tried from DWC:

                  M7720 F"python3 script.py"
                  

                  but the full argument gets lost. I foolishly tried double quotes and DSF crashed. haha.

                  Please, forgive my inexperience. There is probably a simple answer to this, but it seems to escape my googling ability.

                  wilrikerundefined 2 Replies Last reply Reply Quote 0
                  • wilrikerundefined
                    wilriker @EMRosa
                    last edited by

                    @EMRosa Quick answer is: this is currently not implemented.
                    I'll think about adding it, though.

                    Manuel
                    Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
                    with probably always latest firmware/DWC (incl. betas or self-compiled)
                    My Tool Collection

                    EMRosaundefined 1 Reply Last reply Reply Quote 0
                    • ukwebbundefined
                      ukwebb
                      last edited by

                      followed your video and got it installed and working in 5 minutes - superb work - thanks

                      1 Reply Last reply Reply Quote 0
                      • EMRosaundefined
                        EMRosa @wilriker
                        last edited by

                        @wilriker thanks for the quick reply! No worries; at least it wasn’t something related to my Linux-literacy (or lack thereof). Haha. Your work and efforts on this implementation are inspirational.

                        1 Reply Last reply Reply Quote 0
                        • wilrikerundefined
                          wilriker @EMRosa
                          last edited by wilriker

                          @EMRosa said in [DSF Extension] Exec On MCode (was: Shutdown SBC):

                          I've tried single quotes, escape characters, and changing it to an argument passed through the m-code command;

                          execonmcode --command "sudo pkill -fx %F" -mCode 7720 -execAsync
                          

                          I thought about this some more and actually the above should work. Though probably as you use -fx on pkill this has to match the full commandline, i.e. you also have to specify the path to python3, so you will probably need use the following command:

                          M7720 F"/usr/bin/python3 script.py"
                          

                          EDIT: to find the full commandline while your script.py is running use

                          $ pgrep -fa "python3 script.py"
                          

                          This should give you the PID as well as the full command-line of your process.

                          Manuel
                          Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
                          with probably always latest firmware/DWC (incl. betas or self-compiled)
                          My Tool Collection

                          1 Reply Last reply Reply Quote 0
                          • EMRosaundefined
                            EMRosa
                            last edited by

                            Thanks so much for the assist! I’ll try that, but I was using the exact process as listed from

                            ps -aux | grep python3

                            This lists the process as “python3 location/to/script/script.py”.

                            In my case, I was able to put the command line call in another python script and just call it that way. I just wasn’t sure if it would be helpful (or necessary) for execonmcode to be able to send quoted arguments. (Or more likely, if I was doing it wrong. Haha)

                            I’ll let you know if adding the path to python helps.

                            wilrikerundefined 1 Reply Last reply Reply Quote 0
                            • wilrikerundefined
                              wilriker @EMRosa
                              last edited by

                              @EMRosa Since you are using pkill you need to use pgrep to find the required command line (it's the same binary that acts different based on which name it is called with but shares common features otherwise). ps is a different tool that uses (and prints) a different format usually dropping the path to the executable. I think there is a flag to ps to add it but I don't know that off-hand.

                              Also quotes are not necessary for execonmcode. Quotes are a concept of shells (sh, bash, zsh, fish, etc.) to provide means to human users to provide command arguments containing spaces. If an application runs another application they usually can provide arguments containing spaces without the need for quotes.

                              Manuel
                              Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
                              with probably always latest firmware/DWC (incl. betas or self-compiled)
                              My Tool Collection

                              1 Reply Last reply Reply Quote 0
                              • wilrikerundefined
                                wilriker
                                last edited by

                                Release 5.2.0

                                I am pleased to announce release of execonmcode 5.2.0.

                                This release uses the latest version of godsfapi and makes use of the filters option in InterceptConnection, i.e. it will only receive those codes the user is interested in. This will significantly lower the the required resources e.g. while running a job.

                                As usual the binaries can be found at GitHub Releases page.

                                Manuel
                                Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
                                with probably always latest firmware/DWC (incl. betas or self-compiled)
                                My Tool Collection

                                oozeBotundefined 1 Reply Last reply Reply Quote 2
                                • oozeBotundefined
                                  oozeBot @wilriker
                                  last edited by

                                  @wilriker at what point in the bootup cycle is execonmcode ready? I created a custom mCode that I'd like to call at the beginning of config.g, but it says it's unsupported. If I call it separately, or at the very end of config.g, it works correctly.

                                  Any thoughts?

                                  wilrikerundefined 1 Reply Last reply Reply Quote 0
                                  • wilrikerundefined
                                    wilriker @oozeBot
                                    last edited by

                                    @oozebot I think that primarily this depends on when execonmcode is started relative to startup of DCS. Or maybe DCS will even delay accepting API clients until a certain amount of startup tasks have been processed. @chrishamm should be able to give more insights on that.

                                    Manuel
                                    Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
                                    with probably always latest firmware/DWC (incl. betas or self-compiled)
                                    My Tool Collection

                                    chrishammundefined 1 Reply Last reply Reply Quote 0
                                    • chrishammundefined
                                      chrishamm administrators @wilriker
                                      last edited by

                                      @wilriker If ExecOnMCode is installed as a DSF plugin (will be officially supported in 3.3), it's started by DuetPluginService which is started by multi-user.target. Note that v3.3 will introduce /sys/dsf-config.g as well which is executed as soon as all the enabled plugins have been started.

                                      Previously plugins were started by DCS, so by basic.target AFAIR.

                                      @oozeBot Either move your custom codes to dsf-config.g if you're on pre-3.3 and/or add a delay to the end of your config file to make sure ExecOnMCode has been fully started before they're executed.

                                      Duet software engineer

                                      oozeBotundefined wilrikerundefined 2 Replies Last reply Reply Quote 0
                                      • oozeBotundefined
                                        oozeBot @chrishamm
                                        last edited by

                                        @chrishamm @wilriker - thank you both. We are just about to begin testing the 3.3 betas. This may need to be postponed until after it is released.

                                        1 Reply Last reply Reply Quote 0
                                        • wilrikerundefined
                                          wilriker @chrishamm
                                          last edited by

                                          @chrishamm As of right now ExecOnMCode is a simple API client just connecting to the socket. It is not yet converted to an official plugin. It will be started by a user-provided systemd unit (or arbitrary other means) completely independent of DCS.

                                          Manuel
                                          Duet 3 6HC (v0.6) with RPi 4B on a custom Cartesian
                                          with probably always latest firmware/DWC (incl. betas or self-compiled)
                                          My Tool Collection

                                          1 Reply Last reply Reply Quote 0
                                          • CrazyCreatorundefined
                                            CrazyCreator
                                            last edited by CrazyCreator

                                            @wilriker
                                            I once wrote you a PM in German. because I don't know if you understand what I am writing via google translator 😉

                                            @All
                                            I would like to send an http command using the Mxxxx command.

                                            Let's assume I've installed everything as shown in the video linked here.

                                            What do I have to enter where and how around this line:

                                            http://IP.Adrresse.Of.Shelly/relay/0?turn=off

                                            to be able to send?

                                            P.S or edit or next step
                                            i installed the ExecOnMCode with help of the two videos. but the shutdown (poweroff) with M7722 don't work for me.
                                            The Service is running:
                                            Bildschirmfoto 2021-05-18 um 22.03.40.png

                                            but in DWC i see this message:
                                            M7722
                                            Warning: M7722: Command is not supported

                                            And the Raspi is still working/running.

                                            On Terminal (connected with Raspi) i see:
                                            pi@CCC:~ $
                                            2021/05/18 22:24:53 Connection to DCS closed
                                            2021/05/18 22:24:53 EOF
                                            pi@CCC:~ $

                                            http://www.crazycreatorcube.com

                                            wilrikerundefined CrazyCreatorundefined 2 Replies Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Unless otherwise noted, all forum content is licensed under CC-BY-SA