Script runs in terminal, fails in cron

I wrote a shell script. It works when executed from the terminal.

I installed Lingon X (cron tool) and when I run it there is completes without an error but does not do anything.

I suspect it has something to do with Apple’s SIP but have not discovered how to circumvent it without totally shutting down SIP.

What configuartion needs to be made to run a shell script via cron/lingon?

I did try going into System Preferences > Security & Privacy and giving lignon full disk access but no joy

Some thoughts that may or may not have anything to do with your problem:

  1. I believe Apple has deprecated cron in favor of launchd. That said, I’m pretty sure cron should still work.

  2. Depending on what it is you’re scheduling with cron, it may be important to understand that cron doesn’t run scripts as you. So environments you’ve set up, libraries you’ve installed, etc., might not be available to the system when it runs your script. I know in Python, you have to provide full paths, include a shebang line if you want anything but the default Python, etc. I don’t remember all the details — at this point I just have a she bag line I use for automation scripts — but it can be a headache.

Not certain about this, as you haven’t shared (and maybe can’t share) your script, but my first guess would be an environment difference. In particular, the $PATH environment variable when running a LaunchAgent includes relatively few directories,

/usr/bin:/bin:/usr/sbin:/sbin

so it may be that your script (or a command your script calls) isn’t in that restricted $PATH.

The two ways around this are to either use full paths for all executables or define the necessary environment variables in your Launch Agent. I haven’t used Lingon in several years, but it should have a way to define environment variables.

1 Like

Thanks for your suggestions. I have set the PATH the same as in my user ENV. I did not see any other parameters that I thought needed to be set. What I see when I try a simple test script is:
touch: /Volumes/Macintosh HD/Users/jrpurvis/Desktop/scriptstart-3: Operation not permitted
/Volumes/Macintosh HD/Users/jrpurvis/Scripts/Test.sh:6: operation not permitted: /Volumes/Macintosh HD/Users/jrpurvis/Desktop/scriptstart-3

Script has these simple lines:
#!/bin/zsh

touch /Volumes/Macintosh\ HD/Users/jrpurvis/Desktop/scriptstart-3
cd /Volumes/Drobo/Documents\ Archive/Personal/Desktop\ Images\ -\ Art/
pwd > /Volumes/Macintosh\ HD/Users/jrpurvis/Desktop/scriptstart-3

Script runs as it should from the command line in Terminal

The problems is permissions related to Full Disk Access. The touch, cd, and pwd command are all in the restricted PATH used by launchd, which is why expanding your PATH didn’t fix the problem.

LaunchControl, which is what I use instead of Lingon, comes with a nifty utility fdautil, which handles this permission problem. I just tried a script similar to yours. It initially failed, but then ran fine when passed through fdautil. LaunchControl actually recognized the problem, suggested the fix, and added the necessary code with a single click.

I don’t like the idea of suggesting you buy another launchd app, but that’s kind of where I’m ending up. I’m sure there are other ways to get your script to run with full disk access, but I don’t know what they are.

I appreciated all of your suggestions. I did look at LaunchControl. It seems to work better than Lingon but my script is still not working.

I have decided to give up on creating my own automation and just buy Hazel. What I was trying to do was scrape my Desktop for saved files and store them in appropriate places. Having spent a few hours working on it and with Apple determined to ‘protect us’ I think that the commercial product will occupy far less of my time going forward.

Again, I appreciate your taking time to answer.

Please see my responses to drdrang

Appreciate your feedback!

I’m a big believer in rolling my own. But when it comes to automating file locations on the Mac, I go with Hazel too.