Mac下Shell脚本复制到Google Drive正常,cron/launchctl执行失败
Mac上cron/launchctl执行Google Drive复制脚本失败问题
设备环境:MacBook Pro(2 GHz四核Intel Core i5,16GB内存),运行Sonoma 14.5
问题场景
尝试设置夜间任务,通过Shell脚本编辑本地文件后,复制到Google桌面集成的本地Google Drive目录。脚本在终端执行正常,但添加到cron或通过launchctl执行时失败。
测试脚本(已替换<user>为实际用户名)
#!/bin/bash echo "test data" > /Users/<user>/Scripts/Scratch/testfile.txt cp /Users/<user>/Scripts/Scratch/testfile.txt /Users/<user>/Google*/My*/copied.txt exit 0
尝试过的解决方法及报错
怀疑Google Drive目录的空格是问题根源,尝试以下方案均无效(终端执行正常,cron/launchctl执行失败):
- 使用通配符
*替代目录名中的空格 - 手动转义空格:
cp /Users/<user>/Scripts/Scratch/testfile.txt /Users/<user>/Google\ Drive/My\ Drive/copied.txt - 创建硬链接或符号链接规避空格路径
cron执行报错
- 转义空格版本报错:
cp: /Users/<user>/Google Drive/My Drive/copied.txt: Operation not permitted - 通配符版本报错:
cp: /Users/<user>/Google*/My*/copied.txt: No such file or directory
已解决情况与剩余问题
- 给cron授予全磁盘访问权限后,cron的执行问题已解决
- 给launchd和launchctl授予相同的全磁盘访问权限后,launchd执行脚本仍失败
内容的提问来源于stack exchange,提问作者Orestes
相关产品推荐
相关产品推荐

