I have some CPU intensive scripts that I don't want to run concurrently. Every now and again the scripts will overlap and crash the server. Already at 2GB and the mysql container still crashes.

I have a crontab setup to hit example.com/cron.php?do_cron=randstr every 15 minutes.

Is there any way to place a lock and only execute one cron task at a time? When it's done, the next one would then run.

I know this can be accomplished using PHP's file locking mechanism (but then it doesn't queue the next task). Not sure if WP does this on its own.

All help appreciated!

1 Answer 1


Simple Cronjob Walker

  1. Create a CPT cronjob for your cronjobs.
  2. Add the relevant post meta data (action, last_executed,… and whatever you need)
  3. Create a walker function

    function my_cronjob_walker() {
        $args= array(
            'post_type' => 'cronjob',
            'posts_per_page' => 1,
            'meta_key' => 'last_executed'
            'orderby' => 'meta_value',
        $cronjob = new WP_Query($args);
        if ($cronjob->have_posts()) while ($cronjob->have_posts()) {
            // do your cronjob stuff here
            do_action(get_post_meta(get_the_ID(), 'action', true);
            update_post_meta(get_the_ID(), 'last_executed', date('c'));
    if ( ! wp_next_scheduled( 'do_cronjob_walker' ) ) {
        wp_schedule_event( time(), 'hourly', 'do_cronjob_walker' );
    add_action( 'do_cronjob_walker', 'my_cronjob_walker' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.