MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    ubuntu - run the script before sleeping and after waking up the PC

    Scheduled Pinned Locked Moved Solved Troubleshooting
    8 Posts 2 Posters 727 Views 2 Watching
    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.
    • M Offline
      majsoft
      last edited by

      Hello everyone, can I ask for help? I’m sorry, I’m using a translator, I can’t speak English.
      I am using the new Ubuntu, where I have Domoticz server and MagicMirror running.
      after 15 minutes the PC goes to sleep and then wakes up with PIR (ESP8266).

      After waking up, it takes a long time for the domoticz server to start working and sending Information to MagicMirror. I don’t want to solve it with service monitor.

      I would like to set up a script:

      before putting the PC to sleep run “sudo service domoticz.sh stop”
      after waking up the PC run “sudo service domoticz.sh start”
      it works reliably in the command line, it just needs a password that I enter and it’s ok.

      I saw somewhere that something like this can be used, but I don’t know exactly where to write, save and that it doesn’t ask for a password. Would someone be willing to write the exact procedure where to write what and save? I found a lot of versions and maybe I got it wrong or the instructions are for older Ubuntu

      First of all thank you very much for the joy and newbie to ubuntu.

      #!/bin/sh
      
      PATH=/sbin:/usr/sbin:/bin:/usr/bin
      
      case "$1" in
          pre)
                  #code execution BEFORE sleeping/hibernating/suspending
          ;;
          post)
                  #code execution AFTER resuming
          ;;
      esac
      
      exit 0
      
      

      Snímek2.jpg

      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @majsoft
        last edited by sdetweil

        @majsoft ok, here is the doc I found

        Suggesting to read this manual page:

        https://www.man7.org/linux/man-pages/man8/systemd-sleep.8.html

        Especially this section:

        Immediately before entering system suspend and/or hibernation
        systemd-suspend.service (and the other mentioned units, respectively)

        will run all executables

        in /usr/lib/systemd/system-sleep/ and pass two arguments to them.

        The first argument will be “pre”,
        the second either “suspend”, “hibernate”, “hybrid-sleep”, or “suspend-then-hibernate”

        depending on the chosen action. An environment variable called “SYSTEMD_SLEEP_ACTION” will be set and contain the sleep action that is processing. This is primarily helpful for “suspend-then-hibernate” where the value of the variable will be “suspend”, “hibernate”, or “suspend-after-failed-hibernate” in cases where hibernation has failed.

        Immediately after leaving system suspend and/or hibernation the same executables are run, but the first argument is now “post”.

        All executables in this directory are executed in parallel, and execution of the action is not continued until all executables have finished.


        so your script should go in that folder… and of course be set with permissions to be executable by root

        see the chmod and chgrp commands

        the permissions are in three sections
        for the current user
        for the a group (the user might be in)
        for all other

        when you do ls -laF somefile (i picked a text file)
        -rw-rw-r-- 1 sam sam 8087 Oct 26 08:59 somefile.txt
        the permissions are
        user rw- (rear/write but not executable
        group rw- (read/write but not executable)
        and all others r-- (read only)
        that is 3 bits each +1 so 10 bits of permission
        usually represented by a number
        in this case 664
        7 is all bits on 111
        0 is all bits off 000
        chmod has shortcuts too chmod +x make executable for all sections
        chmod -w makes it not writeble for all

        so if you want to make a file read/write and executable by bob, read only for anyone in group testers, and NO access for anyone else (other)
        740 bob testers
        chown and chgrp are used to set the owner and group

        in linux most commands have help with the --help parameter chown --help
        and most have a manual page with more text
        the get the manual page, type
        man command_name
        like
        man chown

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • S Offline
          sdetweil @majsoft
          last edited by sdetweil

          @majsoft

          what does this

          PC goes to sleep and then wakes up with PIR (ESP8266).
          

          is there a MagicMirror module? or??

          whatever does that is where you want to call your script to do the server shutdown and startup
          exactly what you do on the console.

          if this requires sudo authority, you can add your userid to the /etc/sudoers file
          to not prompt for your password
          (google search)

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 0
          • M Offline
            majsoft
            last edited by

            I can’t get the script to run itself, there are tutorials everywhere but none of them worked for me. I have to study further.

            S 1 Reply Last reply Reply Quote 0
            • S Offline
              sdetweil @majsoft
              last edited by sdetweil

              @majsoft ok, here is the doc I found

              Suggesting to read this manual page:

              https://www.man7.org/linux/man-pages/man8/systemd-sleep.8.html

              Especially this section:

              Immediately before entering system suspend and/or hibernation
              systemd-suspend.service (and the other mentioned units, respectively)

              will run all executables

              in /usr/lib/systemd/system-sleep/ and pass two arguments to them.

              The first argument will be “pre”,
              the second either “suspend”, “hibernate”, “hybrid-sleep”, or “suspend-then-hibernate”

              depending on the chosen action. An environment variable called “SYSTEMD_SLEEP_ACTION” will be set and contain the sleep action that is processing. This is primarily helpful for “suspend-then-hibernate” where the value of the variable will be “suspend”, “hibernate”, or “suspend-after-failed-hibernate” in cases where hibernation has failed.

              Immediately after leaving system suspend and/or hibernation the same executables are run, but the first argument is now “post”.

              All executables in this directory are executed in parallel, and execution of the action is not continued until all executables have finished.


              so your script should go in that folder… and of course be set with permissions to be executable by root

              see the chmod and chgrp commands

              the permissions are in three sections
              for the current user
              for the a group (the user might be in)
              for all other

              when you do ls -laF somefile (i picked a text file)
              -rw-rw-r-- 1 sam sam 8087 Oct 26 08:59 somefile.txt
              the permissions are
              user rw- (rear/write but not executable
              group rw- (read/write but not executable)
              and all others r-- (read only)
              that is 3 bits each +1 so 10 bits of permission
              usually represented by a number
              in this case 664
              7 is all bits on 111
              0 is all bits off 000
              chmod has shortcuts too chmod +x make executable for all sections
              chmod -w makes it not writeble for all

              so if you want to make a file read/write and executable by bob, read only for anyone in group testers, and NO access for anyone else (other)
              740 bob testers
              chown and chgrp are used to set the owner and group

              in linux most commands have help with the --help parameter chown --help
              and most have a manual page with more text
              the get the manual page, type
              man command_name
              like
              man chown

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • M Offline
                majsoft
                last edited by majsoft

                Hi, thanks for guiding me in the right direction.
                I had an error in the placement of the script.

                Now I have verified that it works.
                I will then delete the sleep 60 command and the output to the file, I had time to find out if the necessary services are really turned off.

                so my solution was as follows:

                file creation

                sudo nano /usr/lib/systemd/system-sleep/wakeup.sh
                

                the contents of the file

                #!/bin/sh
                
                case $1/$2 in
                pre/*)
                echo "zastavuji domoticz......"
                service domoticz.sh stop >> /home/meteo/vystup_off.txt
                sleep 60
                ;;
                post/*)
                echo "spouštím domoticz......"
                service domoticz.sh start >> /home/meteo/vystup_on.txt
                sleep 0
                ;;
                esac
                

                permission settings

                sudo chmod a+x /usr/lib/systemd/system-sleep/wakeup.sh
                

                once again thank you very much.

                my next goal : Control MM Using systemd/systemctl
                PM2 is not working correctly on Ubuntu 24 and I need to have MM as a service.
                so far I’m using mm.sh after startup.

                cd ./MagicMirror
                DISPLAY=:0 npm start
                read;
                
                S 1 Reply Last reply Reply Quote 0
                • S Offline
                  sdetweil @majsoft
                  last edited by

                  @majsoft cool

                  pm2 on 24, will review

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  M 1 Reply Last reply Reply Quote 0
                  • M Offline
                    majsoft @sdetweil
                    last edited by

                    @sdetweil

                    i looked on github but i can’t see or find a fix.

                    S 1 Reply Last reply Reply Quote 0
                    • S Offline
                      sdetweil @majsoft
                      last edited by

                      @majsoft yeh, that one opened by me!

                      fairly slow w fixes

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      1 Reply Last reply Reply Quote 0
                      • S sdetweil has marked this topic as solved on
                      • 1 / 1
                      • First post
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy