0

So I've got these three scrips: one moves pictures to a file (photosscript2.sh), one deletes pictures over 7 days old from that file (photosremove.sh), and one calls both files to keep the folder updated (every-minute.sh). I'm trying to set every-minute.sh up with CRON. Everything works if I type in the file name, ./every-minute.sh manually. But I want it to run in the background with CRON and it does not work. So I added a PATH to CRON. No go. So I had CRON cd to / first, which is where everything is located. Still no go. WTF is wrong. WHY won't it run? Note: CRON works 100% without a PATH on my mom's computer.

#!/bin/bash
#photoscript2.sh
find /Users/username/Pictures/Photos\ Library.photoslibrary/Masters/2019/* -mtime -7 -exec cp {} /Users/username/Documents/Recently\ Added/ \;

#!/bin/bash
#photosremove.sh
#PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/user
#export DISPLAY=:0.0
#someone on here said to add this.  did nothing.
find /Users/username/Documents/Recently\ Added/ -mtime +7 -exec rm {} \+

#!/bin/bash
#every-minute.sh
#this runs the other two scripts.  I'm trying to get this file to be activated by CRON.
/User/username/photosscript2.sh
/User/username/photosremove.sh

#CRONTAB -e
SHELL=/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/user
*/5 * * * * cd / && /User/username/every-minute.sh

Error messages: Before adding PATH:

find: /Users/username/Pictures/Photos Library.photoslibrary/Masters/2019/*: No such file or directory
find: /Users/username/Documents/Recently Added/*: No such file or directory

After adding PATH:

/bin/sh: /User/username/every-minute.sh: No such file or directory
  • I'm a Linux user, so not 100% sure this would apply, but on Linux cron runs as root. So a path like ~/photosscript2.sh would actually be pointing to a script in root's home directory (~ expands to $HOME). So I suggest using the full path to those scripts. – Jeremy Davis Sep 1 '19 at 1:27
  • Do the files mentioned in the error messages actually exist? In the crontab entry, the path is certainly wrong ("/User/..." instead of "/User**s**/..."). Is the username right on that computer? – Gordon Davisson Sep 1 '19 at 2:57
  • @JeremyDavis I don't understand. That should be the full path. /User/user/photoscript2.sh? The third file posted is the file that activates the other two. I'll edit my post and make the file names more clear. – DoesItMatter Sep 1 '19 at 5:15
  • 2
    @DoesItMatter /User does not exist on macOS, it needs an "s" on the end. Please verify that there are no other typos. – Gordon Davisson Sep 1 '19 at 5:24
  • 1
    FWIW, here's a wiki page for testing cron jobs: github.com/turnkeylinux/tracker/wiki/How-to-test-a-cron-job It's for Linux, but I imagine there are enough in common for it to be useful... – Jeremy Davis Sep 1 '19 at 22:19
1

You do not need the glob (*). Try the following for the photoscript2.sh script.

#! /bin/sh
find /Users/username/Pictures/Photos\ Library.photoslibrary/Masters/2019 \
    ! \( -name '.*' -o -name 2019 \) \ 
    -mtime -7 -exec cp {} /Users/username/Documents/Recently\ Added/ \;
  • Hi, I tried this and now I have two error messages: find: : unknown primary or operator /Users/username/photosscript2.sh: line 7: -mtime: command not found – DoesItMatter Sep 1 '19 at 22:11
  • Okay so I took out the \ to denote a new line (I guess cron doesn't like it?) and now the error message is: find: /Users/username/Pictures/Photos Library.photoslibrary/Masters/2019: Operation not permitted – DoesItMatter Sep 1 '19 at 22:16
  • @DoesItMatter- There seems to be issues with your editing. The proposed script works fine when run in cron. There aren't any issues with using back-slashes. The last error message indicates that there may be a GateKeeper issue or whatever Apple calls it. But before you continue on, there are issues with your copy script. It will continually copy the same files until their last modification date is seven days old or older. – fd0 Sep 2 '19 at 12:41
  • So from what I see it's stupid Apple stuff that is preventing this from running. System Integrity Prevention or something. That sucks. On your second point, it does not copy the same files more than once. I've ran it multiple times manually and if the file is already there then it ignores it. I only want files that are less than 7 days. The second script then deletes files that are older than 7 days. – DoesItMatter Sep 2 '19 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .