Add systemd service file
Hej,
you might want to add a systemd service file to your repo.
Something like that: /etc/systemd/system/ruby-grafana-reporter.service
[Unit]
Description=Ruby Grafana reporter
After=network-online.target multi-user.target
Wants=network-online.target
# StartLimitIntervalSec > StartLimitBurst*RestartSec
StartLimitInterval=600s
StartLimitBurst=20
[Service]
Type=simple
User=bolausson
Group=bolausson
WorkingDirectory=/home/bolausson/tools/ruby-grafana-reporter
ExecStart=/home/bolausson/.local/share/gem/ruby/3.0.0/bin/ruby-grafana-reporter --config /home/bolausson/tools/ruby-grafana-reporter/grafana_reporter.config
KillMode=control-group
Restart=always
RestartSec=20s
SyslogIdentifier=ruby-grafana-reporter
MemoryMax=100M
[Install]
WantedBy=multi-user.target
How can I add such a file to a gem? I guess, I would need some kind of an installer for including something like that, right?
If not, please give me a hint, on how this could be done. Looks interesting!
I have no clue about ruby and gem. I would simply add it to your GitHub repo. If people need it, they can grab it from your repo and put it in place.
If you want to include in a gem - maybe this helps: https://stackoverflow.com/questions/10625267/approach-for-installing-system-service-implemented-as-ruby-gem
If you have a good idea on how this could be implemented to work for other users out of the box as well, feel free to provide a PR.
Warning:
I don't know about any best practices in the Ruby world and I don't know what is possible with the ruby install mechanism!
You would have to install it to a known destination like /opt/ruby-grafana-reporter and create a user & group e.g. "ruby-grafana-reporter" during setup.
Then you can provide a pre-configured systemd unit file e.g. by replacing in my above example:
User=bolausson
Group=bolausson
WorkingDirectory=/home/bolausson/tools/ruby-grafana-reporter
ExecStart=/home/bolausson/.local/share/gem/ruby/3.0.0/bin/ruby-grafana-reporter --config /home/bolausson/tools/ruby-grafana-reporter/grafana_reporter.config
with
User=ruby-grafana-reporter
Group=ruby-grafana-reporter
WorkingDirectory=/opt/ruby-grafana-reporter
ExecStart=/opt/ruby-grafana-reporter/bin/ruby-grafana-reporter --config /opt/ruby-grafana-reporter/etc/grafana_reporter.config
Or simply run as root (which I would not encourage to do)