I have the following shell script that evaluates a folder full of json files and parsing them into a new file.
#!/bin/sh
# Location of Reporter-App sync folder
REPORTER_FOLDER=/Users/pirijan/Dropbox/Apps/Reporter-App
# Location of output file on public Dropbox folder
OUTPUT=/Users/pirijan/Dropbox/public/fit-report.json
cd $REPORTER_FOLDER
rm fit-report.json
rm -report.json
rm Icon^M
# get number of json files in folder, excluding 'fit-report.json'
number_reports=$(ls -l | wc -l)
echo "number of reports: $number_reports"
LS=$(ls)
echo $LS
echo
oldest_report=$(ls -t | grep -v 'fit-report.json' | tail -n1)
echo "oldest report is: $oldest_report"
#start json objects array
echo '[' >> fit-report.json
echo ""
# evaluate each iOS reporter-app report file ↴
for (( i=2; i<=$number_reports; i++ ))
do
echo "⚡︎ Parent iteration is: $i"
# set current file to evaluate in this for loop
current_file=$(ls -t | head -n $i | sed -n $i'p')
echo "current file is: $current_file"
# get number of snapshots in the current file
current_file_snapshots=$(cat $current_file | jq '.snapshots | length')
echo "number of snapshots in current file is: $current_file_snapshots"
# parse each snapshot individually in the current file
for (( ii=0; ii<$current_file_snapshots; ii++ ))
do
echo "✈︎ nested for loop at iteration $ii"
# don't append a ',' to the oldest snapshot in the last file.
if [ $current_file == $oldest_report ] && [ $ii == $((current_file_snapshots - 1 )) ]
then
echo
echo "---------------------"
echo "✿ this is the last snapshot"
cat $current_file | jq ".snapshots[$ii] | {date, responses}" >> fit-report.json
else
cat $current_file | jq ".snapshots[$ii] | {date, responses}" >> fit-report.json
echo ',' >> fit-report.json
fi
done
echo "----"
done
# end the json array
echo ']' >> fit-report.json
# copy fit-report.json to dropbox public folder ↴
cp fit-report.json $OUTPUT
rm fit-report.json
# echo on complete
echo '♥︎︎ fit-report updated'
If I run this manually myself, the output is as expected.
However, I want this script to run automatically so I set up a launchd plist in my LaunchAgents folder:
<plist version="1.0">
<dict>
<key>Label</key>
<string>FitReport</string>
<key>Program</key>
<string>/Users/pirijan/Dropbox/projects/fit-report/report.sh</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>WorkingDirectory</key>
<string>/Users/pirijan/Dropbox/projects/fit-report/</string>
<key>LowPriorityIO</key>
<false/>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>23</integer>
<key>Minute</key>
<integer>30</integer>
</dict>
<key>disabled</key>
<true/>
</dict>
</plist>
When the script is run through launchd the json file it returns only contains:
[]
I've tried throwing placeholder output in the first for loop and in the second, and only the first for loop seems to be evaluated. commenting out the second for and running it manually, produces the same result as launchd.
When I tried to observe the log using the instructions described in http://obscuredclarity.blogspot.ca/2011/02/debugging-launchd-configuration-on-mac.html, I never saw anything logged.
Do you know what might be causing this? I have no idea what to do next.
Thanks!
I was able to log stderr output to a file using
<key>StandardErrorPath</key>
<string>/Users/Shared/sillyscript/sillyscript_err.log</string>
(source: http://erikslab.com/2011/02/04/logging-with-launchd/)
Now I know that the problem is that jq (http://stedolan.github.io/jq/) is not being properly evaluated by launchd. So at least I know what's wrong now, just have to figure out the fix..