Using monit to manage bots

in ruby •  7 years ago  (edited)

You can use monit as a utility for managing and monitoring bots so you don't have to keep your shell session open.

I have gone over this before with steemd, so if you want some more context, check out: Using monit to manage steemd

These steps assume you're using ruby scripts, but it should be possible to use other programming languages. These steps also assume you're using rvm to manage ruby.

Install Monit

$ sudo apt-get install monit​

Create file /home/gilligan/mybot/start_mybot.sh

#!/bin/bash

cd $HOME/mybot

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
$HOME/.rvm/scripts/rvm use ruby-2.4.2

nohup $HOME/.rvm/rubies/ruby-2.4.2/bin/ruby mybot.rb > mybot.log 2>&1 & echo $! > $HOME/mybot/nohup_mybot.pid

Note, we're using nohup to get the pid. But you can also configure monit to work without it by looking for your bot using a matching pattern.

Create file /home/gilligan/mybot/stop_mybot.sh

#!/bin/bash

cd $HOME/mybot

/bin/kill `/bin/cat $HOME/mybot/nohup_mybot.pid`

Make them executable

$ chmod +x /home/gilligan/mybot/start_mybot.sh​
$ chmod +x /home/gilligan/mybot/stop_mybot.sh​

Edit the file /etc/monit/monitrc

$ sudo nano /etc/monit/monitrc​

Edit the file as follows:

# uncomment these lines
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
# add this to bottom - change gilligan to your username
check process mybot with pidfile /home/gilligan/mybot/nohup_mybot.pid
start program = "/bin/su - gilligan -c /home/gilligan/mybot/start_mybot.sh"
  with timeout 60 seconds
stop program = "/bin/su - gilligan -c /home/gilligan/mybot/stop_mybot.sh"​
  with timeout 60 seconds

Test the new configuration

$ sudo monit -t

If it looks ok, then proceed to ...

Load the new configuration

$ sudo monit reload​

Enable the watchdog

$ sudo monit start mybot

That's it. You only have to do the above command once.

You can check monit's status by typing below:

$ sudo monit status​

You can also check status from the web console from addresses you authorize.

http://localhost:2812/

This will keep your bot running for you (across restarts, even, no need for any cronjobs or multiplexors) and keep you from fighting with your chosen OS. Keep in mind, the default is for monit to only once a minute, so be patient if you're waiting for it to do something.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  7 years ago (edited)

Very useful...

... concept. Bookmarked for playing...

I am baby material when it comes to programming/scripting, but I got your drdoogie to work like charm. Planning a post on my drdoogie implementation/experience.

For all those who don't know what that is, drdoogie is an exceptionally easy to configure voting bot, which allows to trail accounts and upvote. Easy, reliable, lovely.

Love your work.


Here is @inertia's post on drdoogie.
https://steemit.com/radiator/@inertia/drdoogie-rb-vote-trail-bot-update

VERY important! like a profile manager! i wish there was a standalone program for windows or macosx that i could run and like vessel insert my keys, and then access my account and it would be nice jUST for making posts , even if it just had the posting key, just even a chrome extension for just making posts etc

ANYWAY I upvoted and got @tytran to upvote and this is a great solution, youre a great developer, and I am resteeming because i feel this is very important and I want to be seen as supporting important serious steem developments!

@thejohalfiles and @acidyo and @hanshotfirst and @neoxian and @pharesim and @fredrikaa and @stellabelle and @dan and @ned and @adept and @btcvenom will all probably want to support this too as it is some very useful software

I always feel it is important to support steem developers and I can see you have really done a lot of useful work for steem users. I was told your Github is full of very useful opensource projects and you built Radiator and Dr Otto Dr Phill and voting bots that can be very useful and profitable and can help build businesses on the steem blockchain. Thanks for working to make Steem blockchain a more accessible place for entrepreneurs wishing to develop here.

thats a good post bro..can i your blog resteemit????

no

okk my dear..

Meanie

lol so inertia is meanie for saying no? You knpow, inertia is just making a joke, bvecause no one has to have permission to do anything on steemit... in fact if nnajmull wants to repost or resteem inertias blog he can! @inertia knows this and @nnajmull should learn about this!

of course if you just copy pother peopels work and copypaste plagarize and steal that will eventuially get flagged, but if he meant just resteem that is open for anyone to do! anyone can resteem, no permission necisary!

I apologize if he was juyst asking to resteem. i am not accuysing him of plagaraism but his question was incorrect english and so was confusing XD its ok he will lear better english by being on here and getting paid!

I am getting tired now so I have to finish up here, just keep posting QUALITY content @tanata and i know you were just joking :D

Inertia is actuallyt very nice and we should all know this, he is leaving behind a VERY useful set of FREE open source spoftware whch anyone can use and make themselves very wealthy

Yes, it was a joke, no doubt)

I always feel it is important to support steem developers and I can see you have really done a lot of useful work for steem users. I was told your Github is full of ver Thanks for working to make Steem blockchain a more accessible place for entrepreneurs wishing to develop here.this thinks are so good 883054_603970906297312_553343333_o.jpg21368885_1174280799375567_6670150014350456415_o.jpg

it is very useful to everyone here ..thanks for this post

  ·  7 years ago Reveal Comment