My geth node is located in a remote machine (running on Ubuntu). The safe connection is provided by SSH protocol. Now I am trying use JSON RPC API.
So, the command geth --rpc
returns response like this: HTTP endpoint opened: http://localhost:8545 Listening on [::]:30303 ...
Next I send the first JSON-RPC request. For example, curl -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67} http://localhost:8545'
However my client get the same command from server, without any response.
Why can this behaviour occur?
-
Judging from your answer, your question should be "How to keep geth alive in remote server?"– niksmacNov 28, 2016 at 2:40
2 Answers
Worked for me, just add a single quote after the closing curly brace and the content-type header
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://localhost:8545
Response
{"jsonrpc":"2.0","id":67,"result":"Geth/v1.5.4-unstable-586f10ec/linux/go1.7.1"}
-
Thank for your answer, Herman, but it still returns my inputed command, without any response. Furthermore
history
command shows me that server did not recieve curl request aftergeth --rpc
. Do you know can this behavior pertain to linux-machine (for example, when terminal is disable) or geth? Nov 26, 2016 at 18:07
The thing was a necessity of understanding of Linux shell's work.
When we input some command like $ geth --rpc
the shell looks up needed program (geth) and creates a child process for it. Then it wait untill geth terminates its working.
Therefore if now we trying to send some command to shell or even geth they do not return needed response since shell is waiting for geth and geth is listening port to work with rpc protocol.
There is the solution. We just run geth as background process:
$ geth --rpc &
(with & symbol in the end)
Well, it works, shell in not blocking, geth is listening port 8545, however if we send some command to shell after this (for example, for curl) it automatically stops geth process. This behavior is set by default and occurs because the standard input of background process is associated with controlling terminal (window) still, it gets SIGTTIN
signal when user inputs a command to there, which by default causes the program to be stopped.
So, we need to disable getting input stream from window:
geth --rpc </dev/null >&1 &
where < is input stream (it is associated with /dev/null, i.e. empty file which discards all written data) and > is output stream (it is associated with "&1" i.e. standard output stream, or window).
That's all. Now we can input some command to curl which then sends needed request for geth JSON RPC and returns response:
$ curl -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://localhost:8545
{"jsonrpc":"2.0","id":67,"result":"Geth/v1.5.0-unstable/linux/go1.5.1"}
edit: according another part of my question, it confused me that inputted command is sending back. Actually it is normal fact, shell displays all strings which being treated. To disable it:
stty -echo