-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from MartinBernstorff/mb/feat_add_shell_scrip…
…t_logging add shell script logging
- Loading branch information
Showing
1 changed file
with
45 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,54 @@ | ||
#!/bin/zsh | ||
PROJECT="$(dirname "${0}")" | ||
LESSONS_DIR="$HOME/Library/Mobile Documents/iCloud~md~obsidian/Documents/Life Lessons iCloud" | ||
RUN_EVERY_N_SECONDS=120 | ||
|
||
while true; do | ||
# If Anki.app is not already running, launch it | ||
ANKI_COUNT=$(ps -ax | grep -c "Applications\/Anki.app") | ||
if (($ANKI_COUNT == 0)); | ||
then | ||
echo "🧠 Launching Anki" | ||
open -a Anki | ||
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee" | ||
exec > >(tee -a logs/shell_script.log) | ||
# Redirect stderr into stdout ( 2>&1 ), then redirect stdout into a named pipe running "tee" | ||
exec 2>&1 | ||
|
||
log() { | ||
# Assign the date and time stamp, in "DD:MM-HH:MM:SS" format | ||
TIME_STAMP=$(date +"%d/%m %H:%M:%S") | ||
# Assign the current user | ||
USER=$(whoami) | ||
# Write to the file *and* echo to stdout | ||
mkdir -p logs | ||
echo "$USER - $TIME_STAMP - $1" | ||
} | ||
|
||
alert_if_fails() { | ||
"$@" | ||
if [ $? -ne 0 ]; then | ||
osascript -e 'display alert "Personal mnemonic execution failed!" message "Check in the logs for error messages"' | ||
fi | ||
} | ||
|
||
main() { | ||
while true; do | ||
# If Anki.app is not already running, launch it | ||
ANKI_COUNT=$(ps -ax | grep -c "Applications\/Anki.app") | ||
if (($ANKI_COUNT == 0)); | ||
then | ||
log "🧠 Launching Anki" | ||
open -a Anki | ||
fi | ||
|
||
log "🧠 Updating personal mnemonic medium with scripts in $PROJECT" | ||
cd $PROJECT | ||
source .venv/bin/activate | ||
|
||
echo "🧠 Updating personal mnemonic medium with scripts in $PROJECT" | ||
cd $PROJECT | ||
source .venv/bin/activate | ||
log "🧠 \"$LESSONS_DIR\"" | ||
python application/main.py -r "$LESSONS_DIR" -p "$PROJECT/Life.apkg" | ||
|
||
echo "🧠 \"$LESSONS_DIR\"" | ||
python application/main.py -r "$LESSONS_DIR" -p "$PROJECT/Life.apkg" | ||
# Send "y" keypress to Anki.app to start sync | ||
# osascript -e 'tell application "Anki" to activate' | ||
# osascript -e 'tell application "System Events" to keystroke "y"' | ||
log "🧠 Finished syncing" | ||
sleep $RUN_EVERY_N_SECONDS | ||
done | ||
} | ||
|
||
# Send "y" keypress to Anki.app to start sync | ||
# osascript -e 'tell application "Anki" to activate' | ||
# osascript -e 'tell application "System Events" to keystroke "y"' | ||
echo "🧠 Finished syncing" | ||
sleep $RUN_EVERY_N_SECONDS | ||
done | ||
alert_if_fails main | ||
|