Running geth --exec starts an interpreter. It allows to run geth console commands. I want to load a javascript function that runs continuously in background in geth. It basically continuously looks for pending transactions and if found, it mines them else doesn't mine continuously (empty blocks unnecessarily).

Currently, I have to manually mine every time and stop mining.

geth --jspath "/root/files/js" --exec 'eth.mining;loadScript("mine_pending_transactions.js");eth.mining;' --datadir=/root/.ethereum/devchain attach

However, I don't want to mine this manually. In order to automate this, I want to load the script and mine it, as if it's a something like background command that runs in the geth console interpreter.

but I learnt from this Stack-overflow post - (Executing custom javascript snippets through non-interactive console) that one needs to access the same interpreter.

Is there a way to do that? Even in case of non-interactive console? Couldn't find anything on official Ethereum documentation.

  • Did you consider to use geth <all you option> js JS_SCRIPT_FILE?. In the help message it is reported js Execute the specified JavaScript files . You can write a Javascript file that run continuosly in background. I use a javascript file to create continuosly new transactions every x milliseconds. So you can securely achieve what you are looking for.
    – Briomkez
    Commented Jul 25, 2018 at 21:33
  • What's <all you option> ? I'm sorry I didn't get that. Commented Jul 25, 2018 at 21:34
  • In your case you need only geth js javascript_file but in general you can also specify some options, such as datadir etc.
    – Briomkez
    Commented Jul 25, 2018 at 21:36
  • Mine is a private geth version. Probably that's why when i run geth js mine_pending_transactions.js as you mentioned, I get an error - listen udp :30303: bind: address already in use. Hence I tried to mention the datadir where my existing ethereum is in place geth --jspath "/root/files/js" --datadir=/root/.ethereum/devchain js mine_pending_transactions.js . But now it returns datadir already used by another process Commented Jul 25, 2018 at 21:45
  • This problem is related to the fact that there exist already a geth instance that is using the datadir or the port. You have either to stop the existing instance or to start the instance in another datadir AND with a different port.
    – Briomkez
    Commented Jul 25, 2018 at 21:47


Your Answer

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

Browse other questions tagged or ask your own question.