Listen gem and monitoring for filesystem changes - ruby

I've just come across the listen gem which watches for file system events (or polls) and can execute your ruby code when a file changes or is added.
It looks really nice, but if I need to catch any change/addition to the files in a directory, is there a way for me to catch any files that were changed since the script was last run?
ie. is there any way to have Listen work with persistence?

If directory change events order matters, then you probably cannot get that order after your script has stopped working, as it lost and cannot be retrieved from the filesystem.
Otherwise I would utilize git repo to collect changes after previous commit, making commits before script stops working.

Try on console with below command to fix the issue
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

Related

logcheck command does not work but log check-test works

I am trying to listen to log file changes and trigger emails in case certain errors happen, currently testing this on mac.
i have executed
brew install logcheck
this worked, i am also able to test by executing
logcheck-test -l backend/test.log -r /usr/local/etc/logcheck/violations.d/ticket
but when i try to execute the logcheck command itself, i get error as command not found
Any ideas, thanks.

How to write a script for delete all the logs everytime macos shutdown?

I knew how to delete all the log files with terminal command "sudo rm -r /var/log" but how to set it up for system events, therefore, clear up everytime system shutdown?
We can execute commands or script at startup or shutdown,
see the links below for details,
unix.stackexchange.com or ccm or opensource.com

Transmission on FreeNAS does not call script on completion

I have been trying to get this running for over a week and am at a loss. I am trying to call a script on completion by using the settings.json config like so:
"script-torrent-done-enabled": true,
"script-torrent-done-filename": "/posttorrent.sh",
My script is in the root of the jail and is owned by transmission. I have also checked permissions on the file (which are 755) and have run chmod +x /posttorrent.sh.
I have even simplified the file to just output to a log file like so:
#!/bin/bash
echo "$TR_TORRENT_NAME is completed" >> /posttorrent.log
However, thus far, I still do not have a posttorrent.log file anywhere, no matter what file I download. I am not totally sure if I am on the right track as Transmission is set to log level 3 and yet I do not even see the calls to the script in /var/log/debug.log. I am sure I am missing something easy as others have been able to get this working, I am just out of options now as I think I have read and/or tried everything I could find in relation to this issue. Thanks!
The script is relying on /bin/bash being present inside of the jail.
You can either change the script to use /bin/sh, change /bin/bash
to /usr/bin/env bash, or link /path/to/port/bin/bash to
/usr/local/bin/bash (or wherever bash is located relative to the
jail directory, but if it exists it should be in /usr/local/bin).
ln -s /usr/local/bin/bash /path/to/jail/bin/bash
Also, the root directory (by default) is only writable by root, so
the transmission user would not have permission to create
the log file in the root directory. To properly allow creation of
the log file change the destination directory to one that the
transmission user has permission to write to. For example
/var/db/transmission/posttorrent.log, if using the FreeNAS plugin.
A directory can be created for the transmission user using the
install utility:
install -d -o transmission -g transmission /home/transmission
Alternatively the log file can be created manually using the
install utility, or the owner can be set with chown:
install -o transmission -g transmission -m 644 /dev/null /posttorrent.log
# or on an existing log file
chown transmission /posttorrent.log
chgrp transmission /posttorrent.log
# normally the mode bits will already be 644
chmod 644 /posttorrent.log
Transmission will also rewrite the configuration file when it
exits. So transmission-daemon has to be stopped before
editing the settings file. However, if using the Transmission plugin,
the settings are stored in a SQLite database
(/usr/pbi/transmission-amd64/transmissionUI/transmission.db)
and the settings file will be recreated from the database on
startup. sqlite3 can be used to manually edit the database, or the plugin's settings can be edited on the FreeNAS web UI.
sqlite3 /usr/pbi/transmission-amd64/transmissionUI/transmission.db <<EOF
UPDATE freenas_transmission SET enable=1;
UPDATE freenas_transmission SET script_post_torrent="/posttorrent.sh";

Get Riak to start with chef

I need help getting Riak to work with Chef.
Currently every time I chef an amazon box with Riak 1.4.8 using the default basho riak cook book I have to manually ssh into the machine kill -9 the beam.smp process then rm -rf /var/lib/riak/ring then I can finally do sudo riak start and it will work.
Prior to that I get:
Node 'riak#' not responding to pings.
I have even created a shell script:
#!/bin/bash
# Generated by Chef for <%= #node[:fqdn] %>
#<%= #node[:ec2][:local_ipv4] %>
# This script should be run by root.
riak stop
riakPid="/var/run/riak/riak.pid"
if [ -e "$riakPid" ]; then
kill -9 $(<${riakPid})
fi
rm -f /var/run/riak/*
rm -f /var/lib/riak/ring/*
riak start
And Chef says:
bash[/etc/riak/clearOldRiakInfo.sh] ran successfully
For the above script.
If I manually run that script everything works fine. Why is this not cheffing properly.
UPDATE:
This has been solved by creating a script to delete the ring directory when the machine gets cheffed.
This would only happen when I would create a new machine from scratch as the fqdn would get set correctly after Riak had started and created the ring. If I manually went on the box and deleted the ring then it would rechef perfectly fine. So I have to create the script so that the very first chef run on the machine would clean out the ring info.
Given the error message you provided, Riak is not starting because the Erlang node name is not being generated correctly. The Erlang node name configuration exists within vm.args and is produced by the node['riak']['args']['-name'] attribute.
The default for node['riak']['args']['-name'] is riak##{node['fqdn']}. Please check the value Ohai is reporting for node['fqdn']. Alternatively, if you are overriding this attribute somewhere else, ensure that produces a valid value for -name.
A more detailed description of -name within vm.args can be found here.

Clockwork Ruby Gem: How to restart?

How does one restart the Ruby clockwork gem?
After reading the Wiki, it seems you can only start it, not stop or restart it.
I don't want to manually kill the process and run it again.
The modern syntax to restart clockworkd is:
bin/clockworkd -c periodic-jobs.rb reload
If you've bundled your gems, as you should:
bundle exec bin/clockworkd -c periodic-jobs.rb reload
…where periodic-jobs.rb is your clockwork jobs config file.
Full options:
bin/clockworkd help
Usage: clockworkd -c FILE [options] start|stop|restart|run
--pid-dir=DIR Alternate directory in which to store the process ids. Default is /Users/jm3/Code/soakcity/tmp.
-i, --identifier=STR An identifier for the process. Default is clock file name.
-l, --log Redirect both STDOUT and STDERR to a logfile named clockworkd[.<identifier>].output in the pid-file directory.
--log-dir=DIR A specific directory to put the log files into (default location is pid directory).
-m, --monitor Start monitor process.
-c, --clock=FILE Clock .rb file. Default is /Users/jm3/Code/soakcity/clock.rb.
-d, --dir=DIR Directory to change to once the process starts
-h, --help Show this message
Learn more in the Demonization section of the clockwork source on GitHub. Hope that helps!
Assuming you started it as a daemon, then 'clockworkd -c YOUR_CLOCK.rb stop' should do the trick.

Resources