I am running a script to watch my events as follows:
run.js:
var fs = require('fs');
Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
address="0x..."
abi=[...]
var myContractInstance = web3.eth.contract(abi).at(address);
blkNum = 1000;
var event = myContractInstance.LogJob({}, { fromBlock: blkNum, toBlock: 'latest' });
event.watch( function (error, result) {
fs.appendFile( "/tmp/test.txt", JSON.stringify(result) + '\n', function(err) {
process.exit();
});
});
After running node run.js
or nohup run.js &
, we could find the output as follows:
cat /tmp/test.txt
Please note that when a new Event
feed in running watch()
terminates itself after reading the fed Events
.
But when I do: sudo killall geth
even geth
is not working on the background, watch() continues to watch any event would come or not. It does not return any error or anything.
[Q] Is there any way to trigger watch()
function to terminate itself when running geth
or Parity
on the background is killed? We could do cntl-c
but I want watch() to kill itself automatically.