41

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 5

41

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

Source.

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

Source.

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.

Source.

10
  • 2
    Great 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.
    – hcvst
    Jan 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
  • updated the answer
    – q9f
    Jan 22, 2016 at 12:27
  • 3
    Systemd 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.
    – e18r
    Jun 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 ...
    – carlolm
    Sep 29, 2017 at 1:16
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
6
  • 2
    To get a console, geth attach
    – e18r
    Jun 8, 2017 at 18:01
  • 2
    killall geth seems like a better option to stop the geth-process.
    – alper
    Dec 18, 2017 at 19:35
  • that's exactly what i need!
    – Richard Fu
    Sep 7, 2018 at 0:39
  • @alper killall geth doesn't work with nohup
    – e18r
    Mar 25, 2021 at 18:55
  • @e18r try: kill -9 $(pgrep -f -l geth | awk '{print $1}')
    – alper
    Mar 25, 2021 at 21:02
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

2

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
0

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:

  1. 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"'

  1. Run
chmod +x geth-testnet
chkconfig --add geth-testnet
chkconfig --level 345 geth-testnet on
2
  • 1
    I 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
    – babycakes
    Nov 23, 2016 at 6:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.