Running Swatch as a permanent Daemon.
Starting Swatch on Boot
I had difficulty registering swatch as service on debian linux. The solution was simply to run it from /etc/rc.local.
I prefer running swatch from /etc/rc.local because it is easy to include multiple swatch commands to watch multiple logs simultaneously and you don’t have to trouble yourself with mutliple files in /etc/init.d that may not enable properly.
Here is the configuration:
Edit /etc/systemd/system/rc-local.service
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
Run these commands to create the file /etc/rc.local and make it executable:
printf '%s\n' '#!/bin/bash' 'exit 0' | tee -a /etc/rc.local
chmod 755 /etc/rc.local
Enable rc-local as a service:
systemctl enable rc-local
Now, just edit /etc/rc.local and keep it simple.
#!/bin/bash
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
/bin/mkdir -p /tmp/swatch
/usr/bin/swatch --daemon \
--config-file=/etc/swatch.conf \
--tail-file=/var/log/test.log \
--pid-file=/var/run/swatch.pid \
--script-dir=/tmp/swatch >> /var/log/swatch.log 2>&1
exit 0
Reboot, and you will find that swatch is running perfectly:
ps -ef | grep swatch|grep -v "grep"
root 370 1 0 14:29 ? 00:00:00 /usr/bin/swatch --daemon --config-file=/etc/swatch.conf --tail-file=/var/log/test.log --pid-file=/var/run/swatch.pid --script-dir=/tmp/swatch
Run a test to see that it is sending an instant message:
echo "trouble" >> /var/log/test.log
It works!
Reconfiguring and Restarting Swatch
Another reason I like using /etc/rc.local to launch swatch is how easy it is to reconfigure it and restart it. Here is an example
# edit swatch.conf to make some change to the configuration
vi /etc/swatch.conf
# kill the swatch processes
pkill swatch
# run /etc/rc.local as a script and test the results
/etc/rc.local
Keeping Swatch Alive
Swatch is not a process that will require a lot of system resources so it is likely to stay alive. If you are worried about it, you could create a cronjob that does the following:
- counts the number of swatch processes running
- if not the number expected:
- kill remaining swatch processes
- run /etc/rc.local as a script