Administration / Webmaster, Datenschutz, English, Linux, OccuSearch, SearX

Daemonize and control SearX with the service command

SearX

SearX Logo

Under several Linux distributions, including Debian and Ubuntu, we have the service command to controll daemons, e.g. service apache2 restart. In this article, we learn how to create a service-command for the standalone version of the SearX meta search engine. We build upon the default installation as prescribed within the quick start guide of the project documentation. We include both, searX and filtron.

Introduction

First we need a possibility to start SearX as a daemon, so we install the daemonize command:

sudo apt-get install daemonize

With daemonize, we can have an error-log, a std-out log and some pid that takes care about having only one instance running. With SearX installed at the default destination /opt/searx, we can execute the following command to start a SearX daemon:

daemonize -a -e /var/log/searx.error.log -o /var/log/searx.log -p /var/run/searx.pid -l /var/lock/subsys/searx /opt/searx/searx-ve/bin/python /opt/searx/searx/webapp.py

In a similar way, we can start a filtron instance (assuming the filter rules at /etc/filtron/rules.json):

daemonize -a -e /var/log/filtron.error.log -o /var/log/filtron.log -p /var/run/filtron.pid -l /var/lock/subsys/filtron /root/go/bin/filtron -rules /etc/filtron/rules.json

Usually, we will start filtron first and then start SearX.

We furthermore need some kill commands to stop filtron and SearX. We use ps, grep, awk and kill.

Stopping filtron:

kill $(ps -aux | grep filtron | awk '{print $2}')

Stopping SearX:

kill $(ps -aux | grep /opt/searx/searx/webapp.py | awk '{print $2}')

Realizing the service commands

We usually have three options following the service command: service searx start, service searx stop and service searx restart. This results in the following code snippet for the searx.sh file stored at /etc/init.d/searx.sh:

#! /bin/sh
sleep 3
case "$1" in
  start)
        daemonize -a -e /var/log/filtron.error.log -o /var/log/filtron.log -p /var/run/filtron.pid -l /var/lock/subsys/filtron /root/go/bin/filtron -rules /etc/filtron/rules.json
        daemonize -a -e /var/log/searx.error.log -o /var/log/searx.log -p /var/run/searx.pid -l /var/lock/subsys/searx /opt/searx/searx-ve/bin/python /opt/searx/searx/webapp.py
        ;;
   stop)
        kill $(ps -aux | grep filtron | awk '{print $2}')
        kill $(ps -aux | grep /opt/searx/searx/webapp.py | awk '{print $2}')
        ;;
  restart)
        kill $(ps -aux | grep filtron | awk '{print $2}')
        kill $(ps -aux | grep /opt/searx/searx/webapp.py | awk '{print $2}')
        daemonize -a -e /var/log/filtron.error.log -o /var/log/filtron.log -p /var/run/filtron.pid -l /var/lock/subsys/filtron /root/go/bin/filtron -rules /etc/filtron/rules.json
        daemonize -a -e /var/log/searx.error.log -o /var/log/searx.log -p /var/run/searx.pid -l /var/lock/subsys/searx /opt/searx/searx-ve/bin/python /opt/searx/searx/webapp.py
        ;;
        *)
        ;;
esac
exit 0

In some cases we have issues with uswgi. Therefore, consider to uninstall uswgi.

sudo apt-get remove uswgi

Now, reinitialize the service command with the following command:

systemctl daemon-reload

Now it is possible to controll filtron and SearX in a comfortable way. Try the following:

service searx start
service searx stop
service seary restart

You will see SearX and filtron starting, stopping or restarting.

Final Remarks

Have some fun with this comfortable solution!

Do it! Build your own search engine! Go go go!

For an example instance, have a look at https://occusearch.net. Feel free to use and recommend this instance to your family, friends and colleagues.