Using jq to parse JSON in launchd

房东的猫 提交于 2020-01-11 12:06:17

问题


I have a shell script that evaluates a folder full of JSON files which runs fine when invoked directly but fails when run from launchd. Specifically, launchd fails on this line:

current_file_snapshots=$(cat $current_file | jq '.snapshots | length')

Because:

jq: command not found

I looked through the jq docs to figure this out, but there's no mention of this problem. I haven't been able to figure out how to get jq (or any similar third party tool) to work through launchd.

Thanks!


回答1:


Ahh, looks like I assumed that launchd shared the same PATHs as my normal bash shell. Explicitly spelling out where JQ lives seemed to work:

So at the top of the file I added:

JQ=/usr/local/bin/jq

and replaced all instances of 'jq' with:

$JQ



回答2:


As the very first process that runs in Mac OS X, launchd has no environment (including a PATH parameter) except for what you specify in /etc/launchd.conf. You could create that file and add

setenv PATH "/usr/local/bin"

If you want the full environment associated with your regular shell sessions, you should execute your script explicitly as a login shell:

bash -l myscript.bash


来源:https://stackoverflow.com/questions/23598328/using-jq-to-parse-json-in-launchd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!