tracking crontab changes with git
You should never manually touch the files in the cron spool directory. Making changes to the files there would not notify the cron daemon properly, and updates to schedules would not become active until the daemon is restarted. (Some cron daemon implementations may periodically check the spool to see if there are updated files in there, but this behaviour is not universal; check your cron(8)
manual).
The cron(8)
manual on Ubuntu says
Note that crontabs in this directory should not be accessed directly - the
crontab
command should be used to access and update them.
You should always use the crontab
command to load new schedules, or to delete existing ones.
As for keeping crontabs in Git, I see no issue with this, but, as I mentioned above, you should not make /var/spool/cron/crontabs
a checkout of that repository. Instead, let it live elsewhere and manually load crontabs with e.g.
crontab my-crontab.txt
where my-crontab.txt
is a file managed with Git.
This would additionally avoid the issue that you have with the files getting modified by your cron daemon.
You could use a filter:
git config filter.dropSecondLine.clean "sed '2d'"
Edit/create .git/info/attributes and add:
* filter=dropSecondLine
If you don't want the filter acting on all the files in the repo, modify the * to match an appropriate pattern or filename.
The effect will be the working directory will remain the same, but the repo blobs will not have the second line in the files. So if you pull it down elsewhere the second line would not appear (the result of the sed 'd2'). And if you change the second line of your log file you will be able to add it, but not commit it, as the change to the blob happens on add, at which point it will be the same file as the one in the repo.