How do I add an entry to my crontab?

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











up vote
12
down vote

favorite
1












I am using crontab for the first time. Want to write a few very simple test cron tasks, and run them.



$crontab * * * * * echo "Hi"


doesn't produce anything.



crontab */1 * * * * echo "hi"


says */1: No such file or directory.



Also, how do I list the currently running cron tasks (not just the ones I own, but ones started by other users such as root as well).



And how do I delete a particular cron task?










share|improve this question























  • good question! :D
    – ncomputers
    Jul 26 '17 at 6:51














up vote
12
down vote

favorite
1












I am using crontab for the first time. Want to write a few very simple test cron tasks, and run them.



$crontab * * * * * echo "Hi"


doesn't produce anything.



crontab */1 * * * * echo "hi"


says */1: No such file or directory.



Also, how do I list the currently running cron tasks (not just the ones I own, but ones started by other users such as root as well).



And how do I delete a particular cron task?










share|improve this question























  • good question! :D
    – ncomputers
    Jul 26 '17 at 6:51












up vote
12
down vote

favorite
1









up vote
12
down vote

favorite
1






1





I am using crontab for the first time. Want to write a few very simple test cron tasks, and run them.



$crontab * * * * * echo "Hi"


doesn't produce anything.



crontab */1 * * * * echo "hi"


says */1: No such file or directory.



Also, how do I list the currently running cron tasks (not just the ones I own, but ones started by other users such as root as well).



And how do I delete a particular cron task?










share|improve this question















I am using crontab for the first time. Want to write a few very simple test cron tasks, and run them.



$crontab * * * * * echo "Hi"


doesn't produce anything.



crontab */1 * * * * echo "hi"


says */1: No such file or directory.



Also, how do I list the currently running cron tasks (not just the ones I own, but ones started by other users such as root as well).



And how do I delete a particular cron task?







cron administration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 23 '11 at 22:11









Gilles

519k12510371566




519k12510371566










asked Sep 23 '11 at 9:46









xyz

1,05071728




1,05071728











  • good question! :D
    – ncomputers
    Jul 26 '17 at 6:51
















  • good question! :D
    – ncomputers
    Jul 26 '17 at 6:51















good question! :D
– ncomputers
Jul 26 '17 at 6:51




good question! :D
– ncomputers
Jul 26 '17 at 6:51










8 Answers
8






active

oldest

votes

















up vote
21
down vote



accepted










You can't use crontab like that. Use man crontab to read about the correct way of calling this utility.



You'll want to use crontab -e to edit the current user's cron entries (you can add/modify/remove lines). Use crontab -l to see the current list of configured tasks.



As for seeing other user's crontabs, that's not possible without being root on default installations. See How do I list all cron jobs for all users for some ways to list everything (as root).



Note: be very careful when you use shell globbing characters on the command line (* and ? especially). * will be expanded to the list of files in the current directory, which can have unexpected effects. If you want to pass * as an argument to something, quote it ('*').






share|improve this answer






















  • so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks.
    – xyz
    Sep 23 '11 at 10:20







  • 2




    crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user."
    – Mat
    Sep 23 '11 at 10:23






  • 1




    The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'.
    – manatwork
    Sep 23 '11 at 10:30











  • @manatwork You may also need XAUTHORITY, which may not be easy.
    – derobert
    Sep 20 '12 at 21:53

















up vote
4
down vote













There are two ways of editing one's crontab:



  1. using crontab -e, which will open the crontab in the editor specified by $VISUAL or $EDITOR, or


  2. using crontab crontab.txt, which will simply import the crontab entries from the file crontab.txt, replacing the existing active crontab for the current user.


So, to remove particular tasks programmatically, you could do something like



$ crontab -l | grep -v 'PATTERN' >crontab.txt && crontab crontab.txt


where PATTERN is a regular expression that will match the task(s) that you'd like to remove. Here, crontab -l will give you your current crontab.



Or, if you have entries in a file called crontab-fragment.txt that you want to remove from the active crontab,



$ crontab -l | grep -v -Fx -f crontab-fragment.txt >crontab.txt && crontab crontab.txt


This reads the current crontab and filters out (removes) any line that also occurs in the file crontab-fragment.txt in the current directory (using a full line string comparison). The result is saved to crontab.txt and then loaded from there to replace the current crontab.



To add a task, do something like



$ crontab -l | cat - crontab-fragment.txt >crontab.txt && crontab crontab.txt


This is assuming that the file crontab-fragment.txt contains the entries that you would like to add. It reads the current crontab, appends the entries from crontab-fragment.txt to this and creates crontab.txt. The crontab.txt file then replaces the current crontab.






share|improve this answer





























    up vote
    2
    down vote













    If you want to modify the crontab interactively, run the command crontab -e, with no other option. This will start an editor on a copy of the crontab; when you exit the editor, the edited copy will become the new crontab. You can control which editor is started through the VISUAL and EDITOR environment variables. To list your crontab, run crontab -l.



    If you want to modify the crontab in a script, set VISUAL and EDITOR to the path to a script or to a shell snippet that modifies the supplied file in place. The editor ed is a possibility here, or sed -i if your implementation of sed has this option. If you want to unconditionally add a line, you can use echo … >>. Take care with quoting; if at all in doubt, write a script and pass the name of the script as EDITOR.



    script=$(mktemp)
    cat <<'EOF' >"$script"
    #!/bin/sh
    ed -s "$1" <<'EOS'
    g/^ *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *echo "hi"$/d
    $a
    * * * * * echo "hi"
    .
    w
    q
    EOS
    EOF





    share|improve this answer





























      up vote
      2
      down vote













      For instruction on how to edit and create cron jobs and an explanation on the files structure see this crontab syntax guide






      share|improve this answer
















      • 1




        Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value.
        – Mat
        Sep 22 '12 at 14:30

















      up vote
      2
      down vote













      This is a bit late, but for others looking here. There are easier ways than manipulating the input/output streams with VISUAL and EDITOR.



      You can simply write your cron schedule in a file, 1 job per line, and use the command crontab [filename] so you can cat your jobs into filename as part of your script and then finally call crontab at the end if you like.



      in fact this is the most common use as man crontab hints by listing it first






      share|improve this answer



























        up vote
        2
        down vote













        If you wish to add entries to the crontab by automation or from the command line,
        you can do (setting the times you wish to have)



        echo "* * * * * /yourpath/yourcommand 2>&1 >> /var/log/somelog.log" >> /var/spool/cron/root (or appropriate username)


        Then:



        service crond reload


        to reload the crontabs.






        share|improve this answer






















        • This is unportable; not all unix store the crontab files under /var/spool/cron. A better way would be through the crontab(1) command.
          – thrig
          Mar 28 '17 at 22:34










        • That may be true, but it was to show the principle of how you can do it on a system, and yes, crontab does the trick if you want to add line by line. It was not meant to be an absolute answer, but one if you know where your stuff is, and you need to mass add. Take it for what it is.
          – Chris Sprucefield
          Mar 29 '17 at 20:39

















        up vote
        1
        down vote













        Try it with this command:



        crontab -e 


        then add your cron job:



        */1 * * * * echo "hi"


        in that file.






        share|improve this answer





























          up vote
          0
          down vote













          I would like to highlight the way I describe below even though It has been mentioned before in another comment. I found it extremely useful when adding the entries to a file and then add the file to crontab.
          That file would contain the two entries below and the file name would be hello-world-crontab.txt.



          • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt

          • 0 09 * * * python /home/user/greeting.py

          Then, I executed the command below to add these entries to the crontab.



          • $ crontab hello-world-crontab.txt

          The next step is to verify these entries have been added.



          • $ crontab -l

          A brief explanation on what each entry does:



          • This crontab entry writes "Hello world!" into the file /home/user/greeting.txt every day at 9 am.

            • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt


          • This crontab executes a python file which prints in the command line 'Hello world!' every day at 9 am.

            • 0 09 * * * python /home/user/greeting.py






          share|improve this answer








          New contributor




          SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















            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: 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%2f21297%2fhow-do-i-add-an-entry-to-my-crontab%23new-answer', 'question_page');

            );

            Post as a guest






























            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            21
            down vote



            accepted










            You can't use crontab like that. Use man crontab to read about the correct way of calling this utility.



            You'll want to use crontab -e to edit the current user's cron entries (you can add/modify/remove lines). Use crontab -l to see the current list of configured tasks.



            As for seeing other user's crontabs, that's not possible without being root on default installations. See How do I list all cron jobs for all users for some ways to list everything (as root).



            Note: be very careful when you use shell globbing characters on the command line (* and ? especially). * will be expanded to the list of files in the current directory, which can have unexpected effects. If you want to pass * as an argument to something, quote it ('*').






            share|improve this answer






















            • so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks.
              – xyz
              Sep 23 '11 at 10:20







            • 2




              crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user."
              – Mat
              Sep 23 '11 at 10:23






            • 1




              The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'.
              – manatwork
              Sep 23 '11 at 10:30











            • @manatwork You may also need XAUTHORITY, which may not be easy.
              – derobert
              Sep 20 '12 at 21:53














            up vote
            21
            down vote



            accepted










            You can't use crontab like that. Use man crontab to read about the correct way of calling this utility.



            You'll want to use crontab -e to edit the current user's cron entries (you can add/modify/remove lines). Use crontab -l to see the current list of configured tasks.



            As for seeing other user's crontabs, that's not possible without being root on default installations. See How do I list all cron jobs for all users for some ways to list everything (as root).



            Note: be very careful when you use shell globbing characters on the command line (* and ? especially). * will be expanded to the list of files in the current directory, which can have unexpected effects. If you want to pass * as an argument to something, quote it ('*').






            share|improve this answer






















            • so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks.
              – xyz
              Sep 23 '11 at 10:20







            • 2




              crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user."
              – Mat
              Sep 23 '11 at 10:23






            • 1




              The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'.
              – manatwork
              Sep 23 '11 at 10:30











            • @manatwork You may also need XAUTHORITY, which may not be easy.
              – derobert
              Sep 20 '12 at 21:53












            up vote
            21
            down vote



            accepted







            up vote
            21
            down vote



            accepted






            You can't use crontab like that. Use man crontab to read about the correct way of calling this utility.



            You'll want to use crontab -e to edit the current user's cron entries (you can add/modify/remove lines). Use crontab -l to see the current list of configured tasks.



            As for seeing other user's crontabs, that's not possible without being root on default installations. See How do I list all cron jobs for all users for some ways to list everything (as root).



            Note: be very careful when you use shell globbing characters on the command line (* and ? especially). * will be expanded to the list of files in the current directory, which can have unexpected effects. If you want to pass * as an argument to something, quote it ('*').






            share|improve this answer














            You can't use crontab like that. Use man crontab to read about the correct way of calling this utility.



            You'll want to use crontab -e to edit the current user's cron entries (you can add/modify/remove lines). Use crontab -l to see the current list of configured tasks.



            As for seeing other user's crontabs, that's not possible without being root on default installations. See How do I list all cron jobs for all users for some ways to list everything (as root).



            Note: be very careful when you use shell globbing characters on the command line (* and ? especially). * will be expanded to the list of files in the current directory, which can have unexpected effects. If you want to pass * as an argument to something, quote it ('*').







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:39









            Community

            1




            1










            answered Sep 23 '11 at 10:05









            Mat

            38.5k8117125




            38.5k8117125











            • so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks.
              – xyz
              Sep 23 '11 at 10:20







            • 2




              crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user."
              – Mat
              Sep 23 '11 at 10:23






            • 1




              The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'.
              – manatwork
              Sep 23 '11 at 10:30











            • @manatwork You may also need XAUTHORITY, which may not be easy.
              – derobert
              Sep 20 '12 at 21:53
















            • so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks.
              – xyz
              Sep 23 '11 at 10:20







            • 2




              crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user."
              – Mat
              Sep 23 '11 at 10:23






            • 1




              The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'.
              – manatwork
              Sep 23 '11 at 10:30











            • @manatwork You may also need XAUTHORITY, which may not be easy.
              – derobert
              Sep 20 '12 at 21:53















            so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks.
            – xyz
            Sep 23 '11 at 10:20





            so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks.
            – xyz
            Sep 23 '11 at 10:20





            2




            2




            crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user."
            – Mat
            Sep 23 '11 at 10:23




            crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user."
            – Mat
            Sep 23 '11 at 10:23




            1




            1




            The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'.
            – manatwork
            Sep 23 '11 at 10:30





            The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'.
            – manatwork
            Sep 23 '11 at 10:30













            @manatwork You may also need XAUTHORITY, which may not be easy.
            – derobert
            Sep 20 '12 at 21:53




            @manatwork You may also need XAUTHORITY, which may not be easy.
            – derobert
            Sep 20 '12 at 21:53












            up vote
            4
            down vote













            There are two ways of editing one's crontab:



            1. using crontab -e, which will open the crontab in the editor specified by $VISUAL or $EDITOR, or


            2. using crontab crontab.txt, which will simply import the crontab entries from the file crontab.txt, replacing the existing active crontab for the current user.


            So, to remove particular tasks programmatically, you could do something like



            $ crontab -l | grep -v 'PATTERN' >crontab.txt && crontab crontab.txt


            where PATTERN is a regular expression that will match the task(s) that you'd like to remove. Here, crontab -l will give you your current crontab.



            Or, if you have entries in a file called crontab-fragment.txt that you want to remove from the active crontab,



            $ crontab -l | grep -v -Fx -f crontab-fragment.txt >crontab.txt && crontab crontab.txt


            This reads the current crontab and filters out (removes) any line that also occurs in the file crontab-fragment.txt in the current directory (using a full line string comparison). The result is saved to crontab.txt and then loaded from there to replace the current crontab.



            To add a task, do something like



            $ crontab -l | cat - crontab-fragment.txt >crontab.txt && crontab crontab.txt


            This is assuming that the file crontab-fragment.txt contains the entries that you would like to add. It reads the current crontab, appends the entries from crontab-fragment.txt to this and creates crontab.txt. The crontab.txt file then replaces the current crontab.






            share|improve this answer


























              up vote
              4
              down vote













              There are two ways of editing one's crontab:



              1. using crontab -e, which will open the crontab in the editor specified by $VISUAL or $EDITOR, or


              2. using crontab crontab.txt, which will simply import the crontab entries from the file crontab.txt, replacing the existing active crontab for the current user.


              So, to remove particular tasks programmatically, you could do something like



              $ crontab -l | grep -v 'PATTERN' >crontab.txt && crontab crontab.txt


              where PATTERN is a regular expression that will match the task(s) that you'd like to remove. Here, crontab -l will give you your current crontab.



              Or, if you have entries in a file called crontab-fragment.txt that you want to remove from the active crontab,



              $ crontab -l | grep -v -Fx -f crontab-fragment.txt >crontab.txt && crontab crontab.txt


              This reads the current crontab and filters out (removes) any line that also occurs in the file crontab-fragment.txt in the current directory (using a full line string comparison). The result is saved to crontab.txt and then loaded from there to replace the current crontab.



              To add a task, do something like



              $ crontab -l | cat - crontab-fragment.txt >crontab.txt && crontab crontab.txt


              This is assuming that the file crontab-fragment.txt contains the entries that you would like to add. It reads the current crontab, appends the entries from crontab-fragment.txt to this and creates crontab.txt. The crontab.txt file then replaces the current crontab.






              share|improve this answer
























                up vote
                4
                down vote










                up vote
                4
                down vote









                There are two ways of editing one's crontab:



                1. using crontab -e, which will open the crontab in the editor specified by $VISUAL or $EDITOR, or


                2. using crontab crontab.txt, which will simply import the crontab entries from the file crontab.txt, replacing the existing active crontab for the current user.


                So, to remove particular tasks programmatically, you could do something like



                $ crontab -l | grep -v 'PATTERN' >crontab.txt && crontab crontab.txt


                where PATTERN is a regular expression that will match the task(s) that you'd like to remove. Here, crontab -l will give you your current crontab.



                Or, if you have entries in a file called crontab-fragment.txt that you want to remove from the active crontab,



                $ crontab -l | grep -v -Fx -f crontab-fragment.txt >crontab.txt && crontab crontab.txt


                This reads the current crontab and filters out (removes) any line that also occurs in the file crontab-fragment.txt in the current directory (using a full line string comparison). The result is saved to crontab.txt and then loaded from there to replace the current crontab.



                To add a task, do something like



                $ crontab -l | cat - crontab-fragment.txt >crontab.txt && crontab crontab.txt


                This is assuming that the file crontab-fragment.txt contains the entries that you would like to add. It reads the current crontab, appends the entries from crontab-fragment.txt to this and creates crontab.txt. The crontab.txt file then replaces the current crontab.






                share|improve this answer














                There are two ways of editing one's crontab:



                1. using crontab -e, which will open the crontab in the editor specified by $VISUAL or $EDITOR, or


                2. using crontab crontab.txt, which will simply import the crontab entries from the file crontab.txt, replacing the existing active crontab for the current user.


                So, to remove particular tasks programmatically, you could do something like



                $ crontab -l | grep -v 'PATTERN' >crontab.txt && crontab crontab.txt


                where PATTERN is a regular expression that will match the task(s) that you'd like to remove. Here, crontab -l will give you your current crontab.



                Or, if you have entries in a file called crontab-fragment.txt that you want to remove from the active crontab,



                $ crontab -l | grep -v -Fx -f crontab-fragment.txt >crontab.txt && crontab crontab.txt


                This reads the current crontab and filters out (removes) any line that also occurs in the file crontab-fragment.txt in the current directory (using a full line string comparison). The result is saved to crontab.txt and then loaded from there to replace the current crontab.



                To add a task, do something like



                $ crontab -l | cat - crontab-fragment.txt >crontab.txt && crontab crontab.txt


                This is assuming that the file crontab-fragment.txt contains the entries that you would like to add. It reads the current crontab, appends the entries from crontab-fragment.txt to this and creates crontab.txt. The crontab.txt file then replaces the current crontab.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jul 30 at 12:00

























                answered Mar 2 '17 at 16:53









                Kusalananda

                114k15218349




                114k15218349




















                    up vote
                    2
                    down vote













                    If you want to modify the crontab interactively, run the command crontab -e, with no other option. This will start an editor on a copy of the crontab; when you exit the editor, the edited copy will become the new crontab. You can control which editor is started through the VISUAL and EDITOR environment variables. To list your crontab, run crontab -l.



                    If you want to modify the crontab in a script, set VISUAL and EDITOR to the path to a script or to a shell snippet that modifies the supplied file in place. The editor ed is a possibility here, or sed -i if your implementation of sed has this option. If you want to unconditionally add a line, you can use echo … >>. Take care with quoting; if at all in doubt, write a script and pass the name of the script as EDITOR.



                    script=$(mktemp)
                    cat <<'EOF' >"$script"
                    #!/bin/sh
                    ed -s "$1" <<'EOS'
                    g/^ *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *echo "hi"$/d
                    $a
                    * * * * * echo "hi"
                    .
                    w
                    q
                    EOS
                    EOF





                    share|improve this answer


























                      up vote
                      2
                      down vote













                      If you want to modify the crontab interactively, run the command crontab -e, with no other option. This will start an editor on a copy of the crontab; when you exit the editor, the edited copy will become the new crontab. You can control which editor is started through the VISUAL and EDITOR environment variables. To list your crontab, run crontab -l.



                      If you want to modify the crontab in a script, set VISUAL and EDITOR to the path to a script or to a shell snippet that modifies the supplied file in place. The editor ed is a possibility here, or sed -i if your implementation of sed has this option. If you want to unconditionally add a line, you can use echo … >>. Take care with quoting; if at all in doubt, write a script and pass the name of the script as EDITOR.



                      script=$(mktemp)
                      cat <<'EOF' >"$script"
                      #!/bin/sh
                      ed -s "$1" <<'EOS'
                      g/^ *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *echo "hi"$/d
                      $a
                      * * * * * echo "hi"
                      .
                      w
                      q
                      EOS
                      EOF





                      share|improve this answer
























                        up vote
                        2
                        down vote










                        up vote
                        2
                        down vote









                        If you want to modify the crontab interactively, run the command crontab -e, with no other option. This will start an editor on a copy of the crontab; when you exit the editor, the edited copy will become the new crontab. You can control which editor is started through the VISUAL and EDITOR environment variables. To list your crontab, run crontab -l.



                        If you want to modify the crontab in a script, set VISUAL and EDITOR to the path to a script or to a shell snippet that modifies the supplied file in place. The editor ed is a possibility here, or sed -i if your implementation of sed has this option. If you want to unconditionally add a line, you can use echo … >>. Take care with quoting; if at all in doubt, write a script and pass the name of the script as EDITOR.



                        script=$(mktemp)
                        cat <<'EOF' >"$script"
                        #!/bin/sh
                        ed -s "$1" <<'EOS'
                        g/^ *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *echo "hi"$/d
                        $a
                        * * * * * echo "hi"
                        .
                        w
                        q
                        EOS
                        EOF





                        share|improve this answer














                        If you want to modify the crontab interactively, run the command crontab -e, with no other option. This will start an editor on a copy of the crontab; when you exit the editor, the edited copy will become the new crontab. You can control which editor is started through the VISUAL and EDITOR environment variables. To list your crontab, run crontab -l.



                        If you want to modify the crontab in a script, set VISUAL and EDITOR to the path to a script or to a shell snippet that modifies the supplied file in place. The editor ed is a possibility here, or sed -i if your implementation of sed has this option. If you want to unconditionally add a line, you can use echo … >>. Take care with quoting; if at all in doubt, write a script and pass the name of the script as EDITOR.



                        script=$(mktemp)
                        cat <<'EOF' >"$script"
                        #!/bin/sh
                        ed -s "$1" <<'EOS'
                        g/^ *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *echo "hi"$/d
                        $a
                        * * * * * echo "hi"
                        .
                        w
                        q
                        EOS
                        EOF






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Apr 13 '17 at 12:36









                        Community

                        1




                        1










                        answered Sep 23 '11 at 22:10









                        Gilles

                        519k12510371566




                        519k12510371566




















                            up vote
                            2
                            down vote













                            For instruction on how to edit and create cron jobs and an explanation on the files structure see this crontab syntax guide






                            share|improve this answer
















                            • 1




                              Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value.
                              – Mat
                              Sep 22 '12 at 14:30














                            up vote
                            2
                            down vote













                            For instruction on how to edit and create cron jobs and an explanation on the files structure see this crontab syntax guide






                            share|improve this answer
















                            • 1




                              Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value.
                              – Mat
                              Sep 22 '12 at 14:30












                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            For instruction on how to edit and create cron jobs and an explanation on the files structure see this crontab syntax guide






                            share|improve this answer












                            For instruction on how to edit and create cron jobs and an explanation on the files structure see this crontab syntax guide







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 20 '12 at 21:40









                            KJS

                            211




                            211







                            • 1




                              Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value.
                              – Mat
                              Sep 22 '12 at 14:30












                            • 1




                              Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value.
                              – Mat
                              Sep 22 '12 at 14:30







                            1




                            1




                            Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value.
                            – Mat
                            Sep 22 '12 at 14:30




                            Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value.
                            – Mat
                            Sep 22 '12 at 14:30










                            up vote
                            2
                            down vote













                            This is a bit late, but for others looking here. There are easier ways than manipulating the input/output streams with VISUAL and EDITOR.



                            You can simply write your cron schedule in a file, 1 job per line, and use the command crontab [filename] so you can cat your jobs into filename as part of your script and then finally call crontab at the end if you like.



                            in fact this is the most common use as man crontab hints by listing it first






                            share|improve this answer
























                              up vote
                              2
                              down vote













                              This is a bit late, but for others looking here. There are easier ways than manipulating the input/output streams with VISUAL and EDITOR.



                              You can simply write your cron schedule in a file, 1 job per line, and use the command crontab [filename] so you can cat your jobs into filename as part of your script and then finally call crontab at the end if you like.



                              in fact this is the most common use as man crontab hints by listing it first






                              share|improve this answer






















                                up vote
                                2
                                down vote










                                up vote
                                2
                                down vote









                                This is a bit late, but for others looking here. There are easier ways than manipulating the input/output streams with VISUAL and EDITOR.



                                You can simply write your cron schedule in a file, 1 job per line, and use the command crontab [filename] so you can cat your jobs into filename as part of your script and then finally call crontab at the end if you like.



                                in fact this is the most common use as man crontab hints by listing it first






                                share|improve this answer












                                This is a bit late, but for others looking here. There are easier ways than manipulating the input/output streams with VISUAL and EDITOR.



                                You can simply write your cron schedule in a file, 1 job per line, and use the command crontab [filename] so you can cat your jobs into filename as part of your script and then finally call crontab at the end if you like.



                                in fact this is the most common use as man crontab hints by listing it first







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Mar 2 '17 at 16:10









                                Lindsay Ryan

                                211




                                211




















                                    up vote
                                    2
                                    down vote













                                    If you wish to add entries to the crontab by automation or from the command line,
                                    you can do (setting the times you wish to have)



                                    echo "* * * * * /yourpath/yourcommand 2>&1 >> /var/log/somelog.log" >> /var/spool/cron/root (or appropriate username)


                                    Then:



                                    service crond reload


                                    to reload the crontabs.






                                    share|improve this answer






















                                    • This is unportable; not all unix store the crontab files under /var/spool/cron. A better way would be through the crontab(1) command.
                                      – thrig
                                      Mar 28 '17 at 22:34










                                    • That may be true, but it was to show the principle of how you can do it on a system, and yes, crontab does the trick if you want to add line by line. It was not meant to be an absolute answer, but one if you know where your stuff is, and you need to mass add. Take it for what it is.
                                      – Chris Sprucefield
                                      Mar 29 '17 at 20:39














                                    up vote
                                    2
                                    down vote













                                    If you wish to add entries to the crontab by automation or from the command line,
                                    you can do (setting the times you wish to have)



                                    echo "* * * * * /yourpath/yourcommand 2>&1 >> /var/log/somelog.log" >> /var/spool/cron/root (or appropriate username)


                                    Then:



                                    service crond reload


                                    to reload the crontabs.






                                    share|improve this answer






















                                    • This is unportable; not all unix store the crontab files under /var/spool/cron. A better way would be through the crontab(1) command.
                                      – thrig
                                      Mar 28 '17 at 22:34










                                    • That may be true, but it was to show the principle of how you can do it on a system, and yes, crontab does the trick if you want to add line by line. It was not meant to be an absolute answer, but one if you know where your stuff is, and you need to mass add. Take it for what it is.
                                      – Chris Sprucefield
                                      Mar 29 '17 at 20:39












                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    If you wish to add entries to the crontab by automation or from the command line,
                                    you can do (setting the times you wish to have)



                                    echo "* * * * * /yourpath/yourcommand 2>&1 >> /var/log/somelog.log" >> /var/spool/cron/root (or appropriate username)


                                    Then:



                                    service crond reload


                                    to reload the crontabs.






                                    share|improve this answer














                                    If you wish to add entries to the crontab by automation or from the command line,
                                    you can do (setting the times you wish to have)



                                    echo "* * * * * /yourpath/yourcommand 2>&1 >> /var/log/somelog.log" >> /var/spool/cron/root (or appropriate username)


                                    Then:



                                    service crond reload


                                    to reload the crontabs.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Mar 28 '17 at 23:20









                                    Stephen Rauch

                                    3,298101328




                                    3,298101328










                                    answered Mar 28 '17 at 22:18









                                    Chris Sprucefield

                                    461




                                    461











                                    • This is unportable; not all unix store the crontab files under /var/spool/cron. A better way would be through the crontab(1) command.
                                      – thrig
                                      Mar 28 '17 at 22:34










                                    • That may be true, but it was to show the principle of how you can do it on a system, and yes, crontab does the trick if you want to add line by line. It was not meant to be an absolute answer, but one if you know where your stuff is, and you need to mass add. Take it for what it is.
                                      – Chris Sprucefield
                                      Mar 29 '17 at 20:39
















                                    • This is unportable; not all unix store the crontab files under /var/spool/cron. A better way would be through the crontab(1) command.
                                      – thrig
                                      Mar 28 '17 at 22:34










                                    • That may be true, but it was to show the principle of how you can do it on a system, and yes, crontab does the trick if you want to add line by line. It was not meant to be an absolute answer, but one if you know where your stuff is, and you need to mass add. Take it for what it is.
                                      – Chris Sprucefield
                                      Mar 29 '17 at 20:39















                                    This is unportable; not all unix store the crontab files under /var/spool/cron. A better way would be through the crontab(1) command.
                                    – thrig
                                    Mar 28 '17 at 22:34




                                    This is unportable; not all unix store the crontab files under /var/spool/cron. A better way would be through the crontab(1) command.
                                    – thrig
                                    Mar 28 '17 at 22:34












                                    That may be true, but it was to show the principle of how you can do it on a system, and yes, crontab does the trick if you want to add line by line. It was not meant to be an absolute answer, but one if you know where your stuff is, and you need to mass add. Take it for what it is.
                                    – Chris Sprucefield
                                    Mar 29 '17 at 20:39




                                    That may be true, but it was to show the principle of how you can do it on a system, and yes, crontab does the trick if you want to add line by line. It was not meant to be an absolute answer, but one if you know where your stuff is, and you need to mass add. Take it for what it is.
                                    – Chris Sprucefield
                                    Mar 29 '17 at 20:39










                                    up vote
                                    1
                                    down vote













                                    Try it with this command:



                                    crontab -e 


                                    then add your cron job:



                                    */1 * * * * echo "hi"


                                    in that file.






                                    share|improve this answer


























                                      up vote
                                      1
                                      down vote













                                      Try it with this command:



                                      crontab -e 


                                      then add your cron job:



                                      */1 * * * * echo "hi"


                                      in that file.






                                      share|improve this answer
























                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote









                                        Try it with this command:



                                        crontab -e 


                                        then add your cron job:



                                        */1 * * * * echo "hi"


                                        in that file.






                                        share|improve this answer














                                        Try it with this command:



                                        crontab -e 


                                        then add your cron job:



                                        */1 * * * * echo "hi"


                                        in that file.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Feb 25 '16 at 10:14









                                        techraf

                                        4,105102139




                                        4,105102139










                                        answered Feb 25 '16 at 9:50









                                        user158204

                                        191




                                        191




















                                            up vote
                                            0
                                            down vote













                                            I would like to highlight the way I describe below even though It has been mentioned before in another comment. I found it extremely useful when adding the entries to a file and then add the file to crontab.
                                            That file would contain the two entries below and the file name would be hello-world-crontab.txt.



                                            • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt

                                            • 0 09 * * * python /home/user/greeting.py

                                            Then, I executed the command below to add these entries to the crontab.



                                            • $ crontab hello-world-crontab.txt

                                            The next step is to verify these entries have been added.



                                            • $ crontab -l

                                            A brief explanation on what each entry does:



                                            • This crontab entry writes "Hello world!" into the file /home/user/greeting.txt every day at 9 am.

                                              • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt


                                            • This crontab executes a python file which prints in the command line 'Hello world!' every day at 9 am.

                                              • 0 09 * * * python /home/user/greeting.py






                                            share|improve this answer








                                            New contributor




                                            SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                            Check out our Code of Conduct.





















                                              up vote
                                              0
                                              down vote













                                              I would like to highlight the way I describe below even though It has been mentioned before in another comment. I found it extremely useful when adding the entries to a file and then add the file to crontab.
                                              That file would contain the two entries below and the file name would be hello-world-crontab.txt.



                                              • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt

                                              • 0 09 * * * python /home/user/greeting.py

                                              Then, I executed the command below to add these entries to the crontab.



                                              • $ crontab hello-world-crontab.txt

                                              The next step is to verify these entries have been added.



                                              • $ crontab -l

                                              A brief explanation on what each entry does:



                                              • This crontab entry writes "Hello world!" into the file /home/user/greeting.txt every day at 9 am.

                                                • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt


                                              • This crontab executes a python file which prints in the command line 'Hello world!' every day at 9 am.

                                                • 0 09 * * * python /home/user/greeting.py






                                              share|improve this answer








                                              New contributor




                                              SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.



















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                I would like to highlight the way I describe below even though It has been mentioned before in another comment. I found it extremely useful when adding the entries to a file and then add the file to crontab.
                                                That file would contain the two entries below and the file name would be hello-world-crontab.txt.



                                                • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt

                                                • 0 09 * * * python /home/user/greeting.py

                                                Then, I executed the command below to add these entries to the crontab.



                                                • $ crontab hello-world-crontab.txt

                                                The next step is to verify these entries have been added.



                                                • $ crontab -l

                                                A brief explanation on what each entry does:



                                                • This crontab entry writes "Hello world!" into the file /home/user/greeting.txt every day at 9 am.

                                                  • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt


                                                • This crontab executes a python file which prints in the command line 'Hello world!' every day at 9 am.

                                                  • 0 09 * * * python /home/user/greeting.py






                                                share|improve this answer








                                                New contributor




                                                SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.









                                                I would like to highlight the way I describe below even though It has been mentioned before in another comment. I found it extremely useful when adding the entries to a file and then add the file to crontab.
                                                That file would contain the two entries below and the file name would be hello-world-crontab.txt.



                                                • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt

                                                • 0 09 * * * python /home/user/greeting.py

                                                Then, I executed the command below to add these entries to the crontab.



                                                • $ crontab hello-world-crontab.txt

                                                The next step is to verify these entries have been added.



                                                • $ crontab -l

                                                A brief explanation on what each entry does:



                                                • This crontab entry writes "Hello world!" into the file /home/user/greeting.txt every day at 9 am.

                                                  • 0 09 * * * echo "Hello world!" >> /home/user/greeting.txt


                                                • This crontab executes a python file which prints in the command line 'Hello world!' every day at 9 am.

                                                  • 0 09 * * * python /home/user/greeting.py







                                                share|improve this answer








                                                New contributor




                                                SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.









                                                share|improve this answer



                                                share|improve this answer






                                                New contributor




                                                SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.









                                                answered 15 hours ago









                                                SylvesterAbreu

                                                1




                                                1




                                                New contributor




                                                SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.





                                                New contributor





                                                SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.






                                                SylvesterAbreu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f21297%2fhow-do-i-add-an-entry-to-my-crontab%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?