2

I have a Mac mini running macOS Monterey, and it has InfluxDB running in a Docker container. I'm trying to set up a nightly cronjob via a shell script I'm calling to back the data up, zip it, and only keep the seven most recent backups. The backing up part works perfectly regardless of whether I'm running it manually or via cron, but the bit that's giving me grief is in cron for the shell expansion to get the current number of backup files so I can keep only the most recent ones.

I've stripped the whole thing down to just trying to figure out how to get shell expansion working at all under cron, with zero luck.

The simplest thing I've come up with is this, which behaves (correctly) as the following when running it manually:

$ /bin/bash -c 'BACKUPS=(/Users/virtualwolf/Documents/InfluxDB_Backups/*) && echo "Number of backups: ${#BACKUPS[*]}" && echo "Oldest backup: ${BACKUPS[0]}"'
Number of backups: 10
Oldest backup: /Users/virtualwolf/Documents/InfluxDB_Backups/2022-04-12_00-30.zip

If I put that exact same thing into cron, I get the following:

Number of backups: 1
Oldest backup: /Users/virtualwolf/Documents/InfluxDB_Backups/*

I know by default cron uses /bin/sh and I've tried setting SHELL=/bin/bash at the top of my crontab as well, to no avail (though I wouldn't have thought that would have any effect if I'm either calling a script directly with a shebang of #!/bin/bash?).

Am I missing something obvious here?

[EDIT] To clarify things a bit, I'm currently testing solely with cron and have excluded running an actual script file just to get down to the minimum possible scenario, the relevant crontab entry looks like this:

* * * * * /bin/bash -c 'BACKUPS=(/Users/virtualwolf/Documents/InfluxDB_Backups/*) && echo Number of backups: ${#BACKUPS[*]} && echo Oldest backup: ${BACKUPS[0]}'

If I copy that exact same line (/bin/bash -c 'BACKUPS=(/Users/virtualwolf/Documents/InfluxDB_Backups/*) && echo Number of backups: ${#BACKUPS[*]} && echo Oldest backup: ${BACKUPS[0]}') into my terminal, it works as expected and it lists ten files and shows the oldest backup. When it's executed by cron, the shell expansion hasn't occurred and it's taken the BACKUPS variable as the literal string /Users/virtualwolf/Documents/InfluxDB_Backups/*.

11
  • Is your home folder encrypted? Is it unencrypted when the cronjob runs?
    – muru
    Commented Apr 19, 2022 at 3:49
  • The whole disk is encrypted using FileVault and it's unlocked at boot when I put my password in, so no issues there. :) I've been testing by just having the thing run every minute. Commented Apr 19, 2022 at 4:00
  • If you put the same commands in your backup script in Cron, do you get the expected output?
    – muru
    Commented Apr 19, 2022 at 4:05
  • When you say "script" do you mean that the commands are in an executable file, and you're executing the file from cron?
    – dg99
    Commented Apr 19, 2022 at 4:16
  • 1
    @Seamus Yeah that's a good call, I've updated the title. Commented Apr 20, 2022 at 0:32

1 Answer 1

3

So thanks to @muru's excellent questioning, it turns out this was nothing to do with Cron and shell expansion at all, and was instead a case of the /usr/sbin/cron executable not having full disk access in System Preferences > Security & Privacy > Privacy.

I unlocked the preference pane, went into "Full Disk Access", clicked the + and hit CmdShiftG to bring up the Go To Folder dialog, popped in /usr/sbin and found cron and added it, and now everything works a treat.

You must log in to answer this question.

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