The Installation Instructions for Ubuntu suggest running geth
from the command line, how can I run it as daemon process / service on Ubuntu?
5 Answers
Run as a systemd service
Create a file geth.service
:
[Unit]
Description=Ethereum go client
[Service]
Type=simple
ExecStart=geth 2>%h/.ethereum/geth.log
[Install]
WantedBy=default.target
Enable service:
systemctl --user enable geth.service
systemctl --user start geth.service
Alternatively you could use screen:
sudo apt-get update && sudo apt-get install screen -y
Then you can make a bash similar to this (~/geth.sh
):
#!/usr/bin/env bash
echo "Starting geth"
screen -dmS geth /usr/bin/geth --verbosity 3
now let's make it executable:
sudo chmod +x ~/geth.sh
You can now run the bash ~/geth.sh
You attach to the screen with screen -x geth
You detach from the screen by pressing CTRL + a then d
If you want to attach to the geth console after the process runs in the background (or in screen), you can use:
geth attach
Or simply fork it in background:
When starting geht, put a &
at the end of the command in a terminal:
geth --rpc &
Before closing the terminal you should disown the process:
disown
You could also pipe the logs to a file like that:
geth --verbosity 4 --rpc 2>> /path/to/logfile
But don't forget to disown it before you close the terminal.
-
2Great answer. The "systemd" should probably be the first example though - at least it's what I would have expected to find as the top answer.– hcvstJan 22, 2016 at 11:26
-
You have my vote, but agree that the systemd section is probably the most relevant portion of the answer. Jan 22, 2016 at 12:01
-
-
3Systemd method: Failed to enable unit: File geth.service: No such file or directory. I'm saving the file in my home folder and I'm executing the command there.– e18rJun 8, 2017 at 17:53
-
5@emisilva I had a similar problem, which I solved by changing: 1) changing the enable command to sudo & making the path an absolute path:
sudo systemctl --user enable /home/ubuntu/geth.service
2) also had to change the command to an absolute path in the service:ExecStart=/usr/bin/geth ...
– carlolmSep 29, 2017 at 1:16
Just using this command is enough for me:
nohup geth --rpc &
To check if service is running:
ps ax | grep geth
To explore last log messages:
tail -f nohup.out
To stop the service I use:
pkill geth
-
2
-
2
-
-
-
Here is an update to 5chdn's answer that uses systemd
. This shows how to run a headless geth
on your Ubuntu server.
Of course, you must install the command line tools first, see https://www.ethereum.org/cli. You should also run MyEtherWallet on your normal desktop computer to create an account + private key so you can receive funds.
Step 1: put on your cape
sudo bash
Other steps here are based on you running as root. If you are not running as root then learn about using systemd with user processes. tl;dr in this case they would only run after you login, but you can configure so a user service starts as boot
Step 2: make a service
cat > /root/geth.service <<EOF
[Unit]
Description=Ethereum go client
[Service]
Type=simple
ExecStart=/usr/bin/geth --etherbase 0xe677698ab732Aa1E56CF2A7997a00e3FA6F5bD88 2>%h/.ethereum/geth.log
[Install]
WantedBy=default.target
EOF
Above, you can substitute your own wallet's etherbase. If you don't specify an etherbase then geth
will emit a warning.
Step 3: run it
systemctl --user enable /root/geth.service
systemctl --user start geth.service
This will run it now and after you reboot.
Step 4: test it
sleep 5 # The geth server takes a few seconds to boot up
geth attach
If geth attach
is successful, you will know the geth
server is running, and you'll see:
Welcome to the Geth JavaScript console!
If something messed up then you'll see:
Fatal: Unable to attach to remote geth: dial unix /root/.ethereum/geth.ipc: connect: connection refused
Best way is to run as systemd:
Create geth.service file (/etc/systemd/system/geth.service):
sudo nano /etc/systemd/system/geth.service
Paste the below
[Unit]
Description=Geth
[Service]
Type=simple
User={$USER}
Restart=always
RestartSec=12
ExecStart=/bin/geth --syncmode "full" --rpc --rpcaddr "0.0.0.0"
[Install]
WantedBy=default.target
Replace {$USER} with username above. Use useradd to create a newUser if required.
sudo useradd -d /home/newUser -m --uid 10000 newUser
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable geth.service
sudo systemctl daemon-reload
service geth status
sudo service geth start
Check the status:
service geth status
Attaching to geth instance:
geth attach
Stop and disable the service:
sudo service geth stop
sudo systemctl disable geth.service
sudo systemctl daemon-reload
Though the accepted answer is great, it didn't work for me. Most likely because I am on AWS Linux AMI (eugh, I know). I intend on switching at some point (systemctl not So far, this seems to have worked for me:
- Create file /etc/init.d/geth-testnet
#!/bin/bash #chkconfig: 2345 20 80 #description Start RPC testnet su ec2-user -c '/path/to/geth --verbosity 3 --testnet --rpc --rpcaddr="0.0.0.0" --rpccorsdomain="*" --rpcapi="db,eth,net,web3,personal"'
- Run
chmod +x geth-testnet chkconfig --add geth-testnet chkconfig --level 345 geth-testnet on
-
1I may be wrong but is this part of the above command: '--rpc --rpcaddr="0.0.0.0" --rpccorsdomain="*" ' opening geth ports to possible attack? I seem to remember some posting five or six months ago about someone losing ether when running geth with ports open to the whole world as this is saying. Might be wrong though. I'm sure someone will correct me if I am. Sep 16, 2016 at 14:43
-
for safety, maybe anyone who finds this remove that part until someone confirms or denies what @ThomasJayRush said Nov 23, 2016 at 6:33