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 mail, so it seems that cron thinks everything is going alright.

The way that I've got the cronjob set up is like so:

*/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 xterm. I.e.:

*/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?

  • pgrep is usually superior to ps | grep; if you stick with ps | grep at least use the ps caux | grep -iq 'my_scrip[t]' form to avoid the matches itself problem. – thrig Dec 1 '16 at 22:56
  • @thrig Ok, thanks for the advice. Strangely, I didn't have the "matches itself" problem when I tested this out with ps caux, although said problem happen with ps aux. Not exactly sure why that is. EDIT: Oh, I just realized that caux limits the column output to just contain the executable name, rather than the full command. So I guess "matches itself" wouldn't happen there because it would have to match the grep process, which is the actual executable that's running. – GDP2 Dec 1 '16 at 23:00
  • Still problematical, as shell scripts can run as "/bin/sh /path/to/script" in which case ps c will only see the /bin/sh bit and not the desired script name. pgrep -f my_script should better find it. – thrig Dec 1 '16 at 23:26
  • @thrig Yes, that's true. However, my script isn't a shell script; it's built with the Racket language. Right now, this is the only racket process which is running on the machine in question so I don't need to worry about other racket processes doing things like that. I.e., I'm actually grepping for the racket interpreter, not the script name, so it's working fine for me. I just put in the question that I'm grepping for the script name since I didn't want to add unnecessary details. Thanks again for the advice, though. – GDP2 Dec 1 '16 at 23:41
  • Hmm, * * * * * /usr/bin/osascript -e 'display dialog "hello"' doesn't show anything for me. Perhaps instead hand the notifications off to something like growl instead? growl.info – thrig Dec 2 '16 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.