How can I run go-ethereum as daemon process on Ubuntu?

  • The Installation Instructions for Ubuntu suggest running geth from the command line, how can I run it as daemon process / service on Ubuntu?

  • Afr

    Afr Correct answer

    5 years ago

    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.

    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.

    You have my vote, but agree that the systemd section is probably the most relevant portion of the answer.

    updated the answer

    Awesome, this is an excellent answer!

    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.

    @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 ...`

    Why do we need to disown? @5chdn

    Wouldn't you want to append to the log in the service file? `2>>` instead of `2>`?

    The `ExecStart` line does not work for me. According to the systemd documentation, "Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported." I also find, contra the systemd documentation, that an absolute path to the binary is needed.

    How to run Geth using its flags in daemon mode with your solution?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM