When working with the runit cookbook, you may define a service like this:
runit_service "your_name" do
env({"HOME" => "some value")
check true
end
and then you’d have to create the following template files:
and
1
sv-your_name-run.erb
. In the 1
sv-your_name-log-run.erb
template you’d have something
like this:1
sv-your_name-run
#!/bin/sh
exec 2>&1
exec chpst -e env echo $(date) >> log.txt
of course, it’s kind of dumb to just echo date, and have that ran as a service, but, hey, it’s just an example. Basically, runit will make sure that the service will be restarted if it goes down. So, in the case above, you will get a new line in log.txt about once a second.
Notice that in the runit_service I’m setting the env attribute. This has the effect of
passing environment variables to runit, which will be creted in the env folder. That’s why
the
part is there in the run script.1
-e env
The second template, I usually put it like this:
#!/bin/sh
exec svlogd -tt ./main
and this will have the effect of sending the logs to
.
Of course, the logs will be sent to that file if you’re logging to STDOUT. As an extra
attribute, I’m setting 1
/etc/service/your_name/log/main/current
, which allows us to write a script to check if our
service is running. It’s template file will be 1
check true
. You can put anything
you want in it, as long as you exit with a 0 code. For example, to check that a ruby
script was running as a service, I did something like this:1
sv-ruby-your_name-check.erb
#!/bin/bash
exec &> /dev/null
exec ps aux | grep -v grep | grep -i ruby
If the check script writes output to stdout, it causes a write error. That’s why I’m redirecting to /dev/null.
To start/stop/restart/view logs:
sudo sv start your_name
sudo sv stop your_name
sudo sv restart your_name
cat /etc/service/your_name/log/main/current