How to run a systemd service when a user logs in?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
0
down vote

favorite












How do I create a daemon that will execute a script at login ? I have an OpenVPN connection that builds a tunnel at start up.I created a .service file that only executes my script After the the openvpn services are up. This daemon fails on startup I believe because the connection isn't fully up or the configuration is just plain invalid. my .service file is in /etc/systemd/system/ directory. I can do a systemctl start mount and it runs just fine if manually started. Can anybody help me out on this ?



[Unit]
Description=Run script to mount drive
Required=openvpn@opencvpn.service
After=openvpn@openvpn.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/sh -c "/path/to/script/somescript.sh start"
ExecStop=/bin/sh -c "/path/to/script/somescript.sh stop"

[Install]
WantedBy=multi-user.target


This is the code I am trying to execute.



#!/bin/bash
2
3 function start
4
5
6 mount -t cifs -o username=guest,password=guest //192.xxx.xxx.xxx/share /mnt/share
7
8
9 function stop
10
11 umount /mnt/share
12
13
14
15 case $1 in
16 start|stop)
17 $1
18 ;;
19 esac









share|improve this question

























    up vote
    0
    down vote

    favorite












    How do I create a daemon that will execute a script at login ? I have an OpenVPN connection that builds a tunnel at start up.I created a .service file that only executes my script After the the openvpn services are up. This daemon fails on startup I believe because the connection isn't fully up or the configuration is just plain invalid. my .service file is in /etc/systemd/system/ directory. I can do a systemctl start mount and it runs just fine if manually started. Can anybody help me out on this ?



    [Unit]
    Description=Run script to mount drive
    Required=openvpn@opencvpn.service
    After=openvpn@openvpn.service

    [Service]
    Type=oneshot
    RemainAfterExit=true
    ExecStart=/bin/sh -c "/path/to/script/somescript.sh start"
    ExecStop=/bin/sh -c "/path/to/script/somescript.sh stop"

    [Install]
    WantedBy=multi-user.target


    This is the code I am trying to execute.



    #!/bin/bash
    2
    3 function start
    4
    5
    6 mount -t cifs -o username=guest,password=guest //192.xxx.xxx.xxx/share /mnt/share
    7
    8
    9 function stop
    10
    11 umount /mnt/share
    12
    13
    14
    15 case $1 in
    16 start|stop)
    17 $1
    18 ;;
    19 esac









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      How do I create a daemon that will execute a script at login ? I have an OpenVPN connection that builds a tunnel at start up.I created a .service file that only executes my script After the the openvpn services are up. This daemon fails on startup I believe because the connection isn't fully up or the configuration is just plain invalid. my .service file is in /etc/systemd/system/ directory. I can do a systemctl start mount and it runs just fine if manually started. Can anybody help me out on this ?



      [Unit]
      Description=Run script to mount drive
      Required=openvpn@opencvpn.service
      After=openvpn@openvpn.service

      [Service]
      Type=oneshot
      RemainAfterExit=true
      ExecStart=/bin/sh -c "/path/to/script/somescript.sh start"
      ExecStop=/bin/sh -c "/path/to/script/somescript.sh stop"

      [Install]
      WantedBy=multi-user.target


      This is the code I am trying to execute.



      #!/bin/bash
      2
      3 function start
      4
      5
      6 mount -t cifs -o username=guest,password=guest //192.xxx.xxx.xxx/share /mnt/share
      7
      8
      9 function stop
      10
      11 umount /mnt/share
      12
      13
      14
      15 case $1 in
      16 start|stop)
      17 $1
      18 ;;
      19 esac









      share|improve this question













      How do I create a daemon that will execute a script at login ? I have an OpenVPN connection that builds a tunnel at start up.I created a .service file that only executes my script After the the openvpn services are up. This daemon fails on startup I believe because the connection isn't fully up or the configuration is just plain invalid. my .service file is in /etc/systemd/system/ directory. I can do a systemctl start mount and it runs just fine if manually started. Can anybody help me out on this ?



      [Unit]
      Description=Run script to mount drive
      Required=openvpn@opencvpn.service
      After=openvpn@openvpn.service

      [Service]
      Type=oneshot
      RemainAfterExit=true
      ExecStart=/bin/sh -c "/path/to/script/somescript.sh start"
      ExecStop=/bin/sh -c "/path/to/script/somescript.sh stop"

      [Install]
      WantedBy=multi-user.target


      This is the code I am trying to execute.



      #!/bin/bash
      2
      3 function start
      4
      5
      6 mount -t cifs -o username=guest,password=guest //192.xxx.xxx.xxx/share /mnt/share
      7
      8
      9 function stop
      10
      11 umount /mnt/share
      12
      13
      14
      15 case $1 in
      16 start|stop)
      17 $1
      18 ;;
      19 esac






      systemd






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 20 at 0:31









      Nate Davis

      11




      11

























          active

          oldest

          votes











          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',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          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%2f1017472%2fhow-to-run-a-systemd-service-when-a-user-logs-in%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1017472%2fhow-to-run-a-systemd-service-when-a-user-logs-in%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Which professions warranted travel in Medieval times?

          How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

          Trouble downloading packages list due to a “Hash sum mismatch” error