In systemd service file, how do I say “after USB is ready”?












2















I have a program running on Raspbian Stretch, talking to a mobile phone via USB, using a specialized protocol implemented on top of libusb.



I'd like the program to run on startup, so I make up a systemd service file, but not sure what target it should run after:



[Unit]
Description=My Program
After=network.target <-- ???

[Service]
ExecStart=/home/pi/myprogram
User=root

[Install]
WantedBy=multi-user.target


This content can do the job, but what should it be after properly? How could I say "after USB is ready"?



Most info I can find on the web is about setting up udev rules, which I understand is to load a kernel module on seeing a certain device, which I don't think is what I want.



Any help is appreciated.










share|improve this question



























    2















    I have a program running on Raspbian Stretch, talking to a mobile phone via USB, using a specialized protocol implemented on top of libusb.



    I'd like the program to run on startup, so I make up a systemd service file, but not sure what target it should run after:



    [Unit]
    Description=My Program
    After=network.target <-- ???

    [Service]
    ExecStart=/home/pi/myprogram
    User=root

    [Install]
    WantedBy=multi-user.target


    This content can do the job, but what should it be after properly? How could I say "after USB is ready"?



    Most info I can find on the web is about setting up udev rules, which I understand is to load a kernel module on seeing a certain device, which I don't think is what I want.



    Any help is appreciated.










    share|improve this question

























      2












      2








      2


      1






      I have a program running on Raspbian Stretch, talking to a mobile phone via USB, using a specialized protocol implemented on top of libusb.



      I'd like the program to run on startup, so I make up a systemd service file, but not sure what target it should run after:



      [Unit]
      Description=My Program
      After=network.target <-- ???

      [Service]
      ExecStart=/home/pi/myprogram
      User=root

      [Install]
      WantedBy=multi-user.target


      This content can do the job, but what should it be after properly? How could I say "after USB is ready"?



      Most info I can find on the web is about setting up udev rules, which I understand is to load a kernel module on seeing a certain device, which I don't think is what I want.



      Any help is appreciated.










      share|improve this question














      I have a program running on Raspbian Stretch, talking to a mobile phone via USB, using a specialized protocol implemented on top of libusb.



      I'd like the program to run on startup, so I make up a systemd service file, but not sure what target it should run after:



      [Unit]
      Description=My Program
      After=network.target <-- ???

      [Service]
      ExecStart=/home/pi/myprogram
      User=root

      [Install]
      WantedBy=multi-user.target


      This content can do the job, but what should it be after properly? How could I say "after USB is ready"?



      Most info I can find on the web is about setting up udev rules, which I understand is to load a kernel module on seeing a certain device, which I don't think is what I want.



      Any help is appreciated.







      linux usb systemd libusb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 15 '18 at 7:05









      Nick LeeNick Lee

      1134




      1134






















          2 Answers
          2






          active

          oldest

          votes


















          1














          I have not solved this before, but it seems like it could be a good fit for "path-based activation".



          Instead of having an "After=" clause in your service file, you would create a .path file, as described in man systemd.path.



          Find a suitable file under /dev/bus/usb or /sys/bus/usb, whose presence indicates that "USB is up". Then have systemd monitor the file path using the .path file you'll create. The .path file would then activate your .service file when the file exists.






          share|improve this answer
























          • Thank you. I eventually monitor the path /dev/bus/usb/001. That's where Raspberry Pi's USB bus is. For anyone interested in a concrete example, here is another nice answer.

            – Nick Lee
            May 17 '18 at 14:20



















          0














          What I would do is to create a systemd device unit using the an udev rule. E.g.: create /etc/udev/rules.d/20-usb-bus.rules with:



          KERNEL=="usb[1-2]", TAG+="systemd"


          At next boot (or udev rules reload) you will now have your system device unit:



          # systemctl status dev-bus-usb-001-001.device
          ● dev-bus-usb-001-001.device - 2.0 root hub
          ...
          # systemctl status dev-bus-usb-002-001.device
          ● dev-bus-usb-002-001.device - 3.0 root hub
          ...


          You can now make your service start after USB bus is ready by adding:



          [Unit]
          ...
          After=dev-bus-usb-001-001.device dev-bus-usb-002-001.device


          to your systemd service.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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/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%2fsuperuser.com%2fquestions%2f1322777%2fin-systemd-service-file-how-do-i-say-after-usb-is-ready%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 have not solved this before, but it seems like it could be a good fit for "path-based activation".



            Instead of having an "After=" clause in your service file, you would create a .path file, as described in man systemd.path.



            Find a suitable file under /dev/bus/usb or /sys/bus/usb, whose presence indicates that "USB is up". Then have systemd monitor the file path using the .path file you'll create. The .path file would then activate your .service file when the file exists.






            share|improve this answer
























            • Thank you. I eventually monitor the path /dev/bus/usb/001. That's where Raspberry Pi's USB bus is. For anyone interested in a concrete example, here is another nice answer.

              – Nick Lee
              May 17 '18 at 14:20
















            1














            I have not solved this before, but it seems like it could be a good fit for "path-based activation".



            Instead of having an "After=" clause in your service file, you would create a .path file, as described in man systemd.path.



            Find a suitable file under /dev/bus/usb or /sys/bus/usb, whose presence indicates that "USB is up". Then have systemd monitor the file path using the .path file you'll create. The .path file would then activate your .service file when the file exists.






            share|improve this answer
























            • Thank you. I eventually monitor the path /dev/bus/usb/001. That's where Raspberry Pi's USB bus is. For anyone interested in a concrete example, here is another nice answer.

              – Nick Lee
              May 17 '18 at 14:20














            1












            1








            1







            I have not solved this before, but it seems like it could be a good fit for "path-based activation".



            Instead of having an "After=" clause in your service file, you would create a .path file, as described in man systemd.path.



            Find a suitable file under /dev/bus/usb or /sys/bus/usb, whose presence indicates that "USB is up". Then have systemd monitor the file path using the .path file you'll create. The .path file would then activate your .service file when the file exists.






            share|improve this answer













            I have not solved this before, but it seems like it could be a good fit for "path-based activation".



            Instead of having an "After=" clause in your service file, you would create a .path file, as described in man systemd.path.



            Find a suitable file under /dev/bus/usb or /sys/bus/usb, whose presence indicates that "USB is up". Then have systemd monitor the file path using the .path file you'll create. The .path file would then activate your .service file when the file exists.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 15 '18 at 13:34









            Mark StosbergMark Stosberg

            47726




            47726













            • Thank you. I eventually monitor the path /dev/bus/usb/001. That's where Raspberry Pi's USB bus is. For anyone interested in a concrete example, here is another nice answer.

              – Nick Lee
              May 17 '18 at 14:20



















            • Thank you. I eventually monitor the path /dev/bus/usb/001. That's where Raspberry Pi's USB bus is. For anyone interested in a concrete example, here is another nice answer.

              – Nick Lee
              May 17 '18 at 14:20

















            Thank you. I eventually monitor the path /dev/bus/usb/001. That's where Raspberry Pi's USB bus is. For anyone interested in a concrete example, here is another nice answer.

            – Nick Lee
            May 17 '18 at 14:20





            Thank you. I eventually monitor the path /dev/bus/usb/001. That's where Raspberry Pi's USB bus is. For anyone interested in a concrete example, here is another nice answer.

            – Nick Lee
            May 17 '18 at 14:20













            0














            What I would do is to create a systemd device unit using the an udev rule. E.g.: create /etc/udev/rules.d/20-usb-bus.rules with:



            KERNEL=="usb[1-2]", TAG+="systemd"


            At next boot (or udev rules reload) you will now have your system device unit:



            # systemctl status dev-bus-usb-001-001.device
            ● dev-bus-usb-001-001.device - 2.0 root hub
            ...
            # systemctl status dev-bus-usb-002-001.device
            ● dev-bus-usb-002-001.device - 3.0 root hub
            ...


            You can now make your service start after USB bus is ready by adding:



            [Unit]
            ...
            After=dev-bus-usb-001-001.device dev-bus-usb-002-001.device


            to your systemd service.






            share|improve this answer




























              0














              What I would do is to create a systemd device unit using the an udev rule. E.g.: create /etc/udev/rules.d/20-usb-bus.rules with:



              KERNEL=="usb[1-2]", TAG+="systemd"


              At next boot (or udev rules reload) you will now have your system device unit:



              # systemctl status dev-bus-usb-001-001.device
              ● dev-bus-usb-001-001.device - 2.0 root hub
              ...
              # systemctl status dev-bus-usb-002-001.device
              ● dev-bus-usb-002-001.device - 3.0 root hub
              ...


              You can now make your service start after USB bus is ready by adding:



              [Unit]
              ...
              After=dev-bus-usb-001-001.device dev-bus-usb-002-001.device


              to your systemd service.






              share|improve this answer


























                0












                0








                0







                What I would do is to create a systemd device unit using the an udev rule. E.g.: create /etc/udev/rules.d/20-usb-bus.rules with:



                KERNEL=="usb[1-2]", TAG+="systemd"


                At next boot (or udev rules reload) you will now have your system device unit:



                # systemctl status dev-bus-usb-001-001.device
                ● dev-bus-usb-001-001.device - 2.0 root hub
                ...
                # systemctl status dev-bus-usb-002-001.device
                ● dev-bus-usb-002-001.device - 3.0 root hub
                ...


                You can now make your service start after USB bus is ready by adding:



                [Unit]
                ...
                After=dev-bus-usb-001-001.device dev-bus-usb-002-001.device


                to your systemd service.






                share|improve this answer













                What I would do is to create a systemd device unit using the an udev rule. E.g.: create /etc/udev/rules.d/20-usb-bus.rules with:



                KERNEL=="usb[1-2]", TAG+="systemd"


                At next boot (or udev rules reload) you will now have your system device unit:



                # systemctl status dev-bus-usb-001-001.device
                ● dev-bus-usb-001-001.device - 2.0 root hub
                ...
                # systemctl status dev-bus-usb-002-001.device
                ● dev-bus-usb-002-001.device - 3.0 root hub
                ...


                You can now make your service start after USB bus is ready by adding:



                [Unit]
                ...
                After=dev-bus-usb-001-001.device dev-bus-usb-002-001.device


                to your systemd service.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 25 at 14:53









                DiegoDiego

                1113




                1113






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f1322777%2fin-systemd-service-file-how-do-i-say-after-usb-is-ready%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

                    Сан-Квентин

                    8-я гвардейская общевойсковая армия

                    Алькесар