This question already has an answer here:

Currently, I am manually running the miner.start() function every time I want to mine in geth. How can I add a trigger in such a way that mining takes place automatically when there are 5 transactions pending?

marked as duplicate by Achala Dissanayake, Richard Horrocks, flygoing, Roman Frolov, Ismael Feb 1 '18 at 3:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Add --preload "mine_on_demand.js" to your geth command. Add this js file to the folder where the geth is located.


function checkWork() {
    if (eth.getBlock("pending").transactions.length > 4) {
        if (eth.mining) return;
        console.log("Pending transactions! Mining...");
    } else {
        console.log("No transactions! Mining stopped.");

eth.filter("latest", function(err, block) { checkWork(); });
eth.filter("pending", function(err, block) { checkWork(); });


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