Running Chia as a service has several benefits, the main one being this always ensures your node / farm is running. Example given: When your farm reboots unexpectedly the service will auto start your farm after boot. You can also use this to autostart only the node or harvester (change the startup script accordingly).
Create the Chia service: sudo nano /etc/systemd/system/[email protected]
and paste the following service:
Note: Replace {USERNAME} (2x) with the name of the user having Chia installed.
[Unit] Description=Chia Service [Service] Type=forking User={USERNAME} WorkingDirectory=/home/{USERNAME}/chia-blockchain ExecStart=/usr/bin/bash -c ". ./activate && chia start %i && deactivate" ExecStop=/usr/bin/bash -c ". ./activate && chia stop -d all && deactivate" Restart=always [Install] WantedBy=multi-user.target
Reloading the systemd manager is needed to work with our new service.
sudo systemctl daemon-reload
The service can take an argument after the '@'. Use this argument to start the required chia service.
For example, to enable the farmer:
sudo systemctl enable --now chia@farmer
Running sudo systemctl status chia@farmer
will now give the following output:
$ systemctl status [email protected] ● [email protected] - Chia Service Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled) Active: active (running) since Fri 2021-12-10 21:40:53 CET; 21s ago Process: 13500 ExecStart=/usr/bin/bash -c . ./activate && chia start farmer && deactivate (code=exited, status=0/SUCCESS) Main PID: 13514 (chia_daemon) Tasks: 17 (limit: 9278) Memory: 1.4G CGroup: /system.slice/system-chia.slice/[email protected] ├─13514 chia_daemon ├─13520 chia_harvester ├─13521 chia_farmer ├─13522 chia_full_node └─13523 chia_wallet Dec 10 21:40:47 chia systemd[1]: Starting Chia Service... Dec 10 21:40:53 chia bash[13501]: Daemon not started yet Dec 10 21:40:53 chia bash[13501]: Starting daemon Dec 10 21:40:53 chia bash[13501]: chia_harvester: started Dec 10 21:40:53 chia bash[13501]: chia_farmer: started Dec 10 21:40:53 chia bash[13501]: chia_full_node: started Dec 10 21:40:53 chia bash[13501]: chia_wallet: started Dec 10 21:40:53 chia systemd[1]: Started Chia Service.
It should say “Active: active” and “enabled” in the Loaded section.
From now on, when you reboot the system, Chia should automatically start up.