Drupal - How can execute batch api over cron jobs
You don't. Batch API is to do long-running processing with user-interaction.
For cron, you either use queue API or you just process for N seconds/things in your hook_cron() implementation, and in both cases, cron will be calling you repeatedly until it is eventually done.
If you want to process as fast as possible then either use a different queue processing that doesn't run on cron or make a custom script/drush command that you execute separate from cron, so that you don't block other cron jobs.
Instead of using Cron API you could provide your own custom Drush command and implement Batch API there. And then execute this Drush command using your server's crontab.
Following comes a sample command that implements a batch. Inside module_name.drush.inc
put:
function module_name_drush_command() {
$items = array();
$items['migrate-batch-list'] = array(
'description' => 'Migrate import match',
'examples' => array(
'drush migrate-batch-list' => 'Migrate import match',
),
);
return $items;
}
function drush_module_name_migrate_batch_list() {
drush_print('Migrate import match list.');
$operations = array(...something here you want.);
$batch = array(
'operations' => $operations,
'finished' => 'importingmatch_finishedBatch',
'title' => t('Import match'),
'init_message' => t('Starting import match.....'),
'progress_message' => t('Completed @current step of @total.'),
'error_message' => t('Import match deletion has encountered an error.'),
);
// Initialize the batch.
batch_set($batch);
// Start the batch process.
drush_backend_batch_process();
}
Note: You can run this from terminal as drush migrate-batch-list