0

I'm trying to use Launchd to run the following shell script:

#!/bin/sh

## wait for sunset, touch file

NIGHTTIME='/Users/mnewman/Documents/webcam/nighttime.txt'

sunwait civ down  14.98N 102.09E
touch "$NIGHTTIME"

"sunwait" is an executable which runs in the background and waits for sunset/sunrise and then quits. In this case I'm setting it to wait until civilian twilight sunset at my geographic location.

If I run this script from the command line it works fine. If I run it using Launchd, the touch command runs before sunwait finishes. I need for sunwait to finish before executing the next line. How do I do this?

3
  • Do you see sunwait still in the process table? That is to say, how do you know that touch runs before sunwait returns, rather than sunwait failing and exiting early in this case? Commented Jan 7, 2015 at 23:35
  • 1
    I'd strongly suggest putting exec >/tmp/wait-for-night.log 2>&1; set -x at the beginning of your script and looking at the contents of /tmp/wait-for-night.log after reproducing this behavior. Commented Jan 7, 2015 at 23:36
  • (As an aside -- I'd encourage you to use lowercase names for variables scoped within your script, such as NIGHTTIME; all-uppercase names are, by convention, reserved for environment variables and builtins, such that using lowercase names for shell variables prevents unintentional conflicts). Commented Jan 7, 2015 at 23:39

1 Answer 1

1

The most likely case here is that you have sunwait installed in a location that isn't in launchd's PATH. The solution is simply to specify the PATH to use in your script -- and, as a safety measure, to tell your script to not create the file if sunwait fails.

#!/bin/sh
PATH=/bin:/usr/bin:/usr/local/bin:/opt/local/bin
nighttime=/Users/mnewman/Documents/webcam/nighttime.txt

sunwait civ down  14.98N 102.09E || exit
touch "$nighttime"

Changing the shebang line to #!/bin/sh -e would also have the effect of causing the script to bail early if sunwait failed to run, though using set -e has significant caveats (documented in BashFAQ #105).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.