How to activate Chia venv from a bash script?

I’m working on a bash script to make it easier for me to monitor my (small but growing) army of plotting machines and farmer.

I’m looking for help with one thing that I can’t figure out - how do I activate the Chia venv from a bash script?

It will output the following to a text file and can be run every 15 mins as a cross job, sending the output via email or some other method:

  • chia farm summary
  • plotman status
  • free -m
  • df
  • the last five ERROR / WARN lines from chia debug.log
  • the last five plot check lines from chia debug.log

I’ll happily share the script when I’m done :slight_smile:

cd ~/chia-blockchain && . ./activate

That worked for me in a simple script I wrote.

1 Like

Hrmm, interesting. That’s not working for me on Ubuntu. I’ll copy the script below in case anyone has any ideas :slight_smile:

!/bin/bash

cd ~/chia-blockchain && . ./activate

“chia farm summary” >> /home/alex/monitor.log

chia farm summary >> /home/alex/monitor.log

“plotman status” >> /home/alex/monitor.log

plotman status >> /home/alex/monitor.log

“memory” >> /home/alex/monitor.log

free -m

“hdd space” >> /home/alex/monitor.log

df

exit

Did you forget the # at the start of !/bin/bash?

(Also did you know # is also called an octothorpe?)

I’m using Ubuntu server and cd ~/chia-blockchain && . ./activate works for me.

Why don’t you do a unit test.

#!/bin/bash
cd ~/chia-blockchain . ./activate
chia -h >> ~/test.txt

See if that works?

3 Likes

Thanks @roybot, that worked! Here’s how the script came out:

#!/bin/bash

rm ~/monitor_output.html

cd ~/chia-blockchain && . ./activate

cd ~/

echo "<html><body>" >> monitor_output.html

echo "<h3>chia farm summary</h3><pre>" >> monitor_output.html

chia farm summary | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "<h3>plotman status</h3><pre>" >> monitor_output.html

plotman status | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "<h3>free memory</h3><pre>" >> monitor_output.html

free -h | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "<h3>df</h3><pre>" >> monitor_output.html

df | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "<h3>mpstat</h3><pre>" >> monitor_output.html

mpstat | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "<h3>chia debug log</h3><pre>" >> monitor_output.html

tail -n 15 ~/.chia/mainnet/log/debug.log | grep -e 'WARN' -e '1 plots' -e '192.168' -e'ERROR' | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "<h3>chia & plotman processes running</h3><pre>" >> monitor_output.html

ps -a | grep -e 'plotman' -e 'chia' | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "<h3>syslog</h3><pre>" >> monitor_output.html

tail -n 15 /var/log/syslog | cut -c-$(tput cols) >> monitor_output.html

echo "</pre>" >> monitor_output.html

echo "</body></html>" >> monitor_output.html

mail -a 'Content-Type: text/html' -s "chia update : $(hostname) : $(date +%F) $(date +%T)" email@domain.com < monitor_output.html

exit
3 Likes

While I’ve got some Linux gurus here… does anyone have an idea why running this in a cron job would result in this writing the headings to the html, but not writing any of the commands ie “tail -n 15 /var/log/syslog”?

I’ve been tearing my hair out for about 2 hours trying to figure this out :slight_smile:

I’ve been running this script manually on all my machines in the meantime and having this info in my mailbox once an hour is really awesome. It means I spend 2 minutes an hour thinking about Chia rather than 58 minutes an hour thinking about Chia and 2 minutes an hour making more coffee :rofl:

I’m not expert, but only outputs would get written. The commands themselves are not outputs, right?

Thanks Roy, I figured it out: just needed an “export TERM=xterm” at the start :slight_smile:

1 Like

I’m trying to write a very basic script that runs chia plotnft show and emails it to me.

chiascripts.sh

#!/bin/bash

echo -e "running chia plotnft show"

cd ~/chia-blockchain && . ./activate

chia plotnft show

exit

I then update my crontab with the following

sudo crontab -e

* * * * * bash /home/<username>/scripts/chiascripts.sh 2>&1 | mail -s "Chia Plot Info" <my email address>

But when I get the email it has this output:

running chia plotnft show
/home/<username>/scripts/chiascripts.sh: line 5: cd: /root/chia-blockchain: No such file or directory
/home/<username>/scripts/chiascripts.sh: line 7: chia: command not found

Any linux gurus who can tell me where I’ve gone wrong?

Cron is running as root, it’s looking for chia-blockchain in the current user’s home directory (~). You will need to use an absolute path in your script, e.g. cd /home/<user>/chia-blockchain && . ./activate. Alternatively don’t run the cron job as root.

What I have done for total convenience is add this script to /usr/local/bin (you’ll obviously have to change the path to chia-blockchain if you didn’t install into /opt):

$ cat /usr/local/bin/chia
#!/bin/bash

cd /opt/chia-blockchain
source activate
exec chia "$@"

then run:

sudo chmod +x /usr/local/bin/chia

now from anywhere else you can run:

chia <whatever>
1 Like

That is awesome, that also means I don’t have to run . ./activate from chia-blockchain now, I like that a lot! Thanks!

So I added that script to

/usr/local/bin

-rwxr-xr-x  1 root root   74 Aug 25 10:24 chia
#!/bin/bash

cd /home/<my username>/chia-blockchain
source activate
exec chia "$@"

And I can run chia without doing

. ./activate from within chia-blockchain

However, my script in sudo crontab -e is now blank, instead of getting an error when it runs.

I have chia installed here:

/home/<my username>/chia-blockchain

sudo crontab -e

* * * * * /home/<my username>/scripts/chiascripts.sh
#!/bin/bash
chia plotnft show | grep -E 'Sync status:|Number of plots:|Points balance:|Percent Successful Points \(24h\):' | mail -s "Chia Info" <my email address>

exit

But when I run it as or from my users crontab it works as expected.

$ bash chiascripts.sh

Are you on a systemd linux? If so check the output of:

journalctl --unit cron

I’m on Ubuntu 21.04

When I run journalctl --unit cron I get a lot of output so I won’t paste it all here. I have a cron job that writes data to each of my HDD every 5 minutes to prevent sleeping. Is there something specific I should be looking for?