How to run an infinite loop program on startup on a raspberry pi without halting boot up












0















I have a shell script:



#!/bin/bash
while sudo /home/pi/MyCode; do :; done
echo Error with MyCode


which runs a program in an infinite loop. I want to set up the shell script to run at start-up (which I've done with other bash scripts). However, I don't want the script to halt start-up, or prevent me from being to ssh in.



I've seen a lot of people who have had to wipe and remount their SD cards due to infinite loops be created on start-up. How do I prevent this?



I've tried augmenting the code:



#!/bin/bash
while sudo /home/pi/MyCode &; do :; done
echo Error with MyCode


to run MyCode in the background, but I keep getting an error along the lines




unexpected character before ;




Is there a way to run this script on startup without halting my startup? If so, can it be done in a way that I still have the option of SSH'ing and stopping the script at any time?










share|improve this question

























  • I'm not sure i understand the question. I don't have a ' before the #!. #!/bin/bash is the fist line of my bash script.

    – JRogerC
    Sep 18 '14 at 21:25











  • I made a type. I meant a /, which appears to be gone now anyway.

    – lzam
    Sep 18 '14 at 21:26
















0















I have a shell script:



#!/bin/bash
while sudo /home/pi/MyCode; do :; done
echo Error with MyCode


which runs a program in an infinite loop. I want to set up the shell script to run at start-up (which I've done with other bash scripts). However, I don't want the script to halt start-up, or prevent me from being to ssh in.



I've seen a lot of people who have had to wipe and remount their SD cards due to infinite loops be created on start-up. How do I prevent this?



I've tried augmenting the code:



#!/bin/bash
while sudo /home/pi/MyCode &; do :; done
echo Error with MyCode


to run MyCode in the background, but I keep getting an error along the lines




unexpected character before ;




Is there a way to run this script on startup without halting my startup? If so, can it be done in a way that I still have the option of SSH'ing and stopping the script at any time?










share|improve this question

























  • I'm not sure i understand the question. I don't have a ' before the #!. #!/bin/bash is the fist line of my bash script.

    – JRogerC
    Sep 18 '14 at 21:25











  • I made a type. I meant a /, which appears to be gone now anyway.

    – lzam
    Sep 18 '14 at 21:26














0












0








0








I have a shell script:



#!/bin/bash
while sudo /home/pi/MyCode; do :; done
echo Error with MyCode


which runs a program in an infinite loop. I want to set up the shell script to run at start-up (which I've done with other bash scripts). However, I don't want the script to halt start-up, or prevent me from being to ssh in.



I've seen a lot of people who have had to wipe and remount their SD cards due to infinite loops be created on start-up. How do I prevent this?



I've tried augmenting the code:



#!/bin/bash
while sudo /home/pi/MyCode &; do :; done
echo Error with MyCode


to run MyCode in the background, but I keep getting an error along the lines




unexpected character before ;




Is there a way to run this script on startup without halting my startup? If so, can it be done in a way that I still have the option of SSH'ing and stopping the script at any time?










share|improve this question
















I have a shell script:



#!/bin/bash
while sudo /home/pi/MyCode; do :; done
echo Error with MyCode


which runs a program in an infinite loop. I want to set up the shell script to run at start-up (which I've done with other bash scripts). However, I don't want the script to halt start-up, or prevent me from being to ssh in.



I've seen a lot of people who have had to wipe and remount their SD cards due to infinite loops be created on start-up. How do I prevent this?



I've tried augmenting the code:



#!/bin/bash
while sudo /home/pi/MyCode &; do :; done
echo Error with MyCode


to run MyCode in the background, but I keep getting an error along the lines




unexpected character before ;




Is there a way to run this script on startup without halting my startup? If so, can it be done in a way that I still have the option of SSH'ing and stopping the script at any time?







linux shell script raspberry-pi infinite-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 18 '14 at 20:34









Scott

15.8k113990




15.8k113990










asked Sep 18 '14 at 20:16









JRogerCJRogerC

111




111













  • I'm not sure i understand the question. I don't have a ' before the #!. #!/bin/bash is the fist line of my bash script.

    – JRogerC
    Sep 18 '14 at 21:25











  • I made a type. I meant a /, which appears to be gone now anyway.

    – lzam
    Sep 18 '14 at 21:26



















  • I'm not sure i understand the question. I don't have a ' before the #!. #!/bin/bash is the fist line of my bash script.

    – JRogerC
    Sep 18 '14 at 21:25











  • I made a type. I meant a /, which appears to be gone now anyway.

    – lzam
    Sep 18 '14 at 21:26

















I'm not sure i understand the question. I don't have a ' before the #!. #!/bin/bash is the fist line of my bash script.

– JRogerC
Sep 18 '14 at 21:25





I'm not sure i understand the question. I don't have a ' before the #!. #!/bin/bash is the fist line of my bash script.

– JRogerC
Sep 18 '14 at 21:25













I made a type. I meant a /, which appears to be gone now anyway.

– lzam
Sep 18 '14 at 21:26





I made a type. I meant a /, which appears to be gone now anyway.

– lzam
Sep 18 '14 at 21:26










2 Answers
2






active

oldest

votes


















0














Remove the :
It generates the syntax error. Alternatively insert a sleep 1 or something similar instead of it, bash can freak out on empty while ... do loops. If you go with this approach you shoukd also lose the &
If you leave it in you will create a new process for every iteration of the loop, grinding your pi to a halt once all your ram is taken.



Also use ./myCode.sh or exec myCode.sh to be shure it actually runs the script.



If you are running raspbian i would also suggest using upstart. This allows for a more controlled way of triggering stuff during boot, like only run once the filesystem is available.



Maybe post the code you want to run also, i can check if there are no other errors. Good for my bash-fu training :-)






share|improve this answer


























  • I had this code running for 36 hours straight without error, so I don't think the : is giving me any problems. However, the code is driving an external piece of hardware, and giving it a second of rest would not be a bad thing.

    – JRogerC
    Sep 18 '14 at 21:24



















0














Put the loop in its own script, running in the background:



startup script:



#!/bin/bash
mainloop.sh &
echo Should get here


mainloop.sh:



#!/bin/bash
while sudo /home/pi/MyCode; do :; done


There's probably a way to do this in one script, I'm not familiar enough with Bash to do it though.






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%2f813666%2fhow-to-run-an-infinite-loop-program-on-startup-on-a-raspberry-pi-without-halting%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









    0














    Remove the :
    It generates the syntax error. Alternatively insert a sleep 1 or something similar instead of it, bash can freak out on empty while ... do loops. If you go with this approach you shoukd also lose the &
    If you leave it in you will create a new process for every iteration of the loop, grinding your pi to a halt once all your ram is taken.



    Also use ./myCode.sh or exec myCode.sh to be shure it actually runs the script.



    If you are running raspbian i would also suggest using upstart. This allows for a more controlled way of triggering stuff during boot, like only run once the filesystem is available.



    Maybe post the code you want to run also, i can check if there are no other errors. Good for my bash-fu training :-)






    share|improve this answer


























    • I had this code running for 36 hours straight without error, so I don't think the : is giving me any problems. However, the code is driving an external piece of hardware, and giving it a second of rest would not be a bad thing.

      – JRogerC
      Sep 18 '14 at 21:24
















    0














    Remove the :
    It generates the syntax error. Alternatively insert a sleep 1 or something similar instead of it, bash can freak out on empty while ... do loops. If you go with this approach you shoukd also lose the &
    If you leave it in you will create a new process for every iteration of the loop, grinding your pi to a halt once all your ram is taken.



    Also use ./myCode.sh or exec myCode.sh to be shure it actually runs the script.



    If you are running raspbian i would also suggest using upstart. This allows for a more controlled way of triggering stuff during boot, like only run once the filesystem is available.



    Maybe post the code you want to run also, i can check if there are no other errors. Good for my bash-fu training :-)






    share|improve this answer


























    • I had this code running for 36 hours straight without error, so I don't think the : is giving me any problems. However, the code is driving an external piece of hardware, and giving it a second of rest would not be a bad thing.

      – JRogerC
      Sep 18 '14 at 21:24














    0












    0








    0







    Remove the :
    It generates the syntax error. Alternatively insert a sleep 1 or something similar instead of it, bash can freak out on empty while ... do loops. If you go with this approach you shoukd also lose the &
    If you leave it in you will create a new process for every iteration of the loop, grinding your pi to a halt once all your ram is taken.



    Also use ./myCode.sh or exec myCode.sh to be shure it actually runs the script.



    If you are running raspbian i would also suggest using upstart. This allows for a more controlled way of triggering stuff during boot, like only run once the filesystem is available.



    Maybe post the code you want to run also, i can check if there are no other errors. Good for my bash-fu training :-)






    share|improve this answer















    Remove the :
    It generates the syntax error. Alternatively insert a sleep 1 or something similar instead of it, bash can freak out on empty while ... do loops. If you go with this approach you shoukd also lose the &
    If you leave it in you will create a new process for every iteration of the loop, grinding your pi to a halt once all your ram is taken.



    Also use ./myCode.sh or exec myCode.sh to be shure it actually runs the script.



    If you are running raspbian i would also suggest using upstart. This allows for a more controlled way of triggering stuff during boot, like only run once the filesystem is available.



    Maybe post the code you want to run also, i can check if there are no other errors. Good for my bash-fu training :-)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 18 '14 at 21:27

























    answered Sep 18 '14 at 21:03









    JakeJake

    343212




    343212













    • I had this code running for 36 hours straight without error, so I don't think the : is giving me any problems. However, the code is driving an external piece of hardware, and giving it a second of rest would not be a bad thing.

      – JRogerC
      Sep 18 '14 at 21:24



















    • I had this code running for 36 hours straight without error, so I don't think the : is giving me any problems. However, the code is driving an external piece of hardware, and giving it a second of rest would not be a bad thing.

      – JRogerC
      Sep 18 '14 at 21:24

















    I had this code running for 36 hours straight without error, so I don't think the : is giving me any problems. However, the code is driving an external piece of hardware, and giving it a second of rest would not be a bad thing.

    – JRogerC
    Sep 18 '14 at 21:24





    I had this code running for 36 hours straight without error, so I don't think the : is giving me any problems. However, the code is driving an external piece of hardware, and giving it a second of rest would not be a bad thing.

    – JRogerC
    Sep 18 '14 at 21:24













    0














    Put the loop in its own script, running in the background:



    startup script:



    #!/bin/bash
    mainloop.sh &
    echo Should get here


    mainloop.sh:



    #!/bin/bash
    while sudo /home/pi/MyCode; do :; done


    There's probably a way to do this in one script, I'm not familiar enough with Bash to do it though.






    share|improve this answer




























      0














      Put the loop in its own script, running in the background:



      startup script:



      #!/bin/bash
      mainloop.sh &
      echo Should get here


      mainloop.sh:



      #!/bin/bash
      while sudo /home/pi/MyCode; do :; done


      There's probably a way to do this in one script, I'm not familiar enough with Bash to do it though.






      share|improve this answer


























        0












        0








        0







        Put the loop in its own script, running in the background:



        startup script:



        #!/bin/bash
        mainloop.sh &
        echo Should get here


        mainloop.sh:



        #!/bin/bash
        while sudo /home/pi/MyCode; do :; done


        There's probably a way to do this in one script, I'm not familiar enough with Bash to do it though.






        share|improve this answer













        Put the loop in its own script, running in the background:



        startup script:



        #!/bin/bash
        mainloop.sh &
        echo Should get here


        mainloop.sh:



        #!/bin/bash
        while sudo /home/pi/MyCode; do :; done


        There's probably a way to do this in one script, I'm not familiar enough with Bash to do it though.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 27 '14 at 14:24









        baochanbaochan

        88968




        88968






























            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%2f813666%2fhow-to-run-an-infinite-loop-program-on-startup-on-a-raspberry-pi-without-halting%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

            How to make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            Touch on Surface Book