Creating a systemd service for Chia - "failed to start"

Hey all! I run a linux server and every service on the machine so far is run through a systemd script.

I tried doing one for Chia but it is NOT working out. I think it has to do with the whole venv thing.

I’ve Googled the topic and even the solutions pitched in these topics.

The two most common issues I run into:

  1. The services keep restarting. Watching my console output, I see all my Chia services (wallet, node, etc.) come online, immediately followed by another message saying they are all coming online. This restarts forever AFAIK.

  2. When trying to write a systemd script that directly uses the chia binary in git-repo/venv/bin/chia, I would typically see “failed to start” next to every service I was trying to start.

Has anyone gotten this working?

They also offer a docker container, that might be easier to setup to always run?

2 Likes

Curious if you got this working?

If not, you may need to post your script to get more help. You can start chia multiple ways, I have been using the chia start farmer version, which starts the required daemons and then exits. This requires setting the correct type in systemd service. Assuming you’ve got that figured out, I’ve setup many python virutalenvs, and your strategy of using the explicit binary (err script) will work. Something like /home/me/chia/venv/bin/chia farmer start will execute with the correct python libraries.

My suggestions:

  • Check the user you are starting the service as. Chia expects a ~/.chia and ~/.local to exist and to contain the chia db state and your signing keys. If those do not exist for the user that is starting the process, chia will just bomb saying it is not initialized.
  • Turn on file logging to get output, there might be more info in there.
1 Like

I have systemd unit that starts a “master” script. That master script launches a bunch of things, chia is one of them.

The unit is like this:

[]$ cat /etc/systemd/system/script-starter.service
[Unit]
Description= script starter
After=remote-fs.target network-online.target
Wants=network-online.target

[Service]
Type=forking

ExecStart=/home/'your username'/scripts/starter.sh
User='your username'
Group='your group'


[Install]
WantedBy=multi-user.target

The starter script is like this:

[]$ cat ~/scripts/starter.sh
#!/bin/bash

#.... other stuff I start..
screen  -dm -S chia bash -c "/home/'your username'/scripts/chia.sh; exec bash"

As you can see, I start chia in a screen so I can go there and check farming, logs…
“screen -rd chia” takes you to the screen. [Ctrl+A D] detaches you from the screen.
Just remember that the first time you attach to that screen you have to go through “. activate” to be able to issue chia commands.

And finally, the chia script is like this:

[]$ cat ~/scripts/chia.sh
#!/bin/bash

cd ~/chia-blockchain/
. activate
chia start farmer

Hope it helps.

3 Likes

Wow, great stuff, I’ll actually try this out tomorrow. A few initial thoughts:

  • I wouldn’t use screen here, that may be causing the issue. You probably don’t want your logs going to stdout, services typically write to a logfile or the syslog, and chia will do this for you. Update your chia config.yaml to make sure it’s sending logs to a file, and you should get them at ~/.chia/mainnet/logs.
  • With that said, I think the starter.sh script is all you need, something like this should do it:
#!/bin/bash

# this should point to the chia script in the bin directory of your virtual env
/home/abc/chia-blockchain/venv/bin/chia start farmer

Sourcing the activate virtual env script is normally just to get your shell setup, it’s not necessary if you are just starting a python program (I’ve been coding python on and off for ~10 years).

2 Likes

It’s working now! I think cascooscuro’s “Type=forking” is the thing that got it working locally.

Thank you!