How do I setup a cron job on OS X to run a curl command at a specific time every day?
Start by running the command:
crontab -e
This will open up the crontab for your user in a text editor. Cron uses a specific notation for scheduled jobs. The format below shows the fields for a user crontab entry (which need to be separated by tabs).
min hour day_of_month month day_of_week command
To schedule a curl command to run at 3am daily, you can insert the line:
0 3 * * * curl args...
Notice how the minutes and hour correspond to 3am (side note: cron uses 24 hour time format, no am or pm). The asterisks that follow mean every day of month, every month, every day of week.
Cron will not be able to wake up your computer from sleep but you may find this post helpful crontab to wake osx from sleep
Related videos on Youtube
user1729603
Rails developer that loves finance, economics, business & tech.
Updated on September 18, 2022Comments
-
user1729603 over 1 year
I have a CURL command I want to run every day at say 3am, but I am not sure about how to set that up and the guides are all weird.
Assume I have a lot of terminal/bash experience, but I have never setup a cron before.
Is it possible for the cron job to wake up my computer, even if it is sleeping and the lid is closed (macbook pro), just to run this one curl command and then put it back to sleep?
How do I set it up either way?
-
user1729603 over 7 yearsWhen I do
crontab -e
I see this in my terminal:$ crontab -e crontab: no changes made to crontab
, and then it opens my text editor with the filecrontab.some-random-string
from the folder/tmp
. Once I edit that and save it, nothing happens in my terminal. If I docrontab -l
, the job I just entered doesn't show up. Then if I docrontab -e
again, it opens another blankcrontab.some-random-string
. I also tested the first crontab I saved by setting the time to be 2 mins from when I saved it and the cron never ran. What could be going on? -
diametralpitch over 7 yearsWhich text editor is opening when you run 'crontab -e'?
-
user1729603 over 7 yearsMy default one,
atom
. That's what launches when I open anything to be edited in my terminal. This is what I have in my~/.bash_profile
,export EDITOR='open -a "/Applications/Atom.app"'
. -
Adam Luchjenbroers over 7 yearsHmm, if you run atom from the command line on its own, does it wait for atom to exit before prompting for another command (if not, you may want to try a different text editor).
-
user1729603 over 7 years@AdamLuchjenbroers I am not sure I fully understand your question. I use atom for all of my code editing. So if I want to edit any file, I just do
atom .
inside the directory oratom <filepath>
. -
user1729603 over 7 yearsThanks for that tip....but I think I would prefer doing it manually at the command line or via text editor to learn :)
-
user1729603 over 7 yearsAny further thoughts? I still haven't made progress on this.
-
diametralpitch over 7 yearsThe temporary file behavior you are observing while editing is expected. Could you switch your text editor to to a terminal-based editor, perhaps vim, and see if you get a different result?
-
user1729603 over 7 years@diametralpitch How do I do that?
-
diametralpitch over 7 years@marcamillion You can set the EDITOR environment variable to /your/path/to/vim (maybe /usr/bin/vim). Don't use 'open -a' for this one.
-
user1729603 over 7 yearsOk so I just realized something. I kept seeing a
You have mail
message in my Terminal. So I recently didmail
and it opened and I saw a bunch of messages that seem to indicate the Cron attempted to run. I opened the first one and it says the following:/bin/sh: -c: line 0: unexpected EOF while looking for matching "' /bin/sh: -c: line 1: syntax error: unexpected end of file
and this is the line item I saw in my inbox:U 2 [email protected] Tue Nov 22 10:23 21/792 "Cron <marcgayle@MG-MBP> * curl -o ../db-bkups/$(date +""
-
user1729603 over 7 yearsHow do I edit this crontab to fix it? I just tried doing
crontab -e
again and it sent me to a fresh temp crontab file. I suspect it may be the path, so I want to edit it to add the full path I want the files saved to. -
user1729603 over 7 yearsIt seems the simple way to run it with
vi
is just to do ` vi crontab -e`. So I did that and redid the cron command. It was empty though, so that means the old one is still there. How do I edit/delete that one? I am still not sure if the the cron executed. It doesn't seem like it did....but maybe I will just wait another day to confirm? -
diametralpitch over 7 years'crontab -r' will remove the current crontab
-
diametralpitch over 7 yearsCan you confirm you have no syntax error in your curl command?
-
diametralpitch over 7 years@marcamillion to make testing easier on yourself, try scheduling a command that just adds some text to a file that runs every minute
-
user1729603 over 7 years@diametralpitch I was able to remove the legacy crontab with that command...thanks. I can confirm I have no syntax error in my curl command. I ran it manually many times and it works. The issue is the save location I assume. I think when it was being run, it was being run from the
/var
directory as opposed to the directory I wrote the command in. -
diametralpitch over 7 yearsInstead of using a relative path to your output file, would using the absolute path now solve your issue?
-
user1729603 over 7 yearsI tried that when I edited crontab using
vi
, but I haven't seen any backups made to that directory. I am also not seeing anymail
with errors. So now it just doesn't seem to be working at all. -
user1729603 over 7 yearsOk I just added the following command to my
crontab
:1 * * * * echo "This is executed by CRON."
-
user1729603 over 7 yearsIt's been at least a few minutes and I don't see any output to my terminal, so it seems this job is not being executed -- or would it be output somewhere else?
-
diametralpitch over 7 yearsThe cron jobs will run in their own shell so you won't see the output. What you can do though is echo something and redirect it into a file like "echo my_text > /path/to/my/file" then use "tail -f /path/to/my/file" to monitor it.
-
diametralpitch over 7 years@marcamillion any luck?