For scheduling regular tasks crontab is a general solution. In your case the logout session should be the trigger. Here lightdm seems best I would say.
You can make a bash script stating your commandline sync and insert it into the /etc/lightdm/lightdm.conf file by:
session-cleanup-script=/path/to/script
For further details another answer already exists on:
Execute a script upon logout/reboot/shutdown in Ubuntu
In case you wish to sync also at regular time intervals you can also use crontab in parallel to run your bash script. Here you need to be aware that contab also runs when you are logged out which is not needed.
here you could check the username (your name) if logged in before starting the grep command:
if whoami | grep -q {yourname}; then {your grep command} ; fi;