Run crontab in specific day but only if is in specific weekday












4














I wanna run command with crontab in a specific day, but only if this day is not sunday.
I tried with this




1 0 27 12 1-6 command.sh




but the command run also wednesday december 26th.
How can I tell to crontab run command only the december 27th?



Thanks
Max










share|improve this question
























  • You want the job to run on 27 but that day must not be a sunday but can be any other day!
    – George Udosen
    10 hours ago










  • Try this 1 0 27 * 1-6
    – George Udosen
    10 hours ago










  • @GeorgeUdosen 1 0 27 * 1-6 will run on every 27th plus every Mon…Sat. Cron's syntax is mean.
    – PerlDuck
    9 hours ago
















4














I wanna run command with crontab in a specific day, but only if this day is not sunday.
I tried with this




1 0 27 12 1-6 command.sh




but the command run also wednesday december 26th.
How can I tell to crontab run command only the december 27th?



Thanks
Max










share|improve this question
























  • You want the job to run on 27 but that day must not be a sunday but can be any other day!
    – George Udosen
    10 hours ago










  • Try this 1 0 27 * 1-6
    – George Udosen
    10 hours ago










  • @GeorgeUdosen 1 0 27 * 1-6 will run on every 27th plus every Mon…Sat. Cron's syntax is mean.
    – PerlDuck
    9 hours ago














4












4








4







I wanna run command with crontab in a specific day, but only if this day is not sunday.
I tried with this




1 0 27 12 1-6 command.sh




but the command run also wednesday december 26th.
How can I tell to crontab run command only the december 27th?



Thanks
Max










share|improve this question















I wanna run command with crontab in a specific day, but only if this day is not sunday.
I tried with this




1 0 27 12 1-6 command.sh




but the command run also wednesday december 26th.
How can I tell to crontab run command only the december 27th?



Thanks
Max







cron






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 hours ago

























asked 11 hours ago









maxranzy

312




312












  • You want the job to run on 27 but that day must not be a sunday but can be any other day!
    – George Udosen
    10 hours ago










  • Try this 1 0 27 * 1-6
    – George Udosen
    10 hours ago










  • @GeorgeUdosen 1 0 27 * 1-6 will run on every 27th plus every Mon…Sat. Cron's syntax is mean.
    – PerlDuck
    9 hours ago


















  • You want the job to run on 27 but that day must not be a sunday but can be any other day!
    – George Udosen
    10 hours ago










  • Try this 1 0 27 * 1-6
    – George Udosen
    10 hours ago










  • @GeorgeUdosen 1 0 27 * 1-6 will run on every 27th plus every Mon…Sat. Cron's syntax is mean.
    – PerlDuck
    9 hours ago
















You want the job to run on 27 but that day must not be a sunday but can be any other day!
– George Udosen
10 hours ago




You want the job to run on 27 but that day must not be a sunday but can be any other day!
– George Udosen
10 hours ago












Try this 1 0 27 * 1-6
– George Udosen
10 hours ago




Try this 1 0 27 * 1-6
– George Udosen
10 hours ago












@GeorgeUdosen 1 0 27 * 1-6 will run on every 27th plus every Mon…Sat. Cron's syntax is mean.
– PerlDuck
9 hours ago




@GeorgeUdosen 1 0 27 * 1-6 will run on every 27th plus every Mon…Sat. Cron's syntax is mean.
– PerlDuck
9 hours ago










1 Answer
1






active

oldest

votes


















4














I don't think it is possible with cron alone because the syntax doesn't
allow for "at 27th except on Sundays". It only allows
for "either 27th or non-Sundays (or both)".



I suggest to either change command.sh to immediately exit on non-Sundays
or put that check into the cronjob:



1 0 27 12 *   test $(date +%u) -ne 7   &&  command.sh


At the shell, date +%u returns the day of week (1…7, 1 is Monday). In
a cronjob we have to escape that % sign (%). The command will
check whether the current day is a non-Sunday and only then execute
command.sh.



The cronjob will run on every Dec 27th at 00:01 o'clock,
no matter what day of the week that is, but only for non-Sundays the
command.sh gets executed because the previous test only succeeds
for them.



Note: I sometimes use https://crontab.guru to check my cronjob's
timetables. It's quite handy.
Your definition translates to “At
00:01 on day-of-month 27 and on every day-of-week from Monday through
Saturday in December.”
so it's quite the opposite of what you want.





From the manpage:




Note: The day of a command's execution can be specified by two fields
— day of month, and day of week. If both fields are restricted (i.e.,
aren't *), the command will be run when either field matches the
current time. For example, 30 4 1,15 * 5 would cause a command to
be run at 4:30 am on the 1st and 15th of each month, plus every
Friday. One can, however, achieve the desired result by adding a test
to the command (see the last example in EXAMPLE CRON FILE below).





# Run on every second Saturday of the month
0 4 8-14 * * test $(date +%u) -eq 6 && echo "2nd Saturday"






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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%2faskubuntu.com%2fquestions%2f1104605%2frun-crontab-in-specific-day-but-only-if-is-in-specific-weekday%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    I don't think it is possible with cron alone because the syntax doesn't
    allow for "at 27th except on Sundays". It only allows
    for "either 27th or non-Sundays (or both)".



    I suggest to either change command.sh to immediately exit on non-Sundays
    or put that check into the cronjob:



    1 0 27 12 *   test $(date +%u) -ne 7   &&  command.sh


    At the shell, date +%u returns the day of week (1…7, 1 is Monday). In
    a cronjob we have to escape that % sign (%). The command will
    check whether the current day is a non-Sunday and only then execute
    command.sh.



    The cronjob will run on every Dec 27th at 00:01 o'clock,
    no matter what day of the week that is, but only for non-Sundays the
    command.sh gets executed because the previous test only succeeds
    for them.



    Note: I sometimes use https://crontab.guru to check my cronjob's
    timetables. It's quite handy.
    Your definition translates to “At
    00:01 on day-of-month 27 and on every day-of-week from Monday through
    Saturday in December.”
    so it's quite the opposite of what you want.





    From the manpage:




    Note: The day of a command's execution can be specified by two fields
    — day of month, and day of week. If both fields are restricted (i.e.,
    aren't *), the command will be run when either field matches the
    current time. For example, 30 4 1,15 * 5 would cause a command to
    be run at 4:30 am on the 1st and 15th of each month, plus every
    Friday. One can, however, achieve the desired result by adding a test
    to the command (see the last example in EXAMPLE CRON FILE below).





    # Run on every second Saturday of the month
    0 4 8-14 * * test $(date +%u) -eq 6 && echo "2nd Saturday"






    share|improve this answer




























      4














      I don't think it is possible with cron alone because the syntax doesn't
      allow for "at 27th except on Sundays". It only allows
      for "either 27th or non-Sundays (or both)".



      I suggest to either change command.sh to immediately exit on non-Sundays
      or put that check into the cronjob:



      1 0 27 12 *   test $(date +%u) -ne 7   &&  command.sh


      At the shell, date +%u returns the day of week (1…7, 1 is Monday). In
      a cronjob we have to escape that % sign (%). The command will
      check whether the current day is a non-Sunday and only then execute
      command.sh.



      The cronjob will run on every Dec 27th at 00:01 o'clock,
      no matter what day of the week that is, but only for non-Sundays the
      command.sh gets executed because the previous test only succeeds
      for them.



      Note: I sometimes use https://crontab.guru to check my cronjob's
      timetables. It's quite handy.
      Your definition translates to “At
      00:01 on day-of-month 27 and on every day-of-week from Monday through
      Saturday in December.”
      so it's quite the opposite of what you want.





      From the manpage:




      Note: The day of a command's execution can be specified by two fields
      — day of month, and day of week. If both fields are restricted (i.e.,
      aren't *), the command will be run when either field matches the
      current time. For example, 30 4 1,15 * 5 would cause a command to
      be run at 4:30 am on the 1st and 15th of each month, plus every
      Friday. One can, however, achieve the desired result by adding a test
      to the command (see the last example in EXAMPLE CRON FILE below).





      # Run on every second Saturday of the month
      0 4 8-14 * * test $(date +%u) -eq 6 && echo "2nd Saturday"






      share|improve this answer


























        4












        4








        4






        I don't think it is possible with cron alone because the syntax doesn't
        allow for "at 27th except on Sundays". It only allows
        for "either 27th or non-Sundays (or both)".



        I suggest to either change command.sh to immediately exit on non-Sundays
        or put that check into the cronjob:



        1 0 27 12 *   test $(date +%u) -ne 7   &&  command.sh


        At the shell, date +%u returns the day of week (1…7, 1 is Monday). In
        a cronjob we have to escape that % sign (%). The command will
        check whether the current day is a non-Sunday and only then execute
        command.sh.



        The cronjob will run on every Dec 27th at 00:01 o'clock,
        no matter what day of the week that is, but only for non-Sundays the
        command.sh gets executed because the previous test only succeeds
        for them.



        Note: I sometimes use https://crontab.guru to check my cronjob's
        timetables. It's quite handy.
        Your definition translates to “At
        00:01 on day-of-month 27 and on every day-of-week from Monday through
        Saturday in December.”
        so it's quite the opposite of what you want.





        From the manpage:




        Note: The day of a command's execution can be specified by two fields
        — day of month, and day of week. If both fields are restricted (i.e.,
        aren't *), the command will be run when either field matches the
        current time. For example, 30 4 1,15 * 5 would cause a command to
        be run at 4:30 am on the 1st and 15th of each month, plus every
        Friday. One can, however, achieve the desired result by adding a test
        to the command (see the last example in EXAMPLE CRON FILE below).





        # Run on every second Saturday of the month
        0 4 8-14 * * test $(date +%u) -eq 6 && echo "2nd Saturday"






        share|improve this answer














        I don't think it is possible with cron alone because the syntax doesn't
        allow for "at 27th except on Sundays". It only allows
        for "either 27th or non-Sundays (or both)".



        I suggest to either change command.sh to immediately exit on non-Sundays
        or put that check into the cronjob:



        1 0 27 12 *   test $(date +%u) -ne 7   &&  command.sh


        At the shell, date +%u returns the day of week (1…7, 1 is Monday). In
        a cronjob we have to escape that % sign (%). The command will
        check whether the current day is a non-Sunday and only then execute
        command.sh.



        The cronjob will run on every Dec 27th at 00:01 o'clock,
        no matter what day of the week that is, but only for non-Sundays the
        command.sh gets executed because the previous test only succeeds
        for them.



        Note: I sometimes use https://crontab.guru to check my cronjob's
        timetables. It's quite handy.
        Your definition translates to “At
        00:01 on day-of-month 27 and on every day-of-week from Monday through
        Saturday in December.”
        so it's quite the opposite of what you want.





        From the manpage:




        Note: The day of a command's execution can be specified by two fields
        — day of month, and day of week. If both fields are restricted (i.e.,
        aren't *), the command will be run when either field matches the
        current time. For example, 30 4 1,15 * 5 would cause a command to
        be run at 4:30 am on the 1st and 15th of each month, plus every
        Friday. One can, however, achieve the desired result by adding a test
        to the command (see the last example in EXAMPLE CRON FILE below).





        # Run on every second Saturday of the month
        0 4 8-14 * * test $(date +%u) -eq 6 && echo "2nd Saturday"







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 9 hours ago

























        answered 9 hours ago









        PerlDuck

        5,25911231




        5,25911231






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1104605%2frun-crontab-in-specific-day-but-only-if-is-in-specific-weekday%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

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo