Automate setting all nodes to UP state when stopping farmerbot

I’ve seen a few people run into issues when stopping farmerbot, specifically when the on-chain power target of their nodes is set to DOWN. This should address the issue.


This script is to be used in conjunction with the service modification explained below, effectively automating the switch to “UP” state anytime the farmerbot service is stopped using sudo systemctl stop {farmerbot}

In all instances where you see {farmID} or {farmerbot} - replace these with your actual farm ID and farmerbot service name.

First create the wake script in the directory containing your farmerbot config.yaml file (this location is strictly necessary due to the requirement of the .env which is in the same directory):

cd ~/path/to/farmerbot
sudo nano wakeall{farmID}.sh

Add the following, then save (ctrl+o, enter) and exit (ctrl+x):

#!/bin/bash

# Change to the directory where farmerbot and .env are located
cd /path/to/farmerbot/

# Run the farmerbot wake all command
farmerbot start all -e .env --farm {farmID}

Once the script is created and assuming you’re still in the directory where the script is located, make it executable:

sudo chmod +x wakeall{farmID}.sh

Next we need to add a line to the service override.conf

sudo nano /etc/systemd/system/{farmerbot}.service.d/override.conf

Add:

[Service]
ExecStopPost=/bin/bash /path/to/farmerbot/wakeall{farmID}.sh

Then reload the daemon

sudo systemctl daemon-reload

That’s it. Now when we run sudo systemctl stop {farmerbot} the service will be stopped and all nodes under farmerbot control will have their on-chain power target set to “UP”. Remember to switch the {placeholders} for the actual farm ID and farmerbot service name.

1 Like

That is very nice! Thanks for sharing this work.

1 Like

Cool idea, and thanks for sharing it!

One minor note: if your script is chmod'd executable and has a proper shebang, as shown in the post, you can just call it directly. Like this:

ExecStopPost=/path/to/farmerbot/wakeall{farmID}.sh

On the flip side, if you do call bash rather than the script directly, then chmod and the shebang aren’t needed.

The steps shown will totally work—there’s just a little something extra :slight_smile:

1 Like

I initially had the ExecStopPost= line as you wrote there but when I ran the systemctl stop command it threw an error. The easiest fix was to add bash to that line but yes, making it executable is somewhat redundant. Belt and braces I guess.

1 Like

Rather than create a new topic for this, I’ll add it here as a reply. A farmer asked for a way to:

a. trigger awaking of all nodes within a farm when the farmerbot host first boots, and
b. enforce a delay on farmerbot service to ensure all nodes are “UP” before farmerbot gains control of the farm.

We’ll attack this one by by one.

If you followed the guide in the OP you’ll already have this script, otherwise we need to first create the wake script in the directory containing your farmerbot config.yaml file (this location is strictly necessary due to the requirement of the .env which is in the same directory):

cd ~/path/to/farmerbot
sudo nano wakeall{farmID}.sh

Add the following, then save (ctrl+o, enter) and exit (ctrl+x):

#!/bin/bash

# Change to the directory where farmerbot and .env are located
cd /path/to/farmerbot/

# Run the farmerbot wake all command
farmerbot start all -e .env --farm {farmID}

Next create a systemd service file for your script. Let’s call it wakeall{farmID}.service:

sudo nano /etc/systemd/system/wakeall{farmID}.service

Copy/paste the following ensuring you adjust path and script name:

[Unit]
Description=Wake All Nodes Script
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash /path/to/script/wakeall{farmID}.sh
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

Then save (ctrl+o, enter) and exit (ctrl+x).

Reload systemd, enable, and start the service:

sudo systemctl daemon-reload
sudo systemctl enable wakeallnodes.service

This service will run once at boot after the network is available, bringing all nodes online.

Now we can add a timer to run {farmerbot}.service after all nodes are awake. There are a couple of ways to do this but using a {farmerbot}.timer is preferable to adding a sleep delay to the actual service. First we need to disable autostart on boot:

sudo systemctl disable {farmerbot}

Then create the timer file with:

sudo nano /etc/systemd/system/{farmerbot}.timer

Copy/paste the following, adjusting as necessary:

[Unit]
Description=Start ThreeFold Farmerbot with a delay

[Timer]
OnBootSec=5min

[Install]
WantedBy=timers.target

Reload systemd:

sudo systemctl daemon-reload

Enable the timer:

sudo systemctl enable {farmerbot}.timer

With this the {farmerbot}.service will start 5 minutes after the system has booted, as dictated by the farmerbot.timer. The service itself does not need to be enabled directly because the timer will handle starting it. The timer automatically associates with the service of the same name ( farmerbot.timer for farmerbot.service ) and triggers it according to the timer’s schedule. You can check the status of the timer with systemctl list-timers to see when it’s scheduled to activate the service next. It’s important to ensure that the service is not automatically started by another means so don’t skip over ‘sudo systemctl disable {farmerbot}’.