0

I have manually added a command to my crontab file (Monterey 12.6.9). Pretty simple; the crontab entry is something like:

0 10 * * * mv /Users/me/Desktop/test /Users/me/Desktop/test2

Which means, at 10am every day, do this rename.

But it simply doesn't work. Other commands in my crontab file work fine, which suggests that cron IS actually executing. I HAVE given the cron app "Full Disk Access" in Security & Privacy. That mv command works fine as a terminal command. Not sure where the cron log is, so I haven't pieced together what is happening. This is confusing. Advice?

Seamus
  • 4,547
user637
  • 11

1 Answers1

0

This works on my Ventura system, and it should work on your Monterey system...

I don't think cron has a "log file" per se, and so I'll suggest you get into the habit of creating and using a file to capture the output of stdout and stderr from all cron jobs. It's the best way to troubleshoot mysterious cron issues.

FROM:

0 10 * * * mv /Users/me/Desktop/test /Users/me/Desktop/test2

TO:

0 10 * * * mv /Users/me/Desktop/test /Users/me/Desktop/test2 >> /Users/me/Desktop/test_log 2>&1

The information in the file test_log should contain something useful, and suggest where the issue is. I suspect - in this case - it's something simple.

Here's the breakdown on the addition:

  • >> a redirect (with augmentation instead of replacement)

  • 2>&1 redirect stderr to stdout; effectively combines both streams into a single stream

Another good habit with crontab entries that name an executable file is to specify the full path to that executable. You'll get away with it when the executable is in the PATH, but it may not be obvious what cron's PATH actually is.

Yet another good habit with cron is to turn OFF the default MAILTO feature. This is done by adding this line at or near the top of your crontab:

MAILTO=""
Seamus
  • 4,547
  • Thank you. I will try that. Now I just noticed that the cron daemon has been mailing me an error message. It is
    /bin/sh: -c: line 0: unexpected EOF while looking for matching `)' /bin/sh: -c: line 1: syntax error: unexpected end of file What does that mean??? Again, everything else in my crontab file is running properly (these are from the scheduling for SuperDuper), and my new line is on something like line 5.
    – user637 Sep 20 '23 at 02:25
  • @user637: Yeah - that's a fairly inconvenient default, but it's easy to eliminate it. I've edited my answer to explain. – Seamus Sep 20 '23 at 02:28
  • I entered that revised command aimed at a few minutes ago, and NOTHING came out. No test_log appeared on my Desktop. That crontab line is simply not being executed. Also, I don't see any edit in your answer that explains. – user637 Sep 20 '23 at 02:34
  • Could you please edit your question to post a crontab entry that *does* work? IOW - a counter-example. – Seamus Sep 20 '23 at 02:43
  • OK, sorry. I did that wrong before. My test_log DID come out, and there is NOTHING in it. And, haha, test DID get renamed to test2 with that test_log line! So that line worked completely with no errors. Working on posting more. – user637 Sep 20 '23 at 12:30
  • OK, geez Louise, it ALL WORKS NOW. Maybe asking it to produce a test_log reset something? But thanks for your help. But I do have a related problem. I'd like to schedule a file to get renamed with a date. I try 0 10 * * * mv /Users/me/Desktop/test* /Users/me/Desktop/test_$(date +%m-%d-%Y), and it doesn't work. Doesn't do anything. But when I do mv /Users/me/Desktop/test* /Users/me/Desktop/test_$(date +%m-%d-%Y) on the terminal, it works perfectly. Why is cron incapable of setting a date? I guess I can ask for a log file, but I'm just not sure that date request is correct. – user637 Sep 20 '23 at 14:04
  • OMG, for cron, you need BACKSLASHES! 0 10 * * * mv /Users/me/Desktop/test* /Users/me/Desktop/test_$(date +%m-%d-%Y). Piece of crap! – user637 Sep 20 '23 at 17:56
  • 2
    @user637 See https://unix.stackexchange.com/questions/29578/how-can-i-execute-date-inside-of-a-crontab-job – Barmar Sep 20 '23 at 19:25
  • @user637: Your original question didn't include anything re how to escape a date format specification. Barmar's comment contains a link that addresses that. Hopefully, this has answered all of your follow-up questions; if not, please consider submitting a new question. – Seamus Sep 20 '23 at 19:36
  • Yes, thank you. Barmar had it right. Just didn't manage to see it. This is NOT well known. You certainly don't need backslashes running it as a regular terminal command. Is this generally true that commands that work in terminal don't work in cron? That's right, my original question wasn't about this. – user637 Sep 21 '23 at 02:05
  • @user637: No, in most cases, you don't need to escape the % character, but that is covered in man 5 crontab. And wrt your original question, if you feel that my answer helped in some way, I'd ask you read this topic from the help center. – Seamus Sep 21 '23 at 20:46
  • @user637: Yes, you did. I was only trying to point out the need for that was covered in the documentation; i.e. man 5 crontab. – Seamus Sep 22 '23 at 20:40