How to run a cron job as a specific user?











up vote
62
down vote

favorite
6












I would like to run a cron job as a specific user on my machine. How may I specify the user for a cron job to run as?.



The cron jobs will be running on a server (running on Ubuntu 10.0.4). The 'users' are users that have been created specifically for carrying out specific server side tasks. These 'users' have the following in common:




  • Cannot log onto the system

  • Have restricted access to specific folders/files










share|improve this question




























    up vote
    62
    down vote

    favorite
    6












    I would like to run a cron job as a specific user on my machine. How may I specify the user for a cron job to run as?.



    The cron jobs will be running on a server (running on Ubuntu 10.0.4). The 'users' are users that have been created specifically for carrying out specific server side tasks. These 'users' have the following in common:




    • Cannot log onto the system

    • Have restricted access to specific folders/files










    share|improve this question


























      up vote
      62
      down vote

      favorite
      6









      up vote
      62
      down vote

      favorite
      6






      6





      I would like to run a cron job as a specific user on my machine. How may I specify the user for a cron job to run as?.



      The cron jobs will be running on a server (running on Ubuntu 10.0.4). The 'users' are users that have been created specifically for carrying out specific server side tasks. These 'users' have the following in common:




      • Cannot log onto the system

      • Have restricted access to specific folders/files










      share|improve this question















      I would like to run a cron job as a specific user on my machine. How may I specify the user for a cron job to run as?.



      The cron jobs will be running on a server (running on Ubuntu 10.0.4). The 'users' are users that have been created specifically for carrying out specific server side tasks. These 'users' have the following in common:




      • Cannot log onto the system

      • Have restricted access to specific folders/files







      ubuntu security permissions cron cronjob






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 27 '17 at 17:15









      random

      12.7k84657




      12.7k84657










      asked Aug 2 '10 at 11:50









      morpheous

      1,57882528




      1,57882528






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          74
          down vote



          accepted










          Assuming you can't just log in and add it to that user's crontab, put a file in /etc/cron.d. It should be formatted as a normal cronjob, but with an extra field. Before the command to run and after the timing, put the user. You should be able to find examples already on your system.



          Example:



          #<timing>   <user> <command>
          11 * * * * root /usr/lib/command





          share|improve this answer























          • I think this is the correct answer. I will have to look for some examples and investigate some more.
            – morpheous
            Aug 2 '10 at 12:19






          • 2




            Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d).
            – Patkos Csaba
            Aug 2 '10 at 12:25






          • 3




            The preferably solution I guess would be to just crontab -u <user> -e to keep everything in one place, which you can do as root. I like to group my cron jobs by function, though, instead of just by user, so this is the preferable solution for me.
            – Stuart H
            Sep 5 '17 at 10:46


















          up vote
          39
          down vote













          As root, to edit the cron of user1:



          crontab -u user1 -e


          You can also start your command with:



          su user1 -c foo bar


          But often, the scripts themselves reduce their own access when started as root.






          share|improve this answer




























            up vote
            0
            down vote













            I have been looking for this for a couple weeks and this finally worked...



            Create your script as user1



            ssh user1@ipaddress
            nano hourly-event.sh


            enter some command



            #!/bin/bash
            echo "YAY it works" > /home/user1/yay.txt


            make it executable



            chmod 755 hourly-event.sh


            edit the crontab for user1



            sudo crontab -u user1 -e


            put a line at the bottom pointing to your script(s)



            # m h  dom mon dow   command
            * * * * * bash ./hourly-event.sh


            exit - saving changes (it will show a /tmp directory when saving... it's ok)



            wait for the turn of the minute



            open your newly create yay.txt



            nano /home/user1/yay.txt


            you should now have a nano window open with "YAY it works" as the first and only line
            if the bottom of your nano window say New File...
            well.. i dunno



            you can also check your newly crontab entry for user1 at: /var/spool/cron/crontabs/user1



            PEACE






            share|improve this answer






























              up vote
              0
              down vote













              Sometimes you may want the user load his environment, especially when using "rbenv", "nvm", just like this :



              0 0 * * * source ~/.bash_profile && cd /opt/app/current && bundle exec ruby ...


              (here the command source ~/.bash_profile will load you Ruby or Node environment.






              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',
                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%2f170866%2fhow-to-run-a-cron-job-as-a-specific-user%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                74
                down vote



                accepted










                Assuming you can't just log in and add it to that user's crontab, put a file in /etc/cron.d. It should be formatted as a normal cronjob, but with an extra field. Before the command to run and after the timing, put the user. You should be able to find examples already on your system.



                Example:



                #<timing>   <user> <command>
                11 * * * * root /usr/lib/command





                share|improve this answer























                • I think this is the correct answer. I will have to look for some examples and investigate some more.
                  – morpheous
                  Aug 2 '10 at 12:19






                • 2




                  Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d).
                  – Patkos Csaba
                  Aug 2 '10 at 12:25






                • 3




                  The preferably solution I guess would be to just crontab -u <user> -e to keep everything in one place, which you can do as root. I like to group my cron jobs by function, though, instead of just by user, so this is the preferable solution for me.
                  – Stuart H
                  Sep 5 '17 at 10:46















                up vote
                74
                down vote



                accepted










                Assuming you can't just log in and add it to that user's crontab, put a file in /etc/cron.d. It should be formatted as a normal cronjob, but with an extra field. Before the command to run and after the timing, put the user. You should be able to find examples already on your system.



                Example:



                #<timing>   <user> <command>
                11 * * * * root /usr/lib/command





                share|improve this answer























                • I think this is the correct answer. I will have to look for some examples and investigate some more.
                  – morpheous
                  Aug 2 '10 at 12:19






                • 2




                  Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d).
                  – Patkos Csaba
                  Aug 2 '10 at 12:25






                • 3




                  The preferably solution I guess would be to just crontab -u <user> -e to keep everything in one place, which you can do as root. I like to group my cron jobs by function, though, instead of just by user, so this is the preferable solution for me.
                  – Stuart H
                  Sep 5 '17 at 10:46













                up vote
                74
                down vote



                accepted







                up vote
                74
                down vote



                accepted






                Assuming you can't just log in and add it to that user's crontab, put a file in /etc/cron.d. It should be formatted as a normal cronjob, but with an extra field. Before the command to run and after the timing, put the user. You should be able to find examples already on your system.



                Example:



                #<timing>   <user> <command>
                11 * * * * root /usr/lib/command





                share|improve this answer














                Assuming you can't just log in and add it to that user's crontab, put a file in /etc/cron.d. It should be formatted as a normal cronjob, but with an extra field. Before the command to run and after the timing, put the user. You should be able to find examples already on your system.



                Example:



                #<timing>   <user> <command>
                11 * * * * root /usr/lib/command






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 14 '14 at 11:35









                Pere

                719711




                719711










                answered Aug 2 '10 at 11:52









                Daenyth

                5,37212121




                5,37212121












                • I think this is the correct answer. I will have to look for some examples and investigate some more.
                  – morpheous
                  Aug 2 '10 at 12:19






                • 2




                  Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d).
                  – Patkos Csaba
                  Aug 2 '10 at 12:25






                • 3




                  The preferably solution I guess would be to just crontab -u <user> -e to keep everything in one place, which you can do as root. I like to group my cron jobs by function, though, instead of just by user, so this is the preferable solution for me.
                  – Stuart H
                  Sep 5 '17 at 10:46


















                • I think this is the correct answer. I will have to look for some examples and investigate some more.
                  – morpheous
                  Aug 2 '10 at 12:19






                • 2




                  Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d).
                  – Patkos Csaba
                  Aug 2 '10 at 12:25






                • 3




                  The preferably solution I guess would be to just crontab -u <user> -e to keep everything in one place, which you can do as root. I like to group my cron jobs by function, though, instead of just by user, so this is the preferable solution for me.
                  – Stuart H
                  Sep 5 '17 at 10:46
















                I think this is the correct answer. I will have to look for some examples and investigate some more.
                – morpheous
                Aug 2 '10 at 12:19




                I think this is the correct answer. I will have to look for some examples and investigate some more.
                – morpheous
                Aug 2 '10 at 12:19




                2




                2




                Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d).
                – Patkos Csaba
                Aug 2 '10 at 12:25




                Plus one from me, this is the best way to do what you want. More precisely look in /etc/crontab file and you will see, that after the time template and before the command there is "root" written there. This means that "root" runs those commands. Just add your own schedule and instead of "root" use any user. Also, note that if you use per user crontabs (as sugested by Eric D) you may loose the functionality of the system wide crontab (/etc/crontab and /etc/cron.d).
                – Patkos Csaba
                Aug 2 '10 at 12:25




                3




                3




                The preferably solution I guess would be to just crontab -u <user> -e to keep everything in one place, which you can do as root. I like to group my cron jobs by function, though, instead of just by user, so this is the preferable solution for me.
                – Stuart H
                Sep 5 '17 at 10:46




                The preferably solution I guess would be to just crontab -u <user> -e to keep everything in one place, which you can do as root. I like to group my cron jobs by function, though, instead of just by user, so this is the preferable solution for me.
                – Stuart H
                Sep 5 '17 at 10:46












                up vote
                39
                down vote













                As root, to edit the cron of user1:



                crontab -u user1 -e


                You can also start your command with:



                su user1 -c foo bar


                But often, the scripts themselves reduce their own access when started as root.






                share|improve this answer

























                  up vote
                  39
                  down vote













                  As root, to edit the cron of user1:



                  crontab -u user1 -e


                  You can also start your command with:



                  su user1 -c foo bar


                  But often, the scripts themselves reduce their own access when started as root.






                  share|improve this answer























                    up vote
                    39
                    down vote










                    up vote
                    39
                    down vote









                    As root, to edit the cron of user1:



                    crontab -u user1 -e


                    You can also start your command with:



                    su user1 -c foo bar


                    But often, the scripts themselves reduce their own access when started as root.






                    share|improve this answer












                    As root, to edit the cron of user1:



                    crontab -u user1 -e


                    You can also start your command with:



                    su user1 -c foo bar


                    But often, the scripts themselves reduce their own access when started as root.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 2 '10 at 11:58









                    Eric Darchis

                    1,088912




                    1,088912






















                        up vote
                        0
                        down vote













                        I have been looking for this for a couple weeks and this finally worked...



                        Create your script as user1



                        ssh user1@ipaddress
                        nano hourly-event.sh


                        enter some command



                        #!/bin/bash
                        echo "YAY it works" > /home/user1/yay.txt


                        make it executable



                        chmod 755 hourly-event.sh


                        edit the crontab for user1



                        sudo crontab -u user1 -e


                        put a line at the bottom pointing to your script(s)



                        # m h  dom mon dow   command
                        * * * * * bash ./hourly-event.sh


                        exit - saving changes (it will show a /tmp directory when saving... it's ok)



                        wait for the turn of the minute



                        open your newly create yay.txt



                        nano /home/user1/yay.txt


                        you should now have a nano window open with "YAY it works" as the first and only line
                        if the bottom of your nano window say New File...
                        well.. i dunno



                        you can also check your newly crontab entry for user1 at: /var/spool/cron/crontabs/user1



                        PEACE






                        share|improve this answer



























                          up vote
                          0
                          down vote













                          I have been looking for this for a couple weeks and this finally worked...



                          Create your script as user1



                          ssh user1@ipaddress
                          nano hourly-event.sh


                          enter some command



                          #!/bin/bash
                          echo "YAY it works" > /home/user1/yay.txt


                          make it executable



                          chmod 755 hourly-event.sh


                          edit the crontab for user1



                          sudo crontab -u user1 -e


                          put a line at the bottom pointing to your script(s)



                          # m h  dom mon dow   command
                          * * * * * bash ./hourly-event.sh


                          exit - saving changes (it will show a /tmp directory when saving... it's ok)



                          wait for the turn of the minute



                          open your newly create yay.txt



                          nano /home/user1/yay.txt


                          you should now have a nano window open with "YAY it works" as the first and only line
                          if the bottom of your nano window say New File...
                          well.. i dunno



                          you can also check your newly crontab entry for user1 at: /var/spool/cron/crontabs/user1



                          PEACE






                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            I have been looking for this for a couple weeks and this finally worked...



                            Create your script as user1



                            ssh user1@ipaddress
                            nano hourly-event.sh


                            enter some command



                            #!/bin/bash
                            echo "YAY it works" > /home/user1/yay.txt


                            make it executable



                            chmod 755 hourly-event.sh


                            edit the crontab for user1



                            sudo crontab -u user1 -e


                            put a line at the bottom pointing to your script(s)



                            # m h  dom mon dow   command
                            * * * * * bash ./hourly-event.sh


                            exit - saving changes (it will show a /tmp directory when saving... it's ok)



                            wait for the turn of the minute



                            open your newly create yay.txt



                            nano /home/user1/yay.txt


                            you should now have a nano window open with "YAY it works" as the first and only line
                            if the bottom of your nano window say New File...
                            well.. i dunno



                            you can also check your newly crontab entry for user1 at: /var/spool/cron/crontabs/user1



                            PEACE






                            share|improve this answer














                            I have been looking for this for a couple weeks and this finally worked...



                            Create your script as user1



                            ssh user1@ipaddress
                            nano hourly-event.sh


                            enter some command



                            #!/bin/bash
                            echo "YAY it works" > /home/user1/yay.txt


                            make it executable



                            chmod 755 hourly-event.sh


                            edit the crontab for user1



                            sudo crontab -u user1 -e


                            put a line at the bottom pointing to your script(s)



                            # m h  dom mon dow   command
                            * * * * * bash ./hourly-event.sh


                            exit - saving changes (it will show a /tmp directory when saving... it's ok)



                            wait for the turn of the minute



                            open your newly create yay.txt



                            nano /home/user1/yay.txt


                            you should now have a nano window open with "YAY it works" as the first and only line
                            if the bottom of your nano window say New File...
                            well.. i dunno



                            you can also check your newly crontab entry for user1 at: /var/spool/cron/crontabs/user1



                            PEACE







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 8 '17 at 4:43









                            quanghgx

                            12




                            12










                            answered Apr 27 '13 at 16:49









                            user220364

                            29




                            29






















                                up vote
                                0
                                down vote













                                Sometimes you may want the user load his environment, especially when using "rbenv", "nvm", just like this :



                                0 0 * * * source ~/.bash_profile && cd /opt/app/current && bundle exec ruby ...


                                (here the command source ~/.bash_profile will load you Ruby or Node environment.






                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  Sometimes you may want the user load his environment, especially when using "rbenv", "nvm", just like this :



                                  0 0 * * * source ~/.bash_profile && cd /opt/app/current && bundle exec ruby ...


                                  (here the command source ~/.bash_profile will load you Ruby or Node environment.






                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    Sometimes you may want the user load his environment, especially when using "rbenv", "nvm", just like this :



                                    0 0 * * * source ~/.bash_profile && cd /opt/app/current && bundle exec ruby ...


                                    (here the command source ~/.bash_profile will load you Ruby or Node environment.






                                    share|improve this answer












                                    Sometimes you may want the user load his environment, especially when using "rbenv", "nvm", just like this :



                                    0 0 * * * source ~/.bash_profile && cd /opt/app/current && bundle exec ruby ...


                                    (here the command source ~/.bash_profile will load you Ruby or Node environment.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 18 at 10:05









                                    Siwei Shen申思维

                                    58944




                                    58944






























                                        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.





                                        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%2fsuperuser.com%2fquestions%2f170866%2fhow-to-run-a-cron-job-as-a-specific-user%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

                                        Сан-Квентин

                                        Алькесар

                                        Josef Freinademetz