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?

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
!
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.