Modifiying shutdown button to add extra features

I have my server running a custom command that shuts down the printer and then using a webaction shuts off the power to the printer and the server though a smart plug. I have a small delay in the ifttt program to allow the server to properly shutdown before removing power.

I have this gcode in a quick command

@pushmessage Hard Shutdown of MK2.5S Initiated
M104 S0 T0
M140 S0
M109 R38
@syncMotion
@webAction Hard_Shutdown
@syncMotion
@pushmessage Hard Shutdown of Mk2.5s Completed
G4 S1
@syncMotion
@execute shutdown

would there be an easy way to modify the action of the current shutdown button in the extcommands.xml to achieve the same result?
in my mind the easiest would be to be able to call my custom quick command from within the shutdown script.

so change the current execute to something else
<execute>sudo /sbin/shutdown -h now</execute>

I am not a programmer so any help on this would be much appreciated.










Comments

  • There is a small difference. Your code only works with an activated printer. If printer is already disabled M/G Codes are ignored which would be ok in that case I guess. So assuming you leave printer always activated in server you can instead call a script and send the @quickcommand "name" to execute your script. See
    https://prgdoc.repetier-server.com/v1/docs/index.html#/en/web-api/unixsockets
    on how to do this when you activate unix sockets. There is a sample script (put it in /var/lib/Repetier-Server/scripts) and adjust the included gcode command and printer slug name (see url in browser when inside a printer tab) it is the one looking like printer name.
  • Thank you for pushing me in the right direction, I have enabled unix sockets and written the script. the script is working when I a run it as user pi, but I don't have the right permission to run it as user repetierserver.

    Sorry, user repetierserver is not allowed to execute '/usr/bin/nc -N -U /tmp/repetier-server.sock' as repetierserver on RepetierServer

    can you point me toward the proper way to give user repetierserver permission to execute the script.







  • Maybe a bit misleading but that was the command as you enter it running user pi. If you make a script of it, it would look like this:

    #!/bin/bash
    nc -N -U /tmp/repetier-server.sock <<EOF
    {
      "action": "send",
      "data": {
        "cmd": "@info 'Message from the shell'"
      },
      "printer": "Felix_Pro_1"
    }
    EOF

    Server runs script as user repetierserver already. I set execute permission with
    chmod 755 myshutdown

    And in extcommands you call it without sudo just full path /var/lib/Repetier-Server/scripts/myshutdown

    As a test on console run
    sudo -i
    sudo -u repetierserver var/lib/Repetier-Server/scripts/myshutdown

    which should work as it simulates user and path server would use then.
  • I got it working, I had sudo in the original script that was the hangup. Now I'm having a problem where the power button on the touch screen disappears as soon as I change the default execute from shutdown to my script.

    <execute>sudo /sbin/shutdown -h now</execute>
    power button shows up on main page of touch screen

    <execute>/var/lib/Repetier-Server/scripts/hard-shutdown.sh</execute>
    no power button

    While playing around i was able to get everything working by putting both commands in the execute section, but I had to put the script command first and the shutdown command second. I don't think the shutdown command is being executed, but the button is at least staying on the screen now and it works.

    <execute>/var/lib/Repetier-Server/scripts/hard-shutdown.sh ; sudo /sbin/shutdown -h +30</execute>

    thanks for the help




  • Sure sudo /sbin/shutdown -h +30 gets executed? Not sure if linux can handle multiple commands in a command call - we are just starting first command. Maybe better put it at the end of hard-shutdown.sh. Since shutdown is in sudoer list for repetierserver that should be no problem.

    If some parts do not get shown it is probably a xml error. Copy xml file to
    https://xmllint.com/
    and verify that it is at least valid xml.

  • Lets backup,

    can you tell me why would the power button disappear from the touch screen any time I remove the shutdown command from the <execute> section?

    <execute>sudo /sbin/shutdown -h now</execute>
    power button shows up on main page of touch screen

    <execute>/var/lib/Repetier-Server/scripts/hard-shutdown.sh</execute>
    no power button



  • You did only show part of it. There are 2 different <execute> tags. The one inside <command> are part of the button and required. The <execute> outside <command> are for the @execute command. So my guess is that you removed the execute inside command.
  • yes I am trying to make the shutdown button execute my custom script so I did replace the <execute> inside the shutdown server command

    This works, but the power button disappears from the touch screen interface

    <command>
    <name>Shutdown Server</name>
    <icon>
    <svg aria-hidden="true" focusable="false" data-prefix="far" data-icon="power-off" class="svg-inline--fa fa-power-off fa-w-16" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M388.5 46.3C457.9 90.3 504 167.8 504 256c0 136.8-110.8 247.7-247.5 248C120 504.3 8.2 393 8 256.4 7.9 168 54 90.3 123.5 46.3c5.8-3.7 13.5-1.8 16.9 4.2l11.8 20.9c3.1 5.5 1.4 12.5-3.9 15.9C92.8 122.9 56 185.1 56 256c0 110.5 89.5 200 200 200s200-89.5 200-200c0-70.9-36.8-133.1-92.3-168.6-5.3-3.4-7-10.4-3.9-15.9l11.8-20.9c3.3-6.1 11.1-7.9 16.9-4.3zM280 276V12c0-6.6-5.4-12-12-12h-24c-6.6 0-12 5.4-12 12v264c0 6.6 5.4 12 12 12h24c6.6 0 12-5.4 12-12z"></path></svg>
    </icon>
    <execute>/var/lib/Repetier-Server/scripts/hard-shutdown.sh</execute>
    <confirm>Really shut down the server?</confirm>
    <!--
     Define if command should show up in local printer interface, default true.
    -->
    <local>true</local>
    <!--
     Define if command should show up in remote printer interface, default true.
    -->
    <remote>true</remote>
    <!--
     Define if command should show up only for users with print permission, default true.
    -->
    <print-permission>true</print-permission>
    <!--
     Define if command should show up only for users with add files permission, default false.
    -->
    <add-permission>true</add-permission>
    <!--
     Define if command should show up only for users with del files permission, default false.
    -->
    <del-permission>true</del-permission>
    <!--
     Define if command should show up only for users with configuration permission, default false.
    -->
    <config-permission>true</config-permission>
    </command>


    changing the execute command to this works and the button stays on the screen. The shutdown command is ignored, but somehow it is triggering something to keep the power button on screen.

    <execute>/var/lib/Repetier-Server/scripts/hard-shutdown.sh ; sudo /sbin/shutdown -h now</execute>





  • I see your problem. It is showing up in both cases, but you need to go to server commands icon to see them.
    The shutdown command on home screen only appears if the command contains "shutdown -h"

    So as solution for you rename hard-shutdown.sh into hard-shutdown and make the execute
    <execute>/var/lib/Repetier-Server/scripts/hard-shutdown -h now</execute>
    Also you don't use the parameter the check to show it is happy and will put it on home screen as well then.
  • edited August 2022
    All is working now, for reference here are my settings

    Printer Settings - G-Codes - Quick Commands
    Command Name - Hard Shutdown

    G-Code to Execute
    @pushmessage Hard Shutdown of MK2.5S Initiated
    M104 S0 T0
    M140 S0
    M109 R38
    @syncMotion
    @webAction Hard_Shutdown
    @syncMotion
    @pushmessage Hard Shutdown of Mk2.5s Completed
    G4 S1
    @syncMotion
    @execute shutdown

    Printer Settings - G-Codes - Event Dependent - Shutdown Printer
    G-Code
    @quickCommand Hard Shutdown


    My Script to execute a quick command
    located in /var/lib/Repetier-Server/scripts

    #!/bin/bash
    nc -N -U /tmp/repetier-server.sock <<EOF
    {
      "action": "send",
      "data": {
        "cmd": "@quickCommand Hard Shutdown"
      },
      "printer": "MK2.5S"
    }
    EOF


    extcommands.xml
    located in /var/lib/Repetier-Server/databse

    <command>
    <name>Shutdown Server</name>
    <icon>
    <svg aria-hidden="true" focusable="false" data-prefix="far" data-icon="power-off" class="svg-inline--fa fa-power-off fa-w-16" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M388.5 46.3C457.9 90.3 504 167.8 504 256c0 136.8-110.8 247.7-247.5 248C120 504.3 8.2 393 8 256.4 7.9 168 54 90.3 123.5 46.3c5.8-3.7 13.5-1.8 16.9 4.2l11.8 20.9c3.1 5.5 1.4 12.5-3.9 15.9C92.8 122.9 56 185.1 56 256c0 110.5 89.5 200 200 200s200-89.5 200-200c0-70.9-36.8-133.1-92.3-168.6-5.3-3.4-7-10.4-3.9-15.9l11.8-20.9c3.3-6.1 11.1-7.9 16.9-4.3zM280 276V12c0-6.6-5.4-12-12-12h-24c-6.6 0-12 5.4-12 12v264c0 6.6 5.4 12 12 12h24c6.6 0 12-5.4 12-12z"></path></svg>
    </icon>
    <execute>/var/lib/Repetier-Server/scripts/hard-shutdown -h now</execute>
    <confirm>Really shut down the server?</confirm>
    <!--
     Define if command should show up in local printer interface, default true.
    -->
    <local>true</local>
    <!--
     Define if command should show up in remote printer interface, default true.
    -->
    <remote>true</remote>
    <!--
     Define if command should show up only for users with print permission, default true.
    -->
    <print-permission>true</print-permission>
    <!--
     Define if command should show up only for users with add files permission, default false.
    -->
    <add-permission>true</add-permission>
    <!--
     Define if command should show up only for users with del files permission, default false.
    -->
    <del-permission>true</del-permission>
    <!--
     Define if command should show up only for users with configuration permission, default false.
    -->
    <config-permission>true</config-permission>
    </command>

    I also have these commands at the end of extcommands.xml, they were originally missing from this installation and I had to add them manually.

    <execute name="shutdown" allowParams="false" sync="false">sudo /sbin/shutdown -h now</execute>
    <execute name="reboot" allowParams="false" sync="false">sudo /sbin/shutdown -r now</execute>



    On the web side of this I created an IFTTT webhook which triggers an alexa command using
    Alexa Action by mkZense Integrations. I then linked the mkZense command to an alexa routine. The alexa routine turns off a tp link smart plug that the printer and server are connected to with a 30 second delay to allow the server to finish shutting down.

    I also had to link the webhook in the Global Settings menu - Web Actions under the name Hard-Shutdown which linked to the name I gave the original webhook when setting up the applet in IFTTT.


    when I press the power button on the touch screen, let the printer idle for more then 30 minutes or toggle the shutdown after print setting my printer will do the following.

    1. send a notification to my phone that the shutdown has started
    2. turn off the hotend
    3. turn off the bed
    4. set the hotend to 38C and wait for it to reach that temperature
    5. trigger the web action, which will trigger alexa routine to turn off my smart plug in 30 seconds
    6. send a notification to my phone that the shutdown has completed
    7. shutdown the server
    8. turn off the smart plug

    Its working great so now I will be implementing it on my second printer/server combo. I have my server (raspberry Pi) powered from the same power supply as my printer so they will always be on/off together.

    Thanks @Repetier for all the help on this















  • Alright I spoke too soon, I am now having an issue where when I let the printer hit the "idle Power Off Time" it then goes through the shutdown sequence correctly, but when I power it up the next time it immediately executes the shutdown sequence again. Usually I only have to turn on the plug one additional time, but today I got stuck in a shutdown loop on one of my printer/server combos. any idea what could be causing this?
  • Does it loos connection meaning does it get red after shutdown? On connection start the idle timer gets set to zero so from that point on it starts counting. If the printer still gets powered by usb connection stays green and triggers timer. On the other side once triggered it does not retrigger. So without seeing the communication when this happens I have at the moment no idea.  If you have logging when idle enabled it should be in the log. But if you post the log please tell us also the time to look at and anything else noteworthy.
  • My server is running on a raspberry pi that is powered off the same power supply as my 3d printer, so when I shutdown my printer (remove power) it also removes power from the server.

    A simplified version of what I am executing is this, assuming I have a 5 minute idle timout setup.
    1. 5 minutes passes
    2. Issue a shutdown command on the server (sudo /sbin/shutdown -r now)
    3. wait 30 seconds
    4. remove power from the printer and the raspberry pi.

    now when I power the printer and server back up its as if the idle timer is counting from the moment the shutdown command was executed.

    I ran a test with a 5 minute idle timer and this was the results (times are approximate)
    9:25 power up printer/server
    9:30 auto shutdown executed
    9:35 power up printer/server
    9:36 auto shutdown executed
    9:41 power up printer/server
    9:43 auto shutdown executed

    Here are my logs which only contain data from the test above.
    server.log
    syslog.log

    it looks like I really just need a way to reset the idle timer on boot




  • Actually that behaviour is correct. If you start with printer connected the idle timer will start it's 5 minutes. If you do nothing stopping the idle timer it will call the shutdown script.

    Remember it is a printer shutdown function to disable a printer for power saving when it is not used. You are misusing it to also shutdown the pi and probably expect it to first shutdown when it had printed. You can do this easily with global variables and changing the shutdown code. For this add in run on connect
    @set global.had_job 0
    and in "Run before job"
    @set global.had_job 1

    then you have variable set to 1 after a print. The put your idle shutdown script inside
    @if {{global.had_job==1}}
    ....
    @endif

    and it will only run after a print on idle.
  • I actually do expect it to shutdown the printer after hitting the idle timer even if the printer has not printed anything. My concern is that the idle timer is not resetting when the server boots up from a hard power down.

    take this scenario
    I have the idle timer set to 30 minutes
    I power up the printer and server at 10:00
    I decide that I no longer want to print anything and I shut the printer and server down at 10:20
    If I boot up any time after 10:30 which is 30 minutes after the idle timer started counting It will immediately execute the idle timeout function upon boot.


    The solution you posted does work to prevent it from going into a shutdown loop, Its a good enough solution to just use it and be done. Ultimately I would like the printer to still timeout and shutdown even if nothing is printed, but I suppose that scenario is fairly rare.




  • I was able to get this to work.

    Idle timeout - 30 minutes

    Run on connect
    @set global.shutdowntime {{1800}}
    @set global.boottime {{time()}}

    Run before job
    @set global.boottime {{time()-global.shutdowntime}}

    Shutdown Printer
    @set global.elapsed {{time() - global.boottime}}
    @if {{global.elapsed>=global.shutdowntime}}
       @quickCommand Hard Shutdown
    @endif

    Now the server will not trigger a shutdown until after 30 minutes has elapsed, but If a print is triggered it will still shutdown even if the print takes less than 30 minutes total.




  • I tested the initialization. Even if device is connected the idle timer gets reset on server start. Actually it always is before trying to connect a printer. And the value is just in ram, not in any file so not permanent. For testing I added a new state variable to see idle time. In next release you can see it as well
    @echo {{state.idle_seconds}}

    I registered that after shutdown value was set to very high time. Maybe that is related to the issue, also I can not reproduce it. But I revised the logic, so now idle stays untouched. But it still gets reset on next M code being send.
Sign In or Register to comment.