I've got a script in OS X that looks in a given directory for the file with the most recent mtime, sees if it's (past) a certain size, and, if so, creates an AppleScript dialog box + repeats an invocation of the
say command until the dialog box is closed. I've verified that this script works for any given directory including the directory that I'm interested in. However, it only seems to work properly when I run it from an interactive shell (i.e., via zsh in Terminal.app, xterm, etc. or Eshell in Emacs). See, when I run the script as a cronjob, all it does is run the
say invocation in the script once; no dialog box and no repetition of
say until the box is closed, which is what I'm expecting it to do. However, I'm not getting any error messages in my
cron thinks everything is going alright.
The way that I've got the cronjob set up is like so:
SHELL=/bin/zsh */1 * * * * . $HOME/.zshrc && ps caux | grep -iq my_script && : || $HOME/bin/my_script /some/dir
Translation: it runs every minute, first sourcing
~/.zshrc (to set environment variables like
$PATH et al.). Then it runs
ps caux, grepping for my script's process name, and if a match is found then no-op. Otherwise, run my script, targeting
/some/dir. The check to see if my process is running is done to make sure no overlapping instances of it occur and I've also verified that this works correctly.
So, when I run the code for that cronjob in a terminal, it works fine. However, as explained previously, something is causing the cronjob itself to not run as expected. When I consulted google about this sort of issue, I found this question on SO that seems to mirror my problem, but for Linux. I tried the accepted answer there, which states to try launching the job via
*/1 * * * * . $HOME/.zshrc && xterm -e ps caux | grep -iq my_script && : || $HOME/bin/my_script /some/dir
Yet, this was to no avail. Again, running the code for this cronjob via an interactive shell worked as expected, but
cron itself still ran the script quirkily, with the only difference being that an
xterm window appeared for a split second every minute.
I'm confused as to why this is happening and I'd like for the script to work as it does when run from an interactive shell. How can I accomplish this and why is
cron running the script in an unexpected way?