I'm running a webserver in a VM.
In order to create consistent backups, I have to stop Apache, MySQL and other processes for a short time before doing a snapshot of the VM's harddrive (LVM-based).
However, there might be some cronjobs currently running on the VM that need access to the MySQL database. I don't want to interrupt them.
Therefore, I'm looking for a way to determine whether there are currently any cronjobs running. When it's time for a backup, I will wait until I don't "disturb" any cronjob.
Any ideas how to accomplish this?
What you really need to do is use a locking system because anything that checks for the existence/non-existence of something leaves a window of opportunity for a process to launch and create the inconsistent state you're tryiung to avoid.
Linux provides the flock command there is an example of using it here.