I am trying to get launchd (which is macOS's alternative for cron) to run a job for me. It doesn't work, while running the commands on a bash launched with sudo does. I tried redirecting the output to files, but examining them hasn't enlightened me, either. I like to have access to this 'primal' environment launchd uses to execute commands, so that I can experiment directly there and see what is missing.

PS: Here is the script I run in my launchd job (as root):

#!/usr/bin/env bash
export HOME=/Users/evar
source /users/evar/.bashrc
/Users/evar/anaconda/bin/python /Base/_Code/Misc/hosts/updateHostsFile.py --auto --replace --backup &> /Users/evar/log/hosts.out2
# tmux new -d -s hosts "/Users/evar/anaconda/bin/python /Base/_Code/Misc/hosts/updateHostsFile.py --auto --replace --backup" # I tried this, but sudo tmux kept saying no sessions while logs kept said "duplicate session hosts".

1 Answer 1


At the beginning of your script you could do something like

env > /tmp/myscriptrun_env.$$

This will put the inherited environment into a file in /tmp.

Now from the command line you can use the command env -i /bin/sh to generate a new shell with a fresh environment. Inside this shell you can source the /tmp file you created.

You may want to edit it first and add export in front of each line to better mimic the launched environment.

  • Then I do sudo bash -c “env < /tmp/my...”?
    – HappyFace
    Oct 31, 2018 at 11:46
  • No. The output is, effectively, a script. But rather than use it directly you should manually inspect it and see if anything important is missing that your program relies on (normally a PATH, or similar) and then set them at the beginning of your script Oct 31, 2018 at 14:09
  • You see, as far as I can see NOTHING is missing. So I want to be in that exact environment and experiment to see what I can uncover.
    – HappyFace
    Oct 31, 2018 at 19:20
  • 1
    Use env -i /bin/sh to get a blank environment then source /tmp/my.... to import the values Oct 31, 2018 at 20:16
  • Add this to the answer, I have already marked it as correct. Thanks!
    – HappyFace
    Oct 31, 2018 at 20:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.