Skip to content

Instantly share code, notes, and snippets.

@abhijit86k
Last active August 29, 2015 14:01
Show Gist options
  • Save abhijit86k/46ef48432650a9a60bcf to your computer and use it in GitHub Desktop.
Save abhijit86k/46ef48432650a9a60bcf to your computer and use it in GitHub Desktop.
Automatic Stock Quote fetch using gnucash and Perl F::Q
#!/bin/bash
#Script to fetch price updates for all stocks listed in Gnucash Accounts
#If you are behind a proxy you'll need to export the proxy name too:
#export http_proxy="http://proxyname.domain.com:PORT";
/usr/local/bin/gnucash --add-price-quotes /path/to/gnucash-accounts/file.gnucash
exit
#!/bin/bash
#Script to fetch price updates for all stocks listed in Gnucash Accounts
#Export proxy if you are behind one:
#export http_proxy="http://proxyname.domain.com:PORT";
if [ $# = 0 ]
then
#echo "Error: Missing filename";
#exit 1;
echo " "
fi
#Check if Gnucash is running by looking for the presence of a lockfile
if [ -e /path/to/gnucash-accounts/file.gnucash.LCK ]
then
echo "Target file locked by another instance of gnucash";
exit 1;
fi
echo ""
DATE=`date`;
echo "$DATE: Attempting to fetch updates for $1";
echo "Warning: Target file name is hardcoded for now";
#Run the price fetch
#gnucash --debug --add-price-quotes="$1"
env `dbus-launch` sh -c 'trap "kill $DBUS_SESSION_BUS_PID" EXIT; /home/username/bin/gnucashgetquotes'
EXITSTAT=$?
echo Status:$EXITSTAT
exit $EXITSTATUS
Read this carefully and completely before you run the commands!
1. First, tell crontab to run the update script periodically. Run crontab -e as user (not root!) and add this line: (replace your username, naturally)
0 9,11,13,15 * * 1,2,3,4,5 /home/username/bin/gnucashstockupdate /path/to/gnucash-accounts/file.gnucash >> /path/to/gnucash-accounts/file/PriceUpdate.log
2. Copy the accompanying script gnucashstockupdate to ~/bin and make it executable
3. Copy the accompanying script gnucashgetquotes to ~/bin and make it executable
4. Done! The crontab entry I have given will attempt to fetch updates on all weekdays at 9, 11, 1, 3 o'clock - which fits the tmings of our exchange. For international quotes change as desired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment