Specify shell for individual cron job

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












I have a cron job I need to run as root and that needs bash rather than the default sh shell.



The root crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash at the top of the crontab file.



How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?



I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.



I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).



Is it possible for me to specify the shell on a job-by-job basis?







share|improve this question
























    up vote
    2
    down vote

    favorite
    1












    I have a cron job I need to run as root and that needs bash rather than the default sh shell.



    The root crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash at the top of the crontab file.



    How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?



    I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.



    I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).



    Is it possible for me to specify the shell on a job-by-job basis?







    share|improve this question






















      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I have a cron job I need to run as root and that needs bash rather than the default sh shell.



      The root crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash at the top of the crontab file.



      How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?



      I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.



      I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).



      Is it possible for me to specify the shell on a job-by-job basis?







      share|improve this question












      I have a cron job I need to run as root and that needs bash rather than the default sh shell.



      The root crontab has other jobs in it that run fine as they are and I'd rather not risk breaking those by specifying SHELL=/bin/bash at the top of the crontab file.



      How can I specify my cron job is to be run using bash without changing the shell used for other existing jobs?



      I'm kind of thinking that a line like 0 * * * * SHELL=/bin/bash myjob might do the trick, but I can't find that mentioned anywhere in the CentOS documentation for cron.



      I also thought that maybe setting the SHELL variable immediately before my job (which would be the last one in the file) might work, but couldn't find an documentation of the effect of placement of environment variables and whether they were assigned in document order or were just considered global regardless of their position (or perhaps generated an error if not before all jobs).



      Is it possible for me to specify the shell on a job-by-job basis?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 15 at 19:16









      scanny

      1112




      1112




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob using bash, you could use something like the following command-line:



          /bin/bash -c 'myjob'


          The corresponding crontab entry might look like this:





          0 * * * * /bin/bash -c 'myjob'


          Alternatively, if myjob is a script, you might use something like the following command-line instead:





          /bin/bash '/path/to/myjob'


          And the corresponding crontab entry for that would be something like this:



          0 * * * * /bin/bash '/path/to/myjob'





          share|improve this answer






















          • You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
            – Jeff Schaller
            Mar 15 at 20:25










          • @scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
            – igal
            Mar 26 at 3:11

















          up vote
          1
          down vote













          If myjob is a shell script, make sure that it is executable and that it's #!-line specifies the correct interpreter for the script file (e.g. #!/bin/bash). That's all.



          If it's a series of commands, I'd recommend putting those in a bash script, with a proper #!-line, as above.



          This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f430466%2fspecify-shell-for-individual-cron-job%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob using bash, you could use something like the following command-line:



            /bin/bash -c 'myjob'


            The corresponding crontab entry might look like this:





            0 * * * * /bin/bash -c 'myjob'


            Alternatively, if myjob is a script, you might use something like the following command-line instead:





            /bin/bash '/path/to/myjob'


            And the corresponding crontab entry for that would be something like this:



            0 * * * * /bin/bash '/path/to/myjob'





            share|improve this answer






















            • You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
              – Jeff Schaller
              Mar 15 at 20:25










            • @scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
              – igal
              Mar 26 at 3:11














            up vote
            5
            down vote













            The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob using bash, you could use something like the following command-line:



            /bin/bash -c 'myjob'


            The corresponding crontab entry might look like this:





            0 * * * * /bin/bash -c 'myjob'


            Alternatively, if myjob is a script, you might use something like the following command-line instead:





            /bin/bash '/path/to/myjob'


            And the corresponding crontab entry for that would be something like this:



            0 * * * * /bin/bash '/path/to/myjob'





            share|improve this answer






















            • You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
              – Jeff Schaller
              Mar 15 at 20:25










            • @scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
              – igal
              Mar 26 at 3:11












            up vote
            5
            down vote










            up vote
            5
            down vote









            The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob using bash, you could use something like the following command-line:



            /bin/bash -c 'myjob'


            The corresponding crontab entry might look like this:





            0 * * * * /bin/bash -c 'myjob'


            Alternatively, if myjob is a script, you might use something like the following command-line instead:





            /bin/bash '/path/to/myjob'


            And the corresponding crontab entry for that would be something like this:



            0 * * * * /bin/bash '/path/to/myjob'





            share|improve this answer














            The most straight-forward way to specify the shell is simply to invoke that shell's executable file. For example, if you want to execute a command called myjob using bash, you could use something like the following command-line:



            /bin/bash -c 'myjob'


            The corresponding crontab entry might look like this:





            0 * * * * /bin/bash -c 'myjob'


            Alternatively, if myjob is a script, you might use something like the following command-line instead:





            /bin/bash '/path/to/myjob'


            And the corresponding crontab entry for that would be something like this:



            0 * * * * /bin/bash '/path/to/myjob'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 15 at 22:09

























            answered Mar 15 at 20:02









            igal

            4,820930




            4,820930











            • You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
              – Jeff Schaller
              Mar 15 at 20:25










            • @scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
              – igal
              Mar 26 at 3:11
















            • You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
              – Jeff Schaller
              Mar 15 at 20:25










            • @scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
              – igal
              Mar 26 at 3:11















            You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
            – Jeff Schaller
            Mar 15 at 20:25




            You can see, for example in cronie, that you can override SHELL (once), otherwise it picks up the default shell, which is set to /bin/sh
            – Jeff Schaller
            Mar 15 at 20:25












            @scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
            – igal
            Mar 26 at 3:11




            @scanny Does this solution not resolve your issue? If not, could you specify what's lacking from this solution?
            – igal
            Mar 26 at 3:11












            up vote
            1
            down vote













            If myjob is a shell script, make sure that it is executable and that it's #!-line specifies the correct interpreter for the script file (e.g. #!/bin/bash). That's all.



            If it's a series of commands, I'd recommend putting those in a bash script, with a proper #!-line, as above.



            This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.






            share|improve this answer
























              up vote
              1
              down vote













              If myjob is a shell script, make sure that it is executable and that it's #!-line specifies the correct interpreter for the script file (e.g. #!/bin/bash). That's all.



              If it's a series of commands, I'd recommend putting those in a bash script, with a proper #!-line, as above.



              This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                If myjob is a shell script, make sure that it is executable and that it's #!-line specifies the correct interpreter for the script file (e.g. #!/bin/bash). That's all.



                If it's a series of commands, I'd recommend putting those in a bash script, with a proper #!-line, as above.



                This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.






                share|improve this answer












                If myjob is a shell script, make sure that it is executable and that it's #!-line specifies the correct interpreter for the script file (e.g. #!/bin/bash). That's all.



                If it's a series of commands, I'd recommend putting those in a bash script, with a proper #!-line, as above.



                This way, you don't really have to worry too much about the particularities of getting the crontab entry correct (apart from at what times to run the script). It also removes the need to update the cronjob when you reimplement the script in Python or Perl or some other shell variant.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 13 at 10:52









                Kusalananda

                103k13201317




                103k13201317






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430466%2fspecify-shell-for-individual-cron-job%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?