Mounted devices not visible when using script with cron

If I run a script in Terminal eg. diskutil list external it works fine, but when I run the same script via cron the command doesn't have any output and I can't access a mounted USB disk.


Full disk access is allowed for Terminal, cron, smbd and diskutil. A hint from another website points me to remove unnecessary xattrs. I have a xattr named "com.apple.lastuseddate#PS" which I removed with xattr -d.


My script simply check the availability of a USB drive with:


if [[ $(mount | awk '$3 == "/Volumes/TKBackup" {print $3}') != "" ]];
or
if [[ $(diskutil list external | awk '$3 == "TKBackup" {print $3}') != "" ]]; 


and than use rsync to backup data to this drive. But that doesn't work via cron.


Any hints?


Posted on Sep 15, 2023 1:32 AM

Reply
6 replies

Sep 15, 2023 11:36 AM in response to tkrampe

tkrampe wrote:

If I run a script in Terminal eg. diskutil list external it works fine, but when I run the same script via cron the command doesn't have any output and I can't access a mounted USB disk.

Full disk access is allowed for Terminal, cron, smbd and diskutil. A hint from another website points me to remove unnecessary xattrs. I have a xattr named "com.apple.lastuseddate#PS" which I removed with xattr -d.

My script simply check the availability of a USB drive with:

if [[ $(mount | awk '$3 == "/Volumes/TKBackup" {print $3}') != "" ]];
or
if [[ $(diskutil list external | awk '$3 == "TKBackup" {print $3}') != "" ]];

and than use rsync to backup data to this drive. But that doesn't work via cron.

Any hints?


verify you have Terminal.app added to the

>System Settings>Privacy & Security>Full Disk Access





ref: Controlling app access to files in macOS


Sep 16, 2023 5:50 PM in response to tkrampe

tkrampe wrote:

Fix it myself, question can be closed.

It is usually nice if you tell everyone what you did to resolve the issue so that anyone else finding your thread will not be stuck in the same situation. It is really frustrating searching for items online and finding a forum thread where someone figured it out without sharing the answer. Plus I would know what to tell anyone else if the question comes up again.


I did begin typing another response a couple days ago, but never had time to finish it since I was trying to research it a bit. One of the things I discovered is that "cron" has been deprecated for a long time now with macOS. While it is still an option, it is better to take advantage of the "launchd" service to schedule tasks....this is what macOS does with "cron" these days anyway from what I've read.



Sep 16, 2023 11:44 PM in response to HWTech


You are absolutly right @HWTech. The error was in the IF query and in the further script itself, cron obviously has no PATH variables set.


In my case

if [[ $(mount | awk '$3 == "/Volumes/TKBackup" {print $3}') != "" ]];


doesn't work because of the missing path. Changing it to

if [[ $(/sbin/mount | awk '$3 == "/Volumes/TKBackup" {print $3}') != "" ]];


works perfect. All other commands in my script had the same problem. In my case whereis is your friend.


I'm a little bit old school and I like cron but for anyone who wants to do this with launchd you can find the relevant information here - https://www.maketecheasier.com/use-launchd-run-scripts-on-schedule-macos/



Mounted devices not visible when using script with cron

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.