1

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?

David Scherfgen
  • 265
  • 2
  • 6

2 Answers2

2

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.

user9517
  • 114,104
  • 20
  • 206
  • 289
1
if [ $(ps h --ppid `pidof crond` | wc -l) -eq 0 ]; then 
   /run/your/backup
else 
    echo "There are some still running cron jobs."
fi
quanta
  • 50,327
  • 19
  • 152
  • 213
  • I guess that's better than "pgrep cron", because the latter would also find any processes that happen to have "cron" in their name, right? – David Scherfgen Mar 08 '13 at 08:36
  • Yes, you're right. `pgrep, pkill - look up or signal processes based on name and other attributes`. – quanta Mar 08 '13 at 08:39