how to run a user script after systemd wakeup?












6















When my debian jessie desktop box wakes up from sleep (via the new shiny systemd) my mouse settings are returned to their defaults, having reset my customisation



xinput set-prop 12 'Device Accel Constant Deceleration' 2.5


which runs when I log in.



how can I run an arbitrary user script on wakeup? (assume that the user is the owner of the X session)



As far as I can recall, the following is the only customisation I've made of the systemd setup (yes, I know it's completely wrong because it doesn't work for arbitrary users, but I've not worked out how to do that yet... this is somewhat related)



additionally, how can I run an arbitrary user script before wakeup, as the user who is currently using the X screen?



cat /etc/systemd/system/i3lock.service



#systemctl enable i3lock.service

[Unit]
Description=i3lock
Before=sleep.target

[Service]
User=fommil
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock -c 000000

[Install]
WantedBy=sleep.target









share|improve this question

























  • askubuntu.com/a/661747 It help me in my case which is different to yours. Maybe it can be useful to you.

    – Sun Bear
    Apr 3 '17 at 4:38
















6















When my debian jessie desktop box wakes up from sleep (via the new shiny systemd) my mouse settings are returned to their defaults, having reset my customisation



xinput set-prop 12 'Device Accel Constant Deceleration' 2.5


which runs when I log in.



how can I run an arbitrary user script on wakeup? (assume that the user is the owner of the X session)



As far as I can recall, the following is the only customisation I've made of the systemd setup (yes, I know it's completely wrong because it doesn't work for arbitrary users, but I've not worked out how to do that yet... this is somewhat related)



additionally, how can I run an arbitrary user script before wakeup, as the user who is currently using the X screen?



cat /etc/systemd/system/i3lock.service



#systemctl enable i3lock.service

[Unit]
Description=i3lock
Before=sleep.target

[Service]
User=fommil
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock -c 000000

[Install]
WantedBy=sleep.target









share|improve this question

























  • askubuntu.com/a/661747 It help me in my case which is different to yours. Maybe it can be useful to you.

    – Sun Bear
    Apr 3 '17 at 4:38














6












6








6


1






When my debian jessie desktop box wakes up from sleep (via the new shiny systemd) my mouse settings are returned to their defaults, having reset my customisation



xinput set-prop 12 'Device Accel Constant Deceleration' 2.5


which runs when I log in.



how can I run an arbitrary user script on wakeup? (assume that the user is the owner of the X session)



As far as I can recall, the following is the only customisation I've made of the systemd setup (yes, I know it's completely wrong because it doesn't work for arbitrary users, but I've not worked out how to do that yet... this is somewhat related)



additionally, how can I run an arbitrary user script before wakeup, as the user who is currently using the X screen?



cat /etc/systemd/system/i3lock.service



#systemctl enable i3lock.service

[Unit]
Description=i3lock
Before=sleep.target

[Service]
User=fommil
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock -c 000000

[Install]
WantedBy=sleep.target









share|improve this question
















When my debian jessie desktop box wakes up from sleep (via the new shiny systemd) my mouse settings are returned to their defaults, having reset my customisation



xinput set-prop 12 'Device Accel Constant Deceleration' 2.5


which runs when I log in.



how can I run an arbitrary user script on wakeup? (assume that the user is the owner of the X session)



As far as I can recall, the following is the only customisation I've made of the systemd setup (yes, I know it's completely wrong because it doesn't work for arbitrary users, but I've not worked out how to do that yet... this is somewhat related)



additionally, how can I run an arbitrary user script before wakeup, as the user who is currently using the X screen?



cat /etc/systemd/system/i3lock.service



#systemctl enable i3lock.service

[Unit]
Description=i3lock
Before=sleep.target

[Service]
User=fommil
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock -c 000000

[Install]
WantedBy=sleep.target






systemd suspend






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 25 '14 at 22:27









Gilles

531k12810631591




531k12810631591










asked Aug 25 '14 at 9:44









fommilfommil

293212




293212













  • askubuntu.com/a/661747 It help me in my case which is different to yours. Maybe it can be useful to you.

    – Sun Bear
    Apr 3 '17 at 4:38



















  • askubuntu.com/a/661747 It help me in my case which is different to yours. Maybe it can be useful to you.

    – Sun Bear
    Apr 3 '17 at 4:38

















askubuntu.com/a/661747 It help me in my case which is different to yours. Maybe it can be useful to you.

– Sun Bear
Apr 3 '17 at 4:38





askubuntu.com/a/661747 It help me in my case which is different to yours. Maybe it can be useful to you.

– Sun Bear
Apr 3 '17 at 4:38










2 Answers
2






active

oldest

votes


















1














I created a file in /lib/systemd/system-sleep.



#!/bin/sh
case $1 in
post)
/bin/runme
;;
esac


The post is called on wakeup.



EDIT:



https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html






share|improve this answer





















  • 1





    This didn't work for me. Can you link to some documentation explaining how it's supposed to work?

    – Ryan Lue
    Jul 22 '18 at 9:34











  • This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do.

    – theferrit32
    Dec 20 '18 at 23:10



















0














This answer is based on askubuntu.com/a/661747/394818 (as also referred to in the comment by @sun-bear), askubuntu.com/q/616272/394818 and superuser.com/a/1269158/585953.



Using a system service:



Create the file /etc/systemd/system/my_user_script.service:



[Unit]
Description=Run my_user_script
After=suspend.target hibernate.target hybrid-sleep.target

[Service]
ExecStart=/path/to/my_user_script
#User=my_user_name
#Environment=DISPLAY=:0

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target


Remove suspend/hibernate/hybrid in case the service should only be executed after waking up from a specific type of sleep. In case the service needs to be ran by a specific user, uncomment the User= and Environment= lines and replace the relevant user name.



Install the service file with:



sudo systemctl enable my_user_script


Using a user service will not work:



In order to avoid setting a hard coded user name with User=, one could create the exact same service file at .local/share/systemd/user/my_user_script.service and activate with systemctl --user enable my_user_script. However, that will not work. @grawity explains in more detail at unix.stackexchange.com/a/174837/163108 why that is:




sleep.target is specific to system services. The reason is, sleep.target is not a magic target that automatically gets activated when going to sleep. It's just a regular target that puts the system to sleep – so the 'user' instances of course won't have an equivalent. (And unfortunately the 'user' instances currently have no way to depend on systemwide services.)







share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f152039%2fhow-to-run-a-user-script-after-systemd-wakeup%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I created a file in /lib/systemd/system-sleep.



    #!/bin/sh
    case $1 in
    post)
    /bin/runme
    ;;
    esac


    The post is called on wakeup.



    EDIT:



    https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html






    share|improve this answer





















    • 1





      This didn't work for me. Can you link to some documentation explaining how it's supposed to work?

      – Ryan Lue
      Jul 22 '18 at 9:34











    • This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do.

      – theferrit32
      Dec 20 '18 at 23:10
















    1














    I created a file in /lib/systemd/system-sleep.



    #!/bin/sh
    case $1 in
    post)
    /bin/runme
    ;;
    esac


    The post is called on wakeup.



    EDIT:



    https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html






    share|improve this answer





















    • 1





      This didn't work for me. Can you link to some documentation explaining how it's supposed to work?

      – Ryan Lue
      Jul 22 '18 at 9:34











    • This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do.

      – theferrit32
      Dec 20 '18 at 23:10














    1












    1








    1







    I created a file in /lib/systemd/system-sleep.



    #!/bin/sh
    case $1 in
    post)
    /bin/runme
    ;;
    esac


    The post is called on wakeup.



    EDIT:



    https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html






    share|improve this answer















    I created a file in /lib/systemd/system-sleep.



    #!/bin/sh
    case $1 in
    post)
    /bin/runme
    ;;
    esac


    The post is called on wakeup.



    EDIT:



    https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 26 '18 at 15:08

























    answered Nov 12 '17 at 8:16









    gam3gam3

    1867




    1867








    • 1





      This didn't work for me. Can you link to some documentation explaining how it's supposed to work?

      – Ryan Lue
      Jul 22 '18 at 9:34











    • This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do.

      – theferrit32
      Dec 20 '18 at 23:10














    • 1





      This didn't work for me. Can you link to some documentation explaining how it's supposed to work?

      – Ryan Lue
      Jul 22 '18 at 9:34











    • This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do.

      – theferrit32
      Dec 20 '18 at 23:10








    1




    1





    This didn't work for me. Can you link to some documentation explaining how it's supposed to work?

    – Ryan Lue
    Jul 22 '18 at 9:34





    This didn't work for me. Can you link to some documentation explaining how it's supposed to work?

    – Ryan Lue
    Jul 22 '18 at 9:34













    This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do.

    – theferrit32
    Dec 20 '18 at 23:10





    This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do.

    – theferrit32
    Dec 20 '18 at 23:10













    0














    This answer is based on askubuntu.com/a/661747/394818 (as also referred to in the comment by @sun-bear), askubuntu.com/q/616272/394818 and superuser.com/a/1269158/585953.



    Using a system service:



    Create the file /etc/systemd/system/my_user_script.service:



    [Unit]
    Description=Run my_user_script
    After=suspend.target hibernate.target hybrid-sleep.target

    [Service]
    ExecStart=/path/to/my_user_script
    #User=my_user_name
    #Environment=DISPLAY=:0

    [Install]
    WantedBy=suspend.target hibernate.target hybrid-sleep.target


    Remove suspend/hibernate/hybrid in case the service should only be executed after waking up from a specific type of sleep. In case the service needs to be ran by a specific user, uncomment the User= and Environment= lines and replace the relevant user name.



    Install the service file with:



    sudo systemctl enable my_user_script


    Using a user service will not work:



    In order to avoid setting a hard coded user name with User=, one could create the exact same service file at .local/share/systemd/user/my_user_script.service and activate with systemctl --user enable my_user_script. However, that will not work. @grawity explains in more detail at unix.stackexchange.com/a/174837/163108 why that is:




    sleep.target is specific to system services. The reason is, sleep.target is not a magic target that automatically gets activated when going to sleep. It's just a regular target that puts the system to sleep – so the 'user' instances of course won't have an equivalent. (And unfortunately the 'user' instances currently have no way to depend on systemwide services.)







    share|improve this answer




























      0














      This answer is based on askubuntu.com/a/661747/394818 (as also referred to in the comment by @sun-bear), askubuntu.com/q/616272/394818 and superuser.com/a/1269158/585953.



      Using a system service:



      Create the file /etc/systemd/system/my_user_script.service:



      [Unit]
      Description=Run my_user_script
      After=suspend.target hibernate.target hybrid-sleep.target

      [Service]
      ExecStart=/path/to/my_user_script
      #User=my_user_name
      #Environment=DISPLAY=:0

      [Install]
      WantedBy=suspend.target hibernate.target hybrid-sleep.target


      Remove suspend/hibernate/hybrid in case the service should only be executed after waking up from a specific type of sleep. In case the service needs to be ran by a specific user, uncomment the User= and Environment= lines and replace the relevant user name.



      Install the service file with:



      sudo systemctl enable my_user_script


      Using a user service will not work:



      In order to avoid setting a hard coded user name with User=, one could create the exact same service file at .local/share/systemd/user/my_user_script.service and activate with systemctl --user enable my_user_script. However, that will not work. @grawity explains in more detail at unix.stackexchange.com/a/174837/163108 why that is:




      sleep.target is specific to system services. The reason is, sleep.target is not a magic target that automatically gets activated when going to sleep. It's just a regular target that puts the system to sleep – so the 'user' instances of course won't have an equivalent. (And unfortunately the 'user' instances currently have no way to depend on systemwide services.)







      share|improve this answer


























        0












        0








        0







        This answer is based on askubuntu.com/a/661747/394818 (as also referred to in the comment by @sun-bear), askubuntu.com/q/616272/394818 and superuser.com/a/1269158/585953.



        Using a system service:



        Create the file /etc/systemd/system/my_user_script.service:



        [Unit]
        Description=Run my_user_script
        After=suspend.target hibernate.target hybrid-sleep.target

        [Service]
        ExecStart=/path/to/my_user_script
        #User=my_user_name
        #Environment=DISPLAY=:0

        [Install]
        WantedBy=suspend.target hibernate.target hybrid-sleep.target


        Remove suspend/hibernate/hybrid in case the service should only be executed after waking up from a specific type of sleep. In case the service needs to be ran by a specific user, uncomment the User= and Environment= lines and replace the relevant user name.



        Install the service file with:



        sudo systemctl enable my_user_script


        Using a user service will not work:



        In order to avoid setting a hard coded user name with User=, one could create the exact same service file at .local/share/systemd/user/my_user_script.service and activate with systemctl --user enable my_user_script. However, that will not work. @grawity explains in more detail at unix.stackexchange.com/a/174837/163108 why that is:




        sleep.target is specific to system services. The reason is, sleep.target is not a magic target that automatically gets activated when going to sleep. It's just a regular target that puts the system to sleep – so the 'user' instances of course won't have an equivalent. (And unfortunately the 'user' instances currently have no way to depend on systemwide services.)







        share|improve this answer













        This answer is based on askubuntu.com/a/661747/394818 (as also referred to in the comment by @sun-bear), askubuntu.com/q/616272/394818 and superuser.com/a/1269158/585953.



        Using a system service:



        Create the file /etc/systemd/system/my_user_script.service:



        [Unit]
        Description=Run my_user_script
        After=suspend.target hibernate.target hybrid-sleep.target

        [Service]
        ExecStart=/path/to/my_user_script
        #User=my_user_name
        #Environment=DISPLAY=:0

        [Install]
        WantedBy=suspend.target hibernate.target hybrid-sleep.target


        Remove suspend/hibernate/hybrid in case the service should only be executed after waking up from a specific type of sleep. In case the service needs to be ran by a specific user, uncomment the User= and Environment= lines and replace the relevant user name.



        Install the service file with:



        sudo systemctl enable my_user_script


        Using a user service will not work:



        In order to avoid setting a hard coded user name with User=, one could create the exact same service file at .local/share/systemd/user/my_user_script.service and activate with systemctl --user enable my_user_script. However, that will not work. @grawity explains in more detail at unix.stackexchange.com/a/174837/163108 why that is:




        sleep.target is specific to system services. The reason is, sleep.target is not a magic target that automatically gets activated when going to sleep. It's just a regular target that puts the system to sleep – so the 'user' instances of course won't have an equivalent. (And unfortunately the 'user' instances currently have no way to depend on systemwide services.)








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 14:55









        davidovitchdavidovitch

        32




        32






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f152039%2fhow-to-run-a-user-script-after-systemd-wakeup%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre