I’m using Perlbrew, which means that my Perl scripts depend on the right Perl version being used because the modules are all installed per version. If I don’t, then I’ll see errors like the following:
`Can't load application from file "/home/alex/farm/face/face.pl": Can't locate GD.pm in @INC (you may need to install the GD module) (@INC contains: ...`
When I run scripts from `cron` or `monit`, their environment is not as great as it usually is when I’m in an interactive session. Thus, I was able to run my scripts from the command line, but I couldn’t restart my webserver from the Monit page.
I run my sites using Toadfarm from `/home/alex/farm/farm` and thus I’d see the following in `/var/log/monit.log` once I ran into a problem:
[CET Jan 23 21:38:31] error : 'toadfarm' total mem amount of 589.9 MB matches resource limit [total mem amount>500.0 MB] [CET Jan 23 21:38:31] info : 'toadfarm' trying to restart [CET Jan 23 21:38:31] info : 'toadfarm' stop: /home/alex/farm/farm [CET Jan 23 21:39:01] error : 'toadfarm' failed to stop (exit status 2) -- /home/alex/farm/farm: Can't locate Toadfarm.pm in @INC (you may need to install the Toadfarm module) (@INC contains: /home /alex/perl5/perlbrew/perls/perl-5.24.0/lib/site_perl/5.24.0/x86_64-linux /home/alex/perl5/perlbrew/perls/perl-5.24.0/lib/site_perl/5. [CET Jan 23 21:41:01] info : 'toadfarm' process is running after previous exec error (slow starting or manually recovered?)
So now I wrote a little wrapper script called `/home/alex/farm/farm-wrapper`.
I use it from my Monit config:
check process toadfarm with pidfile /home/alex/farm/farm.pid start program = "/home/alex/farm/farm-wrapper start" as uid alex gid alex stop program = "/home/alex/farm/farm-wrapper stop" as uid alex gid alex # http://alexschroeder.ch:8080/wiki if failed host alexschroeder.ch port 8080 type tcp protocol http and request "/wiki/Monit" for 5 cycles then restart if totalmem > 1000 MB for 5 cycles then restart if 6 restarts within 15 cycles then timeout
The wrapper script is `chmod +x`, just like the actual farm script, `/home/alex/farm/farm`.
#!/bin/bash 1. This wrapper sets up the Perlbrew environment we need to restart the 1. farm from Monit. source /home/alex/perl5/perlbrew/etc/bashrc 1. Setup local::lib eval "$(perl -I/home/alex/perl5/lib/perl5 -Mlocal::lib)" 1. Run farm, depending on its shebang /home/alex/farm/farm $*
And the actual farm script:
#!/home/alex/perl5/perlbrew/perls/perl-5.24.0/bin/perl use Toadfarm -init; $ENV{PATH} .= ":/usr/local/bin"; ...
#Administration #Monit