Skip to main content

Stagger cron jobs as root

Comments

4 comments

  • vanessa
    First of all, don't put it in /etc/crontab - use /var/spool/cron/root or dump it into /etc/cron.d/ folder. You probably don't want to manage the parallelism issue with crond itself - you should put these commands into a script and have the script create a lock file to prevent it from running the same command when another is in progress. This way the script may be executed while another is running, but won't actually do anything. Here's an example, not saying it's the prettiest one out there: #!/bin/bash LOCKFILE=/var/run/myrsync.lock [ -e $LOCKFILE ] && exit 1 # If lock exists, exit touch $LOCKFILE rsync -aupog -e 'ssh -p 1234' root@123.45.67.89:/home/old/public_html/dir1/ /home/new/public_html/dir1/ rm -f $LOCKFILE
    0
  • GoWilkes
    Good plan, thanks Vanessa. Should I still use sleep 30 after each rm command? I'm concerned that sleep might still tie up the CPU (which is exactly what I'm trying to avoid), but I also need to give a break for a few seconds between each rsync to let the CPU load go down.
    0
  • GoWilkes
    While we're on the subject... any suggestion on putting an end date on the cron, so that it doesn't run past the end of the month? I know that I can remove it manually, I'm just wondering if there's a way to make it stop automatically.
    0
  • cPanelMichael
    [quote="GoWilkes, post: 1597331">While we're on the subject... any suggestion on putting an end date on the cron, so that it doesn't run past the end of the month? I know that I can remove it manually, I'm just wondering if there's a way to make it stop automatically.
    You could create an additional cron job that removes the existing script or cron job on a certain day of the month. Thank you.
    0

Please sign in to leave a comment.