cazala / coin-hive

CoinHive cryptocurrency miner for node.js
https://www.npmjs.com/package/coin-hive
MIT License
1.98k stars 397 forks source link

Running miner as invisible service #51

Closed Blogshot closed 6 years ago

Blogshot commented 6 years ago

Hello,

I'm trying to run this as part of a headless Debian setup. This requires to run this without any console interfaces or the like.

My "setup" is one bash script that invokes the miner:

#!/bin/bash

cd /root/coinhive/ && coin-hive <SITE_KEY> --threads 1 > miner.log

If I run the miner from command line it works fine. However, as soon as I try to start it as a service or background process, the miner terminates.

So far, I've tried:

1) nohup /root/coinhive/miner & Resulting in

TypeError: process.stdin.setRawMode is not a function
    at Object.<anonymous> (/usr/lib/node_modules/coin-hive/bin/coin-hive:182:15)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

2)

/root/coinhive/miner &
bg
disown

Resulting in: Process starts in background, but halts/terminates as soon as I hit disown.

3) Crontab: @reboot /root/coinhive/miner & Resulting in: Miner not starting at all.

4) Ctrl + Z doesn't respond at all.

It seems to me that for the miner to work, there has to be some sort of terminal or window active. Please advise.

sunk818 commented 6 years ago

you can try screen command and detach once you have it running as you want. screen command

ctrl+a then d

https://stackoverflow.com/questions/4847691/how-do-i-get-out-of-a-screen-without-typing-exit

cazala commented 6 years ago

You could use the miner programatically instead of using the CLI. That process.stdin.setRawMode is something from the CLI code, but that shouldn't happen if you use it like this

Blogshot commented 6 years ago

@cazala Thanks, I made a custom js-Script that can be invoked via node command. Seems to be working!

cazala commented 6 years ago

@Blogshot just to let you know, this has been fixed in version v1.9.1, so now you should be able to use the CLI without that process.stdin.setRawMode error

mariomixtegapach commented 6 years ago

@Blogshot Could you share the bash you're using please? :smile:

Blogshot commented 6 years ago

@mariomixtegapach miner

 #!/bin/bash

cd /root/coinhive/ && node miner.js > miner.log 2> miner.err &

echo $! > /root/coinhive/miner.pid

miner.js

(async () => {

  var fs = require('fs');
  fs.writeFile('./miner.pid', process.pid);

  // Create miner
  const miner = await CoinHive('R5hcEcpv1gWEYZ0CzKy8wuC3W6MczjVh'); // CoinHive's Site Key

  // Start miner
  await miner.start();

  // Listen on events
  miner.on('found', () => {
    var date = new Date();

    console.log("[" + date.toLocaleDateString() + " " + date.toLocaleTimeString() + "] Found!");
  });
  miner.on('accepted', () => {

    var date = new Date();

    console.log("[" + date.toLocaleDateString() + " " + date.toLocaleTimeString() + "] Accepted!");
  });

  miner.on('update', data => {

    var date = new Date();

    console.log(
      "[" + date.toLocaleDateString() + " " + date.toLocaleTimeString() + "]" +
      `
      Hashes per second: ${data.hashesPerSecond}
      Total hashes: ${data.totalHashes}
      Accepted hashes: ${data.acceptedHashes}
    `);
  });

})();