Use lockfile to avoid a race condition between writing and reading state file