how to properly schedule a task that schedules a task with 'at' (atd) ? or how to properly quote/escape in...












1














I am looking for a way to create a command line using 'at' (atd) to schedule a task, that schedules a task to do "more stuff" after a reboot (one time task).
What I am looking for right now is a way to make it fly just chaining together "at" schedules.



My problem is, that I get somehow lost with the quoting/escaping in bash.



what I got so far and what works on a manual execution is:



echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes ;
#do admin stuff before reboot in less than 5 mins here ;
touch /fastboot ; reboot


as mentioned, that works fine.
I now would like to encapsulate the whole thing into ANOTHER NEW 'AT' COMMAND, so that the former command line can be scheduled to a specific starting time and no longer requires a manual execution.



I assume it must be possible to encapsulate/include the whole thing into another "at" command, so I would like to find out how to properly quote/escape it so that it will work.



I tried this (not working):



echo "echo "somecommand -someOptions 2>&1 | mail -s \"$HOST after reboot: somecommand -someOptions\" my.mail@address.com" | at now +5 minutes ;
#do admin stuff before reboot in less than 5 mins here ;
touch /fastboot ; reboot ; " | at now + 1 minute


I see that it schedules 2 jobs, does the admin stuff before reboot and reboots, but the 2nd (later) at job is scrambled/crashed because of some bad quoting/escaping I guess. I would like to know and learn more about what is going on here and where I did wrong on the escaping. I guess I could use single quotes on the outer at, since in this case I don't need any variable expansions, but let's assume that I would like to use variable expansion there, too. In that case, how should I escape/quote this thing properly?










share|improve this question





























    1














    I am looking for a way to create a command line using 'at' (atd) to schedule a task, that schedules a task to do "more stuff" after a reboot (one time task).
    What I am looking for right now is a way to make it fly just chaining together "at" schedules.



    My problem is, that I get somehow lost with the quoting/escaping in bash.



    what I got so far and what works on a manual execution is:



    echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes ;
    #do admin stuff before reboot in less than 5 mins here ;
    touch /fastboot ; reboot


    as mentioned, that works fine.
    I now would like to encapsulate the whole thing into ANOTHER NEW 'AT' COMMAND, so that the former command line can be scheduled to a specific starting time and no longer requires a manual execution.



    I assume it must be possible to encapsulate/include the whole thing into another "at" command, so I would like to find out how to properly quote/escape it so that it will work.



    I tried this (not working):



    echo "echo "somecommand -someOptions 2>&1 | mail -s \"$HOST after reboot: somecommand -someOptions\" my.mail@address.com" | at now +5 minutes ;
    #do admin stuff before reboot in less than 5 mins here ;
    touch /fastboot ; reboot ; " | at now + 1 minute


    I see that it schedules 2 jobs, does the admin stuff before reboot and reboots, but the 2nd (later) at job is scrambled/crashed because of some bad quoting/escaping I guess. I would like to know and learn more about what is going on here and where I did wrong on the escaping. I guess I could use single quotes on the outer at, since in this case I don't need any variable expansions, but let's assume that I would like to use variable expansion there, too. In that case, how should I escape/quote this thing properly?










    share|improve this question



























      1












      1








      1







      I am looking for a way to create a command line using 'at' (atd) to schedule a task, that schedules a task to do "more stuff" after a reboot (one time task).
      What I am looking for right now is a way to make it fly just chaining together "at" schedules.



      My problem is, that I get somehow lost with the quoting/escaping in bash.



      what I got so far and what works on a manual execution is:



      echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes ;
      #do admin stuff before reboot in less than 5 mins here ;
      touch /fastboot ; reboot


      as mentioned, that works fine.
      I now would like to encapsulate the whole thing into ANOTHER NEW 'AT' COMMAND, so that the former command line can be scheduled to a specific starting time and no longer requires a manual execution.



      I assume it must be possible to encapsulate/include the whole thing into another "at" command, so I would like to find out how to properly quote/escape it so that it will work.



      I tried this (not working):



      echo "echo "somecommand -someOptions 2>&1 | mail -s \"$HOST after reboot: somecommand -someOptions\" my.mail@address.com" | at now +5 minutes ;
      #do admin stuff before reboot in less than 5 mins here ;
      touch /fastboot ; reboot ; " | at now + 1 minute


      I see that it schedules 2 jobs, does the admin stuff before reboot and reboots, but the 2nd (later) at job is scrambled/crashed because of some bad quoting/escaping I guess. I would like to know and learn more about what is going on here and where I did wrong on the escaping. I guess I could use single quotes on the outer at, since in this case I don't need any variable expansions, but let's assume that I would like to use variable expansion there, too. In that case, how should I escape/quote this thing properly?










      share|improve this question















      I am looking for a way to create a command line using 'at' (atd) to schedule a task, that schedules a task to do "more stuff" after a reboot (one time task).
      What I am looking for right now is a way to make it fly just chaining together "at" schedules.



      My problem is, that I get somehow lost with the quoting/escaping in bash.



      what I got so far and what works on a manual execution is:



      echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes ;
      #do admin stuff before reboot in less than 5 mins here ;
      touch /fastboot ; reboot


      as mentioned, that works fine.
      I now would like to encapsulate the whole thing into ANOTHER NEW 'AT' COMMAND, so that the former command line can be scheduled to a specific starting time and no longer requires a manual execution.



      I assume it must be possible to encapsulate/include the whole thing into another "at" command, so I would like to find out how to properly quote/escape it so that it will work.



      I tried this (not working):



      echo "echo "somecommand -someOptions 2>&1 | mail -s \"$HOST after reboot: somecommand -someOptions\" my.mail@address.com" | at now +5 minutes ;
      #do admin stuff before reboot in less than 5 mins here ;
      touch /fastboot ; reboot ; " | at now + 1 minute


      I see that it schedules 2 jobs, does the admin stuff before reboot and reboots, but the 2nd (later) at job is scrambled/crashed because of some bad quoting/escaping I guess. I would like to know and learn more about what is going on here and where I did wrong on the escaping. I guess I could use single quotes on the outer at, since in this case I don't need any variable expansions, but let's assume that I would like to use variable expansion there, too. In that case, how should I escape/quote this thing properly?







      bash scripting quoting escape-characters at






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 15 at 17:12









      Rui F Ribeiro

      38.9k1479129




      38.9k1479129










      asked Dec 6 '16 at 16:35









      Axel Werner

      3671315




      3671315






















          2 Answers
          2






          active

          oldest

          votes


















          3














          You forgot to backslash the double quote when you wrote \" which should be \". But you can avoid this by using a third type of shell quote, the here-document:



          at now + 1 minute <<!
          echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes
          #do admin stuff before reboot in less than 5 mins here
          touch /fastboot ; reboot
          !





          share|improve this answer





















          • Thanks.vNice. Do u have an example if this here-document Version with encapsulation and All in One Line? I forgot to mention it in the Post, but the here-document I was trying First, but failed too. But I tried it All in One Line.
            – Axel Werner
            Dec 7 '16 at 5:50










          • If you tried putting everything on one line and also used a comment in the middle, then the comment will extend right to the end and any following commands on the line will not be done. This is perhaps the problem you have with your 2nd version with the backslashes at the end of line putting the whole echo output on one line. Instead of # you can use : to start a comment (provided it is just text).
            – meuh
            Dec 7 '16 at 7:47






          • 1




            meuh. you were right. The inner quoting i wanted has to be ' \" ' and i now understand it why it failed. ' " ' scapes the double-quotes. ' \" ' are two charakters. an escaped backslash + and escaped double-quote, which result in a proper ' " ' passed to the inner command. while my mistake was ' \" ' is are two charakters too, but its an escaped backslash followed by a UNESCAPED double-quote which closes the whole inner quoting right after the mail command, way to early. thanks
            – Axel Werner
            Dec 9 '16 at 11:05



















          1














          Wrap what you want to do in a script (bash, perl, ...). Debug the script. Tell at to run the script.:



          at now + 5
          /home/user/bin/doit
          ^D





          share|improve this answer





















            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%2f328453%2fhow-to-properly-schedule-a-task-that-schedules-a-task-with-at-atd-or-how-t%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









            3














            You forgot to backslash the double quote when you wrote \" which should be \". But you can avoid this by using a third type of shell quote, the here-document:



            at now + 1 minute <<!
            echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes
            #do admin stuff before reboot in less than 5 mins here
            touch /fastboot ; reboot
            !





            share|improve this answer





















            • Thanks.vNice. Do u have an example if this here-document Version with encapsulation and All in One Line? I forgot to mention it in the Post, but the here-document I was trying First, but failed too. But I tried it All in One Line.
              – Axel Werner
              Dec 7 '16 at 5:50










            • If you tried putting everything on one line and also used a comment in the middle, then the comment will extend right to the end and any following commands on the line will not be done. This is perhaps the problem you have with your 2nd version with the backslashes at the end of line putting the whole echo output on one line. Instead of # you can use : to start a comment (provided it is just text).
              – meuh
              Dec 7 '16 at 7:47






            • 1




              meuh. you were right. The inner quoting i wanted has to be ' \" ' and i now understand it why it failed. ' " ' scapes the double-quotes. ' \" ' are two charakters. an escaped backslash + and escaped double-quote, which result in a proper ' " ' passed to the inner command. while my mistake was ' \" ' is are two charakters too, but its an escaped backslash followed by a UNESCAPED double-quote which closes the whole inner quoting right after the mail command, way to early. thanks
              – Axel Werner
              Dec 9 '16 at 11:05
















            3














            You forgot to backslash the double quote when you wrote \" which should be \". But you can avoid this by using a third type of shell quote, the here-document:



            at now + 1 minute <<!
            echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes
            #do admin stuff before reboot in less than 5 mins here
            touch /fastboot ; reboot
            !





            share|improve this answer





















            • Thanks.vNice. Do u have an example if this here-document Version with encapsulation and All in One Line? I forgot to mention it in the Post, but the here-document I was trying First, but failed too. But I tried it All in One Line.
              – Axel Werner
              Dec 7 '16 at 5:50










            • If you tried putting everything on one line and also used a comment in the middle, then the comment will extend right to the end and any following commands on the line will not be done. This is perhaps the problem you have with your 2nd version with the backslashes at the end of line putting the whole echo output on one line. Instead of # you can use : to start a comment (provided it is just text).
              – meuh
              Dec 7 '16 at 7:47






            • 1




              meuh. you were right. The inner quoting i wanted has to be ' \" ' and i now understand it why it failed. ' " ' scapes the double-quotes. ' \" ' are two charakters. an escaped backslash + and escaped double-quote, which result in a proper ' " ' passed to the inner command. while my mistake was ' \" ' is are two charakters too, but its an escaped backslash followed by a UNESCAPED double-quote which closes the whole inner quoting right after the mail command, way to early. thanks
              – Axel Werner
              Dec 9 '16 at 11:05














            3












            3








            3






            You forgot to backslash the double quote when you wrote \" which should be \". But you can avoid this by using a third type of shell quote, the here-document:



            at now + 1 minute <<!
            echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes
            #do admin stuff before reboot in less than 5 mins here
            touch /fastboot ; reboot
            !





            share|improve this answer












            You forgot to backslash the double quote when you wrote \" which should be \". But you can avoid this by using a third type of shell quote, the here-document:



            at now + 1 minute <<!
            echo "somecommand -someOptions 2>&1 | mail -s "$HOST after reboot: somecommand -someOptions" my.mail@address.com" | at now +5 minutes
            #do admin stuff before reboot in less than 5 mins here
            touch /fastboot ; reboot
            !






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 6 '16 at 21:44









            meuh

            31.4k11854




            31.4k11854












            • Thanks.vNice. Do u have an example if this here-document Version with encapsulation and All in One Line? I forgot to mention it in the Post, but the here-document I was trying First, but failed too. But I tried it All in One Line.
              – Axel Werner
              Dec 7 '16 at 5:50










            • If you tried putting everything on one line and also used a comment in the middle, then the comment will extend right to the end and any following commands on the line will not be done. This is perhaps the problem you have with your 2nd version with the backslashes at the end of line putting the whole echo output on one line. Instead of # you can use : to start a comment (provided it is just text).
              – meuh
              Dec 7 '16 at 7:47






            • 1




              meuh. you were right. The inner quoting i wanted has to be ' \" ' and i now understand it why it failed. ' " ' scapes the double-quotes. ' \" ' are two charakters. an escaped backslash + and escaped double-quote, which result in a proper ' " ' passed to the inner command. while my mistake was ' \" ' is are two charakters too, but its an escaped backslash followed by a UNESCAPED double-quote which closes the whole inner quoting right after the mail command, way to early. thanks
              – Axel Werner
              Dec 9 '16 at 11:05


















            • Thanks.vNice. Do u have an example if this here-document Version with encapsulation and All in One Line? I forgot to mention it in the Post, but the here-document I was trying First, but failed too. But I tried it All in One Line.
              – Axel Werner
              Dec 7 '16 at 5:50










            • If you tried putting everything on one line and also used a comment in the middle, then the comment will extend right to the end and any following commands on the line will not be done. This is perhaps the problem you have with your 2nd version with the backslashes at the end of line putting the whole echo output on one line. Instead of # you can use : to start a comment (provided it is just text).
              – meuh
              Dec 7 '16 at 7:47






            • 1




              meuh. you were right. The inner quoting i wanted has to be ' \" ' and i now understand it why it failed. ' " ' scapes the double-quotes. ' \" ' are two charakters. an escaped backslash + and escaped double-quote, which result in a proper ' " ' passed to the inner command. while my mistake was ' \" ' is are two charakters too, but its an escaped backslash followed by a UNESCAPED double-quote which closes the whole inner quoting right after the mail command, way to early. thanks
              – Axel Werner
              Dec 9 '16 at 11:05
















            Thanks.vNice. Do u have an example if this here-document Version with encapsulation and All in One Line? I forgot to mention it in the Post, but the here-document I was trying First, but failed too. But I tried it All in One Line.
            – Axel Werner
            Dec 7 '16 at 5:50




            Thanks.vNice. Do u have an example if this here-document Version with encapsulation and All in One Line? I forgot to mention it in the Post, but the here-document I was trying First, but failed too. But I tried it All in One Line.
            – Axel Werner
            Dec 7 '16 at 5:50












            If you tried putting everything on one line and also used a comment in the middle, then the comment will extend right to the end and any following commands on the line will not be done. This is perhaps the problem you have with your 2nd version with the backslashes at the end of line putting the whole echo output on one line. Instead of # you can use : to start a comment (provided it is just text).
            – meuh
            Dec 7 '16 at 7:47




            If you tried putting everything on one line and also used a comment in the middle, then the comment will extend right to the end and any following commands on the line will not be done. This is perhaps the problem you have with your 2nd version with the backslashes at the end of line putting the whole echo output on one line. Instead of # you can use : to start a comment (provided it is just text).
            – meuh
            Dec 7 '16 at 7:47




            1




            1




            meuh. you were right. The inner quoting i wanted has to be ' \" ' and i now understand it why it failed. ' " ' scapes the double-quotes. ' \" ' are two charakters. an escaped backslash + and escaped double-quote, which result in a proper ' " ' passed to the inner command. while my mistake was ' \" ' is are two charakters too, but its an escaped backslash followed by a UNESCAPED double-quote which closes the whole inner quoting right after the mail command, way to early. thanks
            – Axel Werner
            Dec 9 '16 at 11:05




            meuh. you were right. The inner quoting i wanted has to be ' \" ' and i now understand it why it failed. ' " ' scapes the double-quotes. ' \" ' are two charakters. an escaped backslash + and escaped double-quote, which result in a proper ' " ' passed to the inner command. while my mistake was ' \" ' is are two charakters too, but its an escaped backslash followed by a UNESCAPED double-quote which closes the whole inner quoting right after the mail command, way to early. thanks
            – Axel Werner
            Dec 9 '16 at 11:05













            1














            Wrap what you want to do in a script (bash, perl, ...). Debug the script. Tell at to run the script.:



            at now + 5
            /home/user/bin/doit
            ^D





            share|improve this answer


























              1














              Wrap what you want to do in a script (bash, perl, ...). Debug the script. Tell at to run the script.:



              at now + 5
              /home/user/bin/doit
              ^D





              share|improve this answer
























                1












                1








                1






                Wrap what you want to do in a script (bash, perl, ...). Debug the script. Tell at to run the script.:



                at now + 5
                /home/user/bin/doit
                ^D





                share|improve this answer












                Wrap what you want to do in a script (bash, perl, ...). Debug the script. Tell at to run the script.:



                at now + 5
                /home/user/bin/doit
                ^D






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 17 '16 at 16:17









                waltinator

                73048




                73048






























                    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%2f328453%2fhow-to-properly-schedule-a-task-that-schedules-a-task-with-at-atd-or-how-t%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

                    Morgemoulin

                    Scott Moir

                    Souastre