Create a Bash init script for Suricata - bash

I'm running an older version "1.1" of Suricata on my Fedora 14 System. It was installed through yum and as such doesn't have a working init script due to some issues that I've read about. Is there a simple way to include the following in a generic init script so that suricata autostarts when the system boots.
Thanks for any help/direction.

Try this on for size: "call it suricata and place it in your /etc/init.d directory
#!/bin/bash
#
# Init file for suricata
#
#
# chkconfig: 345 52 48
# description: Network Intrusion Detection System
#
# processname: Suricata
# pidfile: /var/run/suricata.pid
source /etc/rc.d/init.d/functions
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="suricata"
desc="Suricata IDS"
start() {
echo -n $"Starting $desc ($prog): "
daemon suricata -c /etc/suricata.yaml -i eth0
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Shutting down $desc ($prog): "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
depending on your system, I'm not running Fedora 14, you may need to provide the absolute path to the suricata binary. Mine is /usr/local/bin/suricata
You should also consider updating or at least compiling from source this gives you a make install-full option that does all of this for you now including installation of an init script. You can download it from the suricata open info sec website

Related

How to start elasticsearch run as service in centos 7?

I have install elasticsearch 2.3.3 in centos 7 but after closing terminal elasticsearch plugin head automatically close but I want keep running in background. please give me helpful answer.
You can run it in background as two ways,
1. Nohup
2. Creating service script and put it in init.d folder
Nohup
Eg: nohup ./bin/elasticsearch
Service script
Use the following script,
#!/bin/bash
### BEGIN INIT INFO
# Provides: Elasticsearch
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Runs elasticsearch daemon
# Description: Runs the elasticsearch daemon as a non-root user
### END INIT INFO
# Process name
NAME=elasticsearch
DESC="Elasticsearch"
PROG="/etc/init.d/elasticsearch"
# Configure location of Elasticsearch bin
ELASTICSEARCH_BIN=/opt/elasticsearch-2.3.0/bin
# PID Info
PID_FOLDER=/var/run/elasticsearch/
PID_FILE=/var/run/elasticsearch/$NAME.pid
LOCK_FILE=/var/lock/subsys/$NAME
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$ELASTICSEARCH_BIN
DAEMON=$ELASTICSEARCH_BIN/$NAME
# Configure logging location
ELASTICSEARCH_LOG=/var/log/elasticsearch.log
# Begin Script
RETVAL=0
if [ `id -u` -ne 0 ]; then
echo "You need root privileges to run this script"
exit 1
fi
# Function library
. /etc/init.d/functions
start() {
echo -n "Starting $DESC : "
pid=`pidofproc -p $PID_FILE elasticsearch`
if [ -n "$pid" ] ; then
echo "Already running."
exit 0
else
# Start Daemon
if [ ! -d "$PID_FOLDER" ] ; then
mkdir $PID_FOLDER
fi
daemon --user=$DAEMON_USER --pidfile=$PID_FILE $DAEMON 1>"$ELASTICSEARCH_LOG" 2>&1 &
sleep 2
pidofproc node > $PID_FILE
RETVAL=$?
[[ $? -eq 0 ]] && success || failure
echo
[ $RETVAL = 0 ] && touch $LOCK_FILE
return $RETVAL
fi
}
reload()
{
echo "Reload command is not implemented for this service."
return $RETVAL
}
stop() {
echo -n "Stopping $DESC : "
killproc -p $PID_FILE $DAEMON
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $PID_FILE $LOCK_FILE
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $PID_FILE $DAEMON
RETVAL=$?
;;
restart)
stop
start
;;
reload)
reload
;;
*)
# Invalid Arguments, print the following message.
echo "Usage: $0 {start|stop|status|restart}" >&2
exit 2
;;
esac
sudo chmod +x /etc/init.d/elasticsearch
sudo update-rc.d elasticsearch defaults 96 9
sudo /etc/init.d/elasticsearch restart

Running Cassandra as a service on OpenSuse

I am trying to run Cassandra as a service on OpenSuse (Leap 42.1).
I have tried installing with apache-cassandra-2.1.11-bin.tar.gz and then copying /etc/init.d/cassandra from https://gist.github.com/sgomezvillamor/5458309. However, the startup script is not designed for OpenSuse, as the system.log says:
/etc/init.d/cassandra: line 30: daemon: command not found.
The problem would not exist if there would be an installer that would create the scripts correctly, similarly as there are for some other OSs. Searching for an installation package, I found http://www.datastax.com/dev/blog/announcing-rpms-cassandra and tried to look for an rpm in rpm.riptano.com but I cannot figure out which one would work in OpenSuse.
Which of those packages would work for OpenSuse? Or, how should I modify the startup script for Suse-fying it?
I would check these instructions which are for Cassandra 2.1: Installing DataStax Community 2.1 on RHEL-based systems.
I'm not sure whether that will get you 100% there on an OpenSUSE system, but should get you very close.
I got it to work with /etc/init.d/cassandra file:
#!/bin/bash
#
# /etc/init.d/cassandra
#
# Startup script for Cassandra
#
# chkconfig: 2345 20 80
# description: Starts and stops Cassandra
#. /etc/rc.d/init.d/functions
export CASSANDRA_HOME=/opt/apache-cassandra-2.1.11
export CASSANDRA_CONF=$CASSANDRA_HOME/conf
export CASSANDRA_INCLUDE=$CASSANDRA_HOME/bin/cassandra.in.sh
#export CASSANDRA_OWNR=cassandra
export CASSANDRA_OWNR=root
#NAME="cassandra"
NAME="root"
log_file=/srv/cassandra/log/cassandra.log
pid_file=/var/run/cassandra/cassandra.pid
lock_file=/var/lock/subsys/$NAME
CASSANDRA_PROG=/opt/apache-cassandra-2.1.11/bin/cassandra
# The first existing directory is used for JAVA_HOME if needed.
JVM_SEARCH_DIRS="/usr/lib/jvm/jre /usr/lib/jvm/jre-1.7.* /usr/lib/jvm/java-1.7.*/jre"
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# If JAVA_HOME has not been set, try to determine it.
if [ -z "$JAVA_HOME" ]; then
# If java is in PATH, use a JAVA_HOME that corresponds to that. This is
# both consistent with how the upstream startup script works, and with
# the use of alternatives to set a system JVM (as is done on Debian and
# Red Hat derivatives).
java="`/usr/bin/which java 2>/dev/null`"
if [ -n "$java" ]; then
java=`readlink --canonicalize "$java"`
JAVA_HOME=`dirname "\`dirname \$java\`"`
else
# No JAVA_HOME set and no java found in PATH; search for a JVM.
for jdir in $JVM_SEARCH_DIRS; do
if [ -x "$jdir/bin/java" ]; then
JAVA_HOME="$jdir"
break
fi
done
# if JAVA_HOME is still empty here, punt.
fi
fi
JAVA="$JAVA_HOME/bin/java"
export JAVA_HOME JAVA
case "$1" in
start)
# Cassandra startup
echo -n "Starting Cassandra: "
su $CASSANDRA_OWNR -c "$CASSANDRA_PROG -p $pid_file" > $log_file 2>&1
retval=$?
[ $retval -eq 0 ] && touch $lock_file
echo "OK"
;;
stop)
# Cassandra shutdown
echo -n "Shutdown Cassandra: "
su $CASSANDRA_OWNR -c "kill `cat $pid_file`"
retval=$?
[ $retval -eq 0 ] && rm -f $lock_file
for t in `seq 40`; do $0 status > /dev/null 2>&1 && sleep 0.5 || break; done
# Adding a sleep here to give jmx time to wind down (CASSANDRA-4483). Not ideal...
# Adam Holmberg suggests this, but that would break if the jmx port is changed
# for t in `seq 40`; do netstat -tnlp | grep "0.0.0.0:7199" > /dev/null 2>&1 && sleep 0.1 || break; done
sleep 5
STATUS=`$0 status`
if [[ $STATUS == "$NAME is stopped" ]]; then
echo "OK"
else
echo "ERROR: could not stop $NAME: $STATUS"
exit 1
fi
;;
reload|restart)
$0 stop
$0 start
;;
status)
status -p $pid_file cassandra
exit $?
;;
*)
echo "Usage: `basename $0` start|stop|status|restart|reload"
exit 1
esac
exit 0

How to set exit codes for "status" command in init script for Tomcat6

I'm installing Tomcat6 and using the following for /etc/init.d/tomcat6:
#!/bin/bash
# description: Tomcat6 service
# processname: java
# chkconfig: - 99 1
## Note: CATALINA_HOME and CATALINA_PID are set elsewhere.##
# Source function library.
. /etc/init.d/functions
# Source sysconfig for tomcat6
if [ -f /etc/sysconfig/tomcat6 ]; then
. /etc/sysconfig/tomcat6
fi
[ -d "$CATALINA_HOME" ] || { echo "Tomcat requires $CATALINA_HOME."; exit 1; }
case $1 in
start|stop|run)
if su $TOMCAT_USER bash -c "cd $CATALINA_HOME/logs; $CATALINA_HOME/bin/catalina.sh $1"; then
echo -n "Tomcat $1 successful"
[ $1 == "stop" ] && rm -f $CATALINA_PID
else
echo -n "Error in Tomcat $1: $?"
fi
;;
restart)
$0 start
$0 stop
;;
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "$0 is already running at ${kpid}"
else
echo "$CATALINA_PID found, but $kpid is not running"
fi
unset kpid
else
echo "$0 is stopped"
fi
;;
esac
exit 0
The problem, as noted in this related ticket, is that Chef checks the "status" of a service and will not start it if the "status" command returns an exit code of "0". Which it always does because the script itself completes successfully, regardless of whether the service is running or not.
I need to adapt my init script to return an exit code of 3 if the service is not running, per the guidelines for Init scripts posted here:
0 program is running or service is OK
1 program is dead and /var/run pid file exists
2 program is dead and /var/lock lock file exists
3 program is not running
4 program or service status is unknown
5-99 reserved for future LSB use
100-149 reserved for distribution use
150-199 reserved for application use
200-254 reserved
I modified my initial script to:
#!/bin/bash
# description: Tomcat6 service
# processname: java
# chkconfig: - 99 1
# Source function library.
. /etc/init.d/functions
# Source sysconfig for tomcat6
if [ -f /etc/sysconfig/tomcat6 ]; then
. /etc/sysconfig/tomcat6
fi
[ -d "$CATALINA_HOME" ] || { echo "Tomcat requires $CATALINA_HOME."; exit 1; }
exit_var=0
case $1 in
start|stop|run)
if su $TOMCAT_USER bash -c "cd $CATALINA_HOME/logs; $CATALINA_HOME/bin/catalina.sh $1"; then
echo -n "Tomcat $1 successful"
[ $1 == "stop" ] && rm -f $CATALINA_PID
else
echo -n "Error in Tomcat $1: $?"
exit_var=1
fi
;;
restart)
$0 start
$0 stop
;;
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "$0 is already running at ${kpid}"
exit_var=0
else
echo "$CATALINA_PID found, but $kpid is not running"
exit_var=4
fi
unset kpid
else
echo "$0 is stopped"
exit_var=3 # Fixes issue with Chef not starting a stopped service.
fi
;;
esac
exit $exit_var
But those aren't ACTUALLY changing the exit codes for the script. How can I set different exit codes for different case scenarios?
Version Info:
OS: CentOS 6.5
Chef: 10.20
Tomcat: 6.0.39
You have the right idea, but you have exit_var=3 in the wrong place. I have placed it below to equal 3 for the status when it is already running:
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "$0 is already running at ${kpid}"
## Fixes issue with Chef not starting a stopped service.
exit_var=3 ## this is the condition of already running
else
echo "$CATALINA_PID found, but $kpid is not running"
exit_var=4
fi
unset kpid
else
echo "$0 is stopped"
exit_var=5 # (renumbered 5 set as you desire)
fi
;;
esac
exit $exit_var

How to write Gradle startup script

I have a Gradle app that I startup using ./gradlew run. This works fine, but I'm trying to deploy to an AWS instance (Ubuntu 12) and I would like the script to execute on boot. I tried writing a startup.sh file with the above command, but no dice. I've also tried adding the command to the /etc/rc.local file, but that doesn't seem to work either. Can someone give me an idea as to how to execute `./gradlew run' on startup? Thanks!
I wrote the following init script for starting gradle applications at system startup for redhat distros (centos/fedora etc).
You need to perform a few steps to tie it all together:
deploy your gradle application using gradle distZip onto your target server
create a configuration file /etc/my-service.conf
link the init script (see below) to the service name in /etc/init.d/my-service
An example configuration file /etc/my-service.conf
username=someunixuser
serviceName=MyStandaloneServer
prog="/path/to/bin/MyStandaloneServer -a any -p params -y you -w want"
javaClass="some.java.MyStandaloneServer"
Note the path to the application from the distZip in the prog line.
You then link the init script to the actual service you want it to be run as, e.g.
ln -s /path/to/gradle-init-start-stop /etc/init.d/my-service
Once you've done this, you can use chkconfig to add the service in the usual way (it defaults to 3/4/5)
Here is the script gradle-init-start-stop
#!/bin/bash
#
# chkconfig: 345 80 20
# description: Start and stop script for gradle created java application startup
#
# This is a generic file that can be used by any distribution from gradle ("gradle distZip").
# Link this file to the name of the process you want to run.
# e.g.
# ln -s /path/to/gradle-init-start-stop /etc/init.d/ivy-jetty
#
# it requires a conf file /etc/NAME.conf, e.g. /etc/ivy-jetty.conf
# otherwise it will quit.
#
# CONFIGURATION FILE ENTRIES:
# ---------------------------
# username=process-owner
# prog="/path/to/gradle-startscript -a any -e extra parameters"
# serviceName=SomeShortNameForService
# javaClass=package.for.JavaClass
. /etc/rc.d/init.d/functions
BASENAME=$(basename $0)
maxShutdownTime=15
CONF=/etc/${BASENAME}.conf
pidfile=/var/run/$BASENAME.pid
if [ ! -f $CONF ] ; then
echo "Could not find configuration file: $CONF"
exit 1
fi
####### SOURCE CONFIGURATION FILE
source $CONF
checkProcessIsRunning() {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
if [ ! -e /proc/$pid ]; then return 1; fi
return 0
}
checkProcessIsOurService() {
local pid="$1"
if [ "$(ps -p $pid --no-headers -o comm)" != "java" ]; then return 1; fi
grep -q --binary -F "$javaClass" /proc/$pid/cmdline
if [ $? -ne 0 ]; then return 1; fi
return 0
}
getServicePID() {
if [ ! -f $pidfile ]; then return 1; fi
pid="$(<$pidfile)"
checkProcessIsRunning $pid || return 1
checkProcessIsOurService $pid || return 1
return 0
}
startService() {
cmd="nohup $prog >/dev/null 2>&1 & echo \$!"
sudo -u $username -H $SHELL -c "$cmd" > $pidfile
sleep 0.2
pid="$(<$pidfile)"
if checkProcessIsRunning $pid; then
return 0
else
return 1
fi
}
start() {
getServicePID
if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; echo ""; return 0; fi
echo -n "Starting $serviceName: "
startService
if [ $? -ne 0 ] ; then
echo "failed"
return 1
else
echo "started"
return 0
fi
}
stopService() {
# soft kill first...
kill $pid || return 1
# check if process dead, sleep 0.2s otherwise
for ((i=0; i<maxShutdownTime*5; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ] ; then
rm -f $pidfile
return 0
fi
sleep 0.2
done
# hard kill now...
kill -s KILL $pid || return 1
# check if process dead, sleep 0.2s otherwise
for ((i=0; i<maxShutdownTime*5; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ] ; then
rm -f $pidfile
return 0
fi
sleep 0.2
done
return 1
}
stop() {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
pid="$(<$pidfile)"
echo -n "Stopping $serviceName "
stopService
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0
}
restart() {
stop
start
}
checkServiceStatus() {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0;
}
####### START OF MAIN SCRIPT
RETVAL=0
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
status)
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit $RETVAL

rc.d script looks for my binary in /run/daemons

EDIT: I am following this example.
Trying to write an archlinux rc.d script for mongod. I put my binaries in /usr/bin. Here is what I got so far:
#!/bin/bash
# import predefined functions
. /etc/rc.conf
. /etc/rc.d/functions
# Point to the binary
DAEMON=/usr/bin/mongod
# Get the ARGS from the conf
. /etc/conf.d/crond
# Function to get the process id
PID=$(get_pid $DAEMON)
case "$1" in
start)
stat_busy "Starting $DAEMON"
# Check the PID exists - and if it does (returns 0) - do no run
[ -z "$PID" ] && $DAEMON $ARGS $> /dev/null
if [ $? = 0 ]; then
add_daemon $DAEMON
stat_done
else
stat_fail
exit 1
fi
;;
stop)
stat_busy "Stopping $DAEMON"
kill -HUP $PID &>/dev/null
rm_daemon $DAEMON
stat_done
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
The problem is that when I do sudo rc.d start mongod, I get the following error:
:: Starting /usr/bin/mongod
[BUSY] /etc/rc.d/functions: line 203: /run/daemons//usr/bin/mongod: No such file or directory
[DONE]
Syntax error.
I used $> instead of &> on the line:
[ -z "$PID" ] && $DAEMON $ARGS $> /dev/null

Resources