Nightly file transfer to Google Drive fails in automated jobs on macOS

I’m stuck with a weird issue on my Mac. I’ve got this simple bash script that edits a file and copies it to my Google Drive folder. It runs fine when I do it manually in Terminal, but it’s a no-go when I try to automate it with cron or launchctl.

Here’s a quick look at what I’m trying to do:

#!/bin/bash

echo "new content" > /Users/myname/Documents/temp.txt

cp /Users/myname/Documents/temp.txt /Users/myname/Google*/Drive*/updated.txt

exit 0

I’ve tried a bunch of things:

  • Using wildcards for folder names with spaces
  • Escaping spaces in the path
  • Creating links to avoid spaces

Nothing seems to work when it’s automated. I keep getting errors like “Operation not permitted” or “No such file or directory”.

I’m running this on a MacBook Pro with Sonoma 14.5. Any ideas why it works in Terminal but not in cron or launchctl? I’m scratching my head here!

I’ve run into similar issues with automated file transfers on macOS, particularly when interfacing with Google Drive. The root of the problem is often related to permissions and restricted access when scripts are executed outside a user session.

In my experience, using explicit full paths in your script rather than wildcards is crucial, as Google Drive folder names may vary. You also need to ensure that the task runner (cron or launchctl) has the necessary permissions—granting Full Disk Access can resolve many issues. Additionally, verify that the plist file for launchctl is correctly set up and consider using tools like rclone for more reliable interactions with Google Drive. These adjustments helped address my automation challenges.

hey mate, i had a similar problem. try using absolute paths and check if your script has the right permissions. also, make sure google drive is actually mounted when the script runs. sometimes it’s not connected in automated jobs. you might wanna look into using the google drive API instead of relying on the local folder. it’s a bit more work but way more reliable for automated stuff.

Having dealt with similar macOS automation headaches, I can attest it’s likely a permissions issue. When scripts run via cron or launchctl, they often lack the necessary access rights to interact with certain directories or applications.

One approach that worked for me was using the ‘sudo’ command in the script to elevate permissions. However, this requires editing the sudoers file, which can be risky if not done correctly.

Another solution is to create an AppleScript that performs the file operations and then use osascript to run it from your bash script. AppleScript tends to have fewer permission issues when automated.

Lastly, consider using a dedicated sync tool like rclone or Google Drive’s official command-line tool. These are designed to handle authentication and permissions more robustly in automated environments.