In the realm of digital note-taking, Obsidian stands out as a powerful tool for documentation and crafting your personal second brain. There are a lot of other tools, like Notion, but Obsidian takes the crown for me as it safes all files locally in Markdown. However, for a meticulous user like myself, ensuring that every note is safely committed and pushed to my remote Git repository is a crucial backup strategy, which also allows me to compare revisions of notes. In this post, I explain how I created an automated script execution on a program exit for Obsidian.
Obsidian and Cronjobs
Obsidian has become my go-to tool for organizing thoughts, ideas, and information. Its dynamic linking and markdown-based approach make it a standout choice for creating a second brain. To elevate my Obsidian experience, I established a routine of regular Git commits and pushes to keep my vault synchronized and secure. The idea to use a Git as a tool to backup and version your vault is something I got from Bryan Jenks.
Previously, I relied on a simple cronjob as Bryan has suggested to back up my Obsidian vault with periodic Git commits every 30 minutes. While effective in most cases, there were instances where I shut down my computer before the cronjob triggered, leaving some crucial notes unpushed.
The Windows Audit Log
To achieve our goal of triggering script execution upon program exit, this blog post builds upon an insightful internet post (available here). This original resource presents a method for running a script when an application opens or closes by enabling audit logging in Windows, generating events for process creation and destruction. The steps described in the linked post above are the following:
Run gpedit.msc
Go to Computer Configuration -> Windows Settings -> Security Settings -> Local Policies -> Audit Policy;
Open Audit process tracking properties and enable it for Success events;
Run gpupdate /force
to update the group policies.
Crafting Your Custom XML Trigger
Unfortunately the described usage of Powershell to retrieve the events did not work for me, although I was able to see them when filtering manually in the Windows event viewer. In the task scheduler of Windows create a new task as you would and create a new trigger. I suggest using the wizard for the initial creation of the XML, as my pre-generated XML differs from the one on the blog post. Fill these values on the trigger of a new
Event logs: Security
Event ID: 4688
Keywords: Audit Success
To tailor the XML trigger for program exit instead of process creation, a couple of straightforward modifications are needed. First, update the EventID
from 4688 to 4689, signaling the event associated with process destruction or program exit. Additionally, ensure that the @Name
field in the EventData
section is adjusted to “ProcessName” instead of “NewProcessName.” These tweaks effectively transform the original XML trigger, originally designed for process creation, into a a trigger for the closure of a program, making it a perfect fit for automating Git commits and pushes when exiting Obsidian.
<QueryList> <Query Id="0" Path="Security"> <Select Path="Security"> *[System[band(Keywords,9007199254740992) and (EventID=4689)]] and *[EventData[Data[@Name='ProcessName'] and (Data='C:\Users\Tobey\AppData\Local\Obsidian\Obsidian.exe')]] </Select> </Query> </QueryList>
This post focuses on the trigger aspect of a Windows task to run a script. If you’re interested in the script itself which I use to create backups of my Obsidian vault using Git, please leave a comment and I will get back to you. The wrapper I have designed is run with Python, which I’ve explained in a previous post.