Shell program to just open a character driver and wait












5














What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
Echo/touch seem to just open and close the device immediately after performing the operation. Cat does not seem to work.



I am using a C application to do the same but was wondering if shell script has some provision for it










share|improve this question



























    5














    What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
    Echo/touch seem to just open and close the device immediately after performing the operation. Cat does not seem to work.



    I am using a C application to do the same but was wondering if shell script has some provision for it










    share|improve this question

























      5












      5








      5


      1





      What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
      Echo/touch seem to just open and close the device immediately after performing the operation. Cat does not seem to work.



      I am using a C application to do the same but was wondering if shell script has some provision for it










      share|improve this question













      What inspired this question is that I am testing the functionality of watchdog device and I was thinking if there is a shell inbuilt command to just open the device and do nothing/wait until terminated?
      Echo/touch seem to just open and close the device immediately after performing the operation. Cat does not seem to work.



      I am using a C application to do the same but was wondering if shell script has some provision for it







      linux bash shell-script






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 10 at 13:14









      yashC

      557




      557






















          2 Answers
          2






          active

          oldest

          votes


















          11














          In Bourne-like shells,



          exec 3< "$device"


          Opens the device on file descriptor 3 of the shell.



          That would be more or less equivalent to C's:



          fd = open(device, O_RDONLY);
          if (fd < 0) handler_error(...);
          if (fd != 3) { dup2(fd, 3); close(fd); }


          (ksh93 also does a fcntl(3, F_SETFD, FD_CLOEXEC) on that fd).



          To close it: exec 3<&-



          In zsh, ksh93 and bash, the equivalent of fd = open(device, O_RDONLY) could also be written as:



          exec {fd}< "$device"


          Where the file descriptor would be the first free one above 9 and stored in $fd.



          To close it: exec {fd}<&-



          Replace < with > for O_WRONLY|O_CREAT|O_TRUNC, and with <> for O_RDWR|O_CREAT and >> for O_WRONLY|O_CREAT|O_APPEND.



          zsh also has a sysopen builtin (in the zsh/system module) where you can specify the flags exactly.



          Note that in POSIX compliant shells, exec being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command command.



          if command exec 3< "$device"; then
          do-what-you-need-to-do
          else
          handle-the-error-yourself
          fi





          share|improve this answer































            2














            while sleep 3600; do :; done >/dev/your_watchdog


            I gather (from the echo and touch working and the cat failing) that the device should be open in write only mode.






            share|improve this answer





















            • sleep 1e99 >/dev/your_watchdog would also work, assuming you're not planning on being after 10 to the 91 years or so...
              – Digital Trauma
              Dec 10 at 20:38












            • sleep: invalid number '1e99' ;-)
              – pizdelect
              Dec 11 at 2:03











            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%2f487113%2fshell-program-to-just-open-a-character-driver-and-wait%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









            11














            In Bourne-like shells,



            exec 3< "$device"


            Opens the device on file descriptor 3 of the shell.



            That would be more or less equivalent to C's:



            fd = open(device, O_RDONLY);
            if (fd < 0) handler_error(...);
            if (fd != 3) { dup2(fd, 3); close(fd); }


            (ksh93 also does a fcntl(3, F_SETFD, FD_CLOEXEC) on that fd).



            To close it: exec 3<&-



            In zsh, ksh93 and bash, the equivalent of fd = open(device, O_RDONLY) could also be written as:



            exec {fd}< "$device"


            Where the file descriptor would be the first free one above 9 and stored in $fd.



            To close it: exec {fd}<&-



            Replace < with > for O_WRONLY|O_CREAT|O_TRUNC, and with <> for O_RDWR|O_CREAT and >> for O_WRONLY|O_CREAT|O_APPEND.



            zsh also has a sysopen builtin (in the zsh/system module) where you can specify the flags exactly.



            Note that in POSIX compliant shells, exec being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command command.



            if command exec 3< "$device"; then
            do-what-you-need-to-do
            else
            handle-the-error-yourself
            fi





            share|improve this answer




























              11














              In Bourne-like shells,



              exec 3< "$device"


              Opens the device on file descriptor 3 of the shell.



              That would be more or less equivalent to C's:



              fd = open(device, O_RDONLY);
              if (fd < 0) handler_error(...);
              if (fd != 3) { dup2(fd, 3); close(fd); }


              (ksh93 also does a fcntl(3, F_SETFD, FD_CLOEXEC) on that fd).



              To close it: exec 3<&-



              In zsh, ksh93 and bash, the equivalent of fd = open(device, O_RDONLY) could also be written as:



              exec {fd}< "$device"


              Where the file descriptor would be the first free one above 9 and stored in $fd.



              To close it: exec {fd}<&-



              Replace < with > for O_WRONLY|O_CREAT|O_TRUNC, and with <> for O_RDWR|O_CREAT and >> for O_WRONLY|O_CREAT|O_APPEND.



              zsh also has a sysopen builtin (in the zsh/system module) where you can specify the flags exactly.



              Note that in POSIX compliant shells, exec being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command command.



              if command exec 3< "$device"; then
              do-what-you-need-to-do
              else
              handle-the-error-yourself
              fi





              share|improve this answer


























                11












                11








                11






                In Bourne-like shells,



                exec 3< "$device"


                Opens the device on file descriptor 3 of the shell.



                That would be more or less equivalent to C's:



                fd = open(device, O_RDONLY);
                if (fd < 0) handler_error(...);
                if (fd != 3) { dup2(fd, 3); close(fd); }


                (ksh93 also does a fcntl(3, F_SETFD, FD_CLOEXEC) on that fd).



                To close it: exec 3<&-



                In zsh, ksh93 and bash, the equivalent of fd = open(device, O_RDONLY) could also be written as:



                exec {fd}< "$device"


                Where the file descriptor would be the first free one above 9 and stored in $fd.



                To close it: exec {fd}<&-



                Replace < with > for O_WRONLY|O_CREAT|O_TRUNC, and with <> for O_RDWR|O_CREAT and >> for O_WRONLY|O_CREAT|O_APPEND.



                zsh also has a sysopen builtin (in the zsh/system module) where you can specify the flags exactly.



                Note that in POSIX compliant shells, exec being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command command.



                if command exec 3< "$device"; then
                do-what-you-need-to-do
                else
                handle-the-error-yourself
                fi





                share|improve this answer














                In Bourne-like shells,



                exec 3< "$device"


                Opens the device on file descriptor 3 of the shell.



                That would be more or less equivalent to C's:



                fd = open(device, O_RDONLY);
                if (fd < 0) handler_error(...);
                if (fd != 3) { dup2(fd, 3); close(fd); }


                (ksh93 also does a fcntl(3, F_SETFD, FD_CLOEXEC) on that fd).



                To close it: exec 3<&-



                In zsh, ksh93 and bash, the equivalent of fd = open(device, O_RDONLY) could also be written as:



                exec {fd}< "$device"


                Where the file descriptor would be the first free one above 9 and stored in $fd.



                To close it: exec {fd}<&-



                Replace < with > for O_WRONLY|O_CREAT|O_TRUNC, and with <> for O_RDWR|O_CREAT and >> for O_WRONLY|O_CREAT|O_APPEND.



                zsh also has a sysopen builtin (in the zsh/system module) where you can specify the flags exactly.



                Note that in POSIX compliant shells, exec being a special builtin, if the file can't be opened, it's a fatal error when non-interactive (it exits the script). You can disabled that by using the command command.



                if command exec 3< "$device"; then
                do-what-you-need-to-do
                else
                handle-the-error-yourself
                fi






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 10 at 14:08

























                answered Dec 10 at 13:18









                Stéphane Chazelas

                298k54563910




                298k54563910

























                    2














                    while sleep 3600; do :; done >/dev/your_watchdog


                    I gather (from the echo and touch working and the cat failing) that the device should be open in write only mode.






                    share|improve this answer





















                    • sleep 1e99 >/dev/your_watchdog would also work, assuming you're not planning on being after 10 to the 91 years or so...
                      – Digital Trauma
                      Dec 10 at 20:38












                    • sleep: invalid number '1e99' ;-)
                      – pizdelect
                      Dec 11 at 2:03
















                    2














                    while sleep 3600; do :; done >/dev/your_watchdog


                    I gather (from the echo and touch working and the cat failing) that the device should be open in write only mode.






                    share|improve this answer





















                    • sleep 1e99 >/dev/your_watchdog would also work, assuming you're not planning on being after 10 to the 91 years or so...
                      – Digital Trauma
                      Dec 10 at 20:38












                    • sleep: invalid number '1e99' ;-)
                      – pizdelect
                      Dec 11 at 2:03














                    2












                    2








                    2






                    while sleep 3600; do :; done >/dev/your_watchdog


                    I gather (from the echo and touch working and the cat failing) that the device should be open in write only mode.






                    share|improve this answer












                    while sleep 3600; do :; done >/dev/your_watchdog


                    I gather (from the echo and touch working and the cat failing) that the device should be open in write only mode.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 10 at 14:45









                    pizdelect

                    31216




                    31216












                    • sleep 1e99 >/dev/your_watchdog would also work, assuming you're not planning on being after 10 to the 91 years or so...
                      – Digital Trauma
                      Dec 10 at 20:38












                    • sleep: invalid number '1e99' ;-)
                      – pizdelect
                      Dec 11 at 2:03


















                    • sleep 1e99 >/dev/your_watchdog would also work, assuming you're not planning on being after 10 to the 91 years or so...
                      – Digital Trauma
                      Dec 10 at 20:38












                    • sleep: invalid number '1e99' ;-)
                      – pizdelect
                      Dec 11 at 2:03
















                    sleep 1e99 >/dev/your_watchdog would also work, assuming you're not planning on being after 10 to the 91 years or so...
                    – Digital Trauma
                    Dec 10 at 20:38






                    sleep 1e99 >/dev/your_watchdog would also work, assuming you're not planning on being after 10 to the 91 years or so...
                    – Digital Trauma
                    Dec 10 at 20:38














                    sleep: invalid number '1e99' ;-)
                    – pizdelect
                    Dec 11 at 2:03




                    sleep: invalid number '1e99' ;-)
                    – pizdelect
                    Dec 11 at 2:03


















                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f487113%2fshell-program-to-just-open-a-character-driver-and-wait%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

                    flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                    Mangá

                    Eduardo VII do Reino Unido