Start the service after the user logs offUpstart job doesn't die correctlyRun sudo script at startup under 15.04 using 'systemd'unable to start a service in systemdInstall Redis-Server on Ubuntu 16.04 LTS minimalExe. path is not absolute (Trying to start a service I created)

What is the difference between the Ancient Greek religion and the Ancient Roman religion?

Why would gloves be necessary for handling flobberworms?

Name of third cumulant?

Is it usual for a US president to make specific comments about a UK Prime Minister's suitability during a general election?

Why most flyback converters / LED drivers operate under 100 kHz?

Why can't we define more Maxwell's relations?

Why do are dates (of writing or publication) missing in scientific papers?

What does "fancy being" mean?

How to check whether the permutation is random or not

Does Amoxicillin (as trihydrate) degrade in stomach acid?

What controls specific gene silencing during cell differentiation?

Was the Berlin Wall Breached Based upon an Erroneous Declaration?

Vintage vs modern B&W photography techniques differ in color luminance - what's going on here?

What is the difference between money and currency?

Is this Forest Gnome Warlock character build legal?

Does an NPC stat block get racial bonuses added to it?

Is there a heuristic approach to the MILP problem?

What is the rule?

What does it take to make metal music?

Genitive case vs. von, when "a noun stands by itself or is used with a word which does not decline"

Isn't LaTeX a complete software for producing books?

What does the KL being symmetric tell us about the distributions?

Languages which changed their writing direction

Fundamental difference between jivan mukta and saguna parabrahman



Start the service after the user logs off


Upstart job doesn't die correctlyRun sudo script at startup under 15.04 using 'systemd'unable to start a service in systemdInstall Redis-Server on Ubuntu 16.04 LTS minimalExe. path is not absolute (Trying to start a service I created)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0

















MyUnit2.service



[Unit]
Description = MyUnit2
[Service]
RemainAfterExit=true
ExecStart=/usr/mscript2
Type=oneshot
[Install]
WantedBy=shutdown.target reboot.target


mscript2



> /var/log/mlog2.log
echo "Time:" >> /var/log/mlog2.log
date >> /var/log/mlog2.log
echo "Uptime:" >> /var/log/mlog2.log
uptime >> /var/log/mlog2.log


According to the task it is necessary that the script be executed after shutdown, reboot, user exit. The first two options I did. How do I finish the job?










share|improve this question


























  • ~/.bash_logout might help.

    – Cyrus
    Jul 14 at 7:43

















0

















MyUnit2.service



[Unit]
Description = MyUnit2
[Service]
RemainAfterExit=true
ExecStart=/usr/mscript2
Type=oneshot
[Install]
WantedBy=shutdown.target reboot.target


mscript2



> /var/log/mlog2.log
echo "Time:" >> /var/log/mlog2.log
date >> /var/log/mlog2.log
echo "Uptime:" >> /var/log/mlog2.log
uptime >> /var/log/mlog2.log


According to the task it is necessary that the script be executed after shutdown, reboot, user exit. The first two options I did. How do I finish the job?










share|improve this question


























  • ~/.bash_logout might help.

    – Cyrus
    Jul 14 at 7:43













0












0








0








MyUnit2.service



[Unit]
Description = MyUnit2
[Service]
RemainAfterExit=true
ExecStart=/usr/mscript2
Type=oneshot
[Install]
WantedBy=shutdown.target reboot.target


mscript2



> /var/log/mlog2.log
echo "Time:" >> /var/log/mlog2.log
date >> /var/log/mlog2.log
echo "Uptime:" >> /var/log/mlog2.log
uptime >> /var/log/mlog2.log


According to the task it is necessary that the script be executed after shutdown, reboot, user exit. The first two options I did. How do I finish the job?










share|improve this question















MyUnit2.service



[Unit]
Description = MyUnit2
[Service]
RemainAfterExit=true
ExecStart=/usr/mscript2
Type=oneshot
[Install]
WantedBy=shutdown.target reboot.target


mscript2



> /var/log/mlog2.log
echo "Time:" >> /var/log/mlog2.log
date >> /var/log/mlog2.log
echo "Uptime:" >> /var/log/mlog2.log
uptime >> /var/log/mlog2.log


According to the task it is necessary that the script be executed after shutdown, reboot, user exit. The first two options I did. How do I finish the job?







bash scripts systemd services






share|improve this question














share|improve this question











share|improve this question




share|improve this question










asked Jul 13 at 13:53









DepressingUtopianDepressingUtopian

91 bronze badge




91 bronze badge















  • ~/.bash_logout might help.

    – Cyrus
    Jul 14 at 7:43

















  • ~/.bash_logout might help.

    – Cyrus
    Jul 14 at 7:43
















~/.bash_logout might help.

– Cyrus
Jul 14 at 7:43





~/.bash_logout might help.

– Cyrus
Jul 14 at 7:43










1 Answer
1






active

oldest

votes


















1


















You could go several routes to start a script after user logout. There may be more but here are the two I am aware of



  1. use gdm to start a script (placed in /etc/gdm/PostSession) after
    logout. More on this
    here.

  2. Use two new separate systemd services. The second one should be started by the first one at the time the first one stops. Set up the first service to stop at user logout by binding it to the already existing user session service (user@.service).

I am not familiar with the details or merits of the first route, so I will only describe the second route.



In a quick test I logged in as a different user on a different tty and checked that my process (a simple echo message) was successfully started/executed upon logout (cmd logout) of this user. It worked fine on a debian-based system with systemd v232.



My example contains three elements:



  1. Modify the existing user@.service to make it start an instance of a service
    (helperunit@.service) which binds to user@.service. This way two
    things are achieved:

    • you get instantiated services which can easily be linked to users (for logging, etc)

    • the helper service is stopped when the user logs out



# /lib/systemd/system/user@.service

[Unit]
Description=User Manager for UID %i
After=systemd-user-sessions.service

[Service]
User=%i
PAMName=systemd-user
Type=notify
ExecStart=-/lib/systemd/systemd --user
Slice=user-%i.slice
KillMode=mixed
Delegate=yes
TasksMax=infinity
TimeoutStopSec=120s

# /etc/systemd/system/user@.service.d/afterlogout.conf
[Service]
Environment="USERUID=%i"
ExecStartPost=+/bin/systemctl start helperunit@$USERUID.service


  1. Create the helperunit service file (bound to user@.service), which is used to start a second service (afterlogout@.service) at the time it is stopped (i.e. upon user logout)


# /etc/systemd/system/helperunit@.service

[Unit]
Description=helper-to-start-afterlogout-service Service
BindsTo=user@%i.service

[Service]
Environment="USERUID=%i"
RemainAfterExit=yes
Type=oneshot
ExecStart=/bin/true
ExecStopPost=/bin/systemctl start afterlogout@$USERUID.service

[Install]


  1. Create a second service unit file which runs your script after user logout


# /etc/systemd/system/afterlogout@.service

[Unit]
Description=trigger-script-after-user-logout Service
Before=reboot.target shutdown.target

[Service]
Environment="USERUID=%i"
Type=oneshot
ExecStart=/bin/echo "user UID=$USERUID has logged out"

[Install]


The way this is set up the service instances will have the user UID in their name (for example, afterlogout@1000.service)






share|improve this answer



























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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/4.0/"u003ecc by-sa 4.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%2faskubuntu.com%2fquestions%2f1158046%2fstart-the-service-after-the-user-logs-off%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown


























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1


















    You could go several routes to start a script after user logout. There may be more but here are the two I am aware of



    1. use gdm to start a script (placed in /etc/gdm/PostSession) after
      logout. More on this
      here.

    2. Use two new separate systemd services. The second one should be started by the first one at the time the first one stops. Set up the first service to stop at user logout by binding it to the already existing user session service (user@.service).

    I am not familiar with the details or merits of the first route, so I will only describe the second route.



    In a quick test I logged in as a different user on a different tty and checked that my process (a simple echo message) was successfully started/executed upon logout (cmd logout) of this user. It worked fine on a debian-based system with systemd v232.



    My example contains three elements:



    1. Modify the existing user@.service to make it start an instance of a service
      (helperunit@.service) which binds to user@.service. This way two
      things are achieved:

      • you get instantiated services which can easily be linked to users (for logging, etc)

      • the helper service is stopped when the user logs out



    # /lib/systemd/system/user@.service

    [Unit]
    Description=User Manager for UID %i
    After=systemd-user-sessions.service

    [Service]
    User=%i
    PAMName=systemd-user
    Type=notify
    ExecStart=-/lib/systemd/systemd --user
    Slice=user-%i.slice
    KillMode=mixed
    Delegate=yes
    TasksMax=infinity
    TimeoutStopSec=120s

    # /etc/systemd/system/user@.service.d/afterlogout.conf
    [Service]
    Environment="USERUID=%i"
    ExecStartPost=+/bin/systemctl start helperunit@$USERUID.service


    1. Create the helperunit service file (bound to user@.service), which is used to start a second service (afterlogout@.service) at the time it is stopped (i.e. upon user logout)


    # /etc/systemd/system/helperunit@.service

    [Unit]
    Description=helper-to-start-afterlogout-service Service
    BindsTo=user@%i.service

    [Service]
    Environment="USERUID=%i"
    RemainAfterExit=yes
    Type=oneshot
    ExecStart=/bin/true
    ExecStopPost=/bin/systemctl start afterlogout@$USERUID.service

    [Install]


    1. Create a second service unit file which runs your script after user logout


    # /etc/systemd/system/afterlogout@.service

    [Unit]
    Description=trigger-script-after-user-logout Service
    Before=reboot.target shutdown.target

    [Service]
    Environment="USERUID=%i"
    Type=oneshot
    ExecStart=/bin/echo "user UID=$USERUID has logged out"

    [Install]


    The way this is set up the service instances will have the user UID in their name (for example, afterlogout@1000.service)






    share|improve this answer






























      1


















      You could go several routes to start a script after user logout. There may be more but here are the two I am aware of



      1. use gdm to start a script (placed in /etc/gdm/PostSession) after
        logout. More on this
        here.

      2. Use two new separate systemd services. The second one should be started by the first one at the time the first one stops. Set up the first service to stop at user logout by binding it to the already existing user session service (user@.service).

      I am not familiar with the details or merits of the first route, so I will only describe the second route.



      In a quick test I logged in as a different user on a different tty and checked that my process (a simple echo message) was successfully started/executed upon logout (cmd logout) of this user. It worked fine on a debian-based system with systemd v232.



      My example contains three elements:



      1. Modify the existing user@.service to make it start an instance of a service
        (helperunit@.service) which binds to user@.service. This way two
        things are achieved:

        • you get instantiated services which can easily be linked to users (for logging, etc)

        • the helper service is stopped when the user logs out



      # /lib/systemd/system/user@.service

      [Unit]
      Description=User Manager for UID %i
      After=systemd-user-sessions.service

      [Service]
      User=%i
      PAMName=systemd-user
      Type=notify
      ExecStart=-/lib/systemd/systemd --user
      Slice=user-%i.slice
      KillMode=mixed
      Delegate=yes
      TasksMax=infinity
      TimeoutStopSec=120s

      # /etc/systemd/system/user@.service.d/afterlogout.conf
      [Service]
      Environment="USERUID=%i"
      ExecStartPost=+/bin/systemctl start helperunit@$USERUID.service


      1. Create the helperunit service file (bound to user@.service), which is used to start a second service (afterlogout@.service) at the time it is stopped (i.e. upon user logout)


      # /etc/systemd/system/helperunit@.service

      [Unit]
      Description=helper-to-start-afterlogout-service Service
      BindsTo=user@%i.service

      [Service]
      Environment="USERUID=%i"
      RemainAfterExit=yes
      Type=oneshot
      ExecStart=/bin/true
      ExecStopPost=/bin/systemctl start afterlogout@$USERUID.service

      [Install]


      1. Create a second service unit file which runs your script after user logout


      # /etc/systemd/system/afterlogout@.service

      [Unit]
      Description=trigger-script-after-user-logout Service
      Before=reboot.target shutdown.target

      [Service]
      Environment="USERUID=%i"
      Type=oneshot
      ExecStart=/bin/echo "user UID=$USERUID has logged out"

      [Install]


      The way this is set up the service instances will have the user UID in their name (for example, afterlogout@1000.service)






      share|improve this answer




























        1














        1










        1









        You could go several routes to start a script after user logout. There may be more but here are the two I am aware of



        1. use gdm to start a script (placed in /etc/gdm/PostSession) after
          logout. More on this
          here.

        2. Use two new separate systemd services. The second one should be started by the first one at the time the first one stops. Set up the first service to stop at user logout by binding it to the already existing user session service (user@.service).

        I am not familiar with the details or merits of the first route, so I will only describe the second route.



        In a quick test I logged in as a different user on a different tty and checked that my process (a simple echo message) was successfully started/executed upon logout (cmd logout) of this user. It worked fine on a debian-based system with systemd v232.



        My example contains three elements:



        1. Modify the existing user@.service to make it start an instance of a service
          (helperunit@.service) which binds to user@.service. This way two
          things are achieved:

          • you get instantiated services which can easily be linked to users (for logging, etc)

          • the helper service is stopped when the user logs out



        # /lib/systemd/system/user@.service

        [Unit]
        Description=User Manager for UID %i
        After=systemd-user-sessions.service

        [Service]
        User=%i
        PAMName=systemd-user
        Type=notify
        ExecStart=-/lib/systemd/systemd --user
        Slice=user-%i.slice
        KillMode=mixed
        Delegate=yes
        TasksMax=infinity
        TimeoutStopSec=120s

        # /etc/systemd/system/user@.service.d/afterlogout.conf
        [Service]
        Environment="USERUID=%i"
        ExecStartPost=+/bin/systemctl start helperunit@$USERUID.service


        1. Create the helperunit service file (bound to user@.service), which is used to start a second service (afterlogout@.service) at the time it is stopped (i.e. upon user logout)


        # /etc/systemd/system/helperunit@.service

        [Unit]
        Description=helper-to-start-afterlogout-service Service
        BindsTo=user@%i.service

        [Service]
        Environment="USERUID=%i"
        RemainAfterExit=yes
        Type=oneshot
        ExecStart=/bin/true
        ExecStopPost=/bin/systemctl start afterlogout@$USERUID.service

        [Install]


        1. Create a second service unit file which runs your script after user logout


        # /etc/systemd/system/afterlogout@.service

        [Unit]
        Description=trigger-script-after-user-logout Service
        Before=reboot.target shutdown.target

        [Service]
        Environment="USERUID=%i"
        Type=oneshot
        ExecStart=/bin/echo "user UID=$USERUID has logged out"

        [Install]


        The way this is set up the service instances will have the user UID in their name (for example, afterlogout@1000.service)






        share|improve this answer














        You could go several routes to start a script after user logout. There may be more but here are the two I am aware of



        1. use gdm to start a script (placed in /etc/gdm/PostSession) after
          logout. More on this
          here.

        2. Use two new separate systemd services. The second one should be started by the first one at the time the first one stops. Set up the first service to stop at user logout by binding it to the already existing user session service (user@.service).

        I am not familiar with the details or merits of the first route, so I will only describe the second route.



        In a quick test I logged in as a different user on a different tty and checked that my process (a simple echo message) was successfully started/executed upon logout (cmd logout) of this user. It worked fine on a debian-based system with systemd v232.



        My example contains three elements:



        1. Modify the existing user@.service to make it start an instance of a service
          (helperunit@.service) which binds to user@.service. This way two
          things are achieved:

          • you get instantiated services which can easily be linked to users (for logging, etc)

          • the helper service is stopped when the user logs out



        # /lib/systemd/system/user@.service

        [Unit]
        Description=User Manager for UID %i
        After=systemd-user-sessions.service

        [Service]
        User=%i
        PAMName=systemd-user
        Type=notify
        ExecStart=-/lib/systemd/systemd --user
        Slice=user-%i.slice
        KillMode=mixed
        Delegate=yes
        TasksMax=infinity
        TimeoutStopSec=120s

        # /etc/systemd/system/user@.service.d/afterlogout.conf
        [Service]
        Environment="USERUID=%i"
        ExecStartPost=+/bin/systemctl start helperunit@$USERUID.service


        1. Create the helperunit service file (bound to user@.service), which is used to start a second service (afterlogout@.service) at the time it is stopped (i.e. upon user logout)


        # /etc/systemd/system/helperunit@.service

        [Unit]
        Description=helper-to-start-afterlogout-service Service
        BindsTo=user@%i.service

        [Service]
        Environment="USERUID=%i"
        RemainAfterExit=yes
        Type=oneshot
        ExecStart=/bin/true
        ExecStopPost=/bin/systemctl start afterlogout@$USERUID.service

        [Install]


        1. Create a second service unit file which runs your script after user logout


        # /etc/systemd/system/afterlogout@.service

        [Unit]
        Description=trigger-script-after-user-logout Service
        Before=reboot.target shutdown.target

        [Service]
        Environment="USERUID=%i"
        Type=oneshot
        ExecStart=/bin/echo "user UID=$USERUID has logged out"

        [Install]


        The way this is set up the service instances will have the user UID in their name (for example, afterlogout@1000.service)







        share|improve this answer













        share|improve this answer




        share|improve this answer










        answered Sep 2 at 11:05









        yesnoyesno

        815 bronze badges




        815 bronze badges































            draft saved

            draft discarded















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1158046%2fstart-the-service-after-the-user-logs-off%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

            Tamil (spriik) Luke uk diar | Nawigatjuun

            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

            Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?