- Do not tinker with crontab at all for the same kinds of a task: Make crontab execute a script that handles all cases sequentially.
- If necessary, reserve a part of the crontab that can be modified elsewhere, recreate only that very part you actually deal with.
Use separate files, like /etc/cron.d/*.
- Get rid of parsing an existing crontab, re-create it entirely when needed.
Good practice: Always write a configuration file instead of editing an existing one.