I'm trying to configure tomcat init.d start script to work with multiple instances (at this time 2 instances)
I'm following below sample script to create init.d script
#!/bin/bash
#
# tomcat This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Short-Description: start and stop tomcat
### END INIT INFO
TOMCAT_USER=root
TOMCAT_HOME="/opt/tomcat7/node1"
SHUTDOWN_WAIT=45
tomcat_pid() {
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat service"
/bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/startup.sh" $TOMCAT_USER
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stoping Tomcat"
/bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/shutdown.sh" $TOMCAT_USER
let kwait=$SHUTDOWN_WAIT
count=0
count_by=5
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
sleep $count_by
let count=$count+$count_by;
done
if [ $count -gt $kwait ]; then
echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo "Tomcat is not running"
fi
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
else
echo "Tomcat is not running"
fi
;;
esac
exit 0
problem is tomcat_pid() method returns process ids of all tomcat instances, because of that, the second instance cannot be started. Is there a better method to handle this?
found a workaround, but expecting better solution
using netstat we can find process id via running port number
echo `netstat -tlnp | awk '/:80 */ {split($NF,a,"/"); print a[1]}'`
So i modified the function tomcat_pid() as below
tomcat_pid() {
echo `netstat -tlnp | awk '/:<port> */ {split($NF,a,"/"); print a[1]}'`
}
Related
I have been trying to install Promtail on a RHEL 6 server and keep getting this error
Error: caller=main.go:115 msg="error creating promtail" error="at least one client config should be provided"
It seems to be requesting for the config file which has been passed CONFIG_FILE=/usr/local/bin/promtailconf.yml
I have done this deployment a number of times on RHEL 8 without a hitch not sure what i am getting wrong
#cat /etc/init.d/promtail
#! /bin/bash
# chkconfig: 345 20 80
# description: my service
345 - 3,4,5 runlevels
20 - start priority
80- stop prioroty
RETVAL=0
PROG="promtail"
EXEC=/usr/local/bin/promtail
LOCKFILE="/var/lock/subsys/$PROG"
LOGFILE=/var/log/promtail.log
DATADIR=/usr/local/bin/promtail/data
CONFIG_FILE=/usr/local/bin/promtailconf.yml
ErrLOGFILE=/var/log/promtail_error.log
# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
echo "/etc/rc.d/init.d/functions is not exists"
exit 0
fi
start() {
if [ -f $LOCKFILE ]
then
echo "$PROG is already running!"
else
echo -n "Starting $PROG: "
nohup $EXEC $OPTIONS > $LOGFILE 2> $ErrLOGFILE &
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $LOCKFILE && success || failure
echo
return $RETVAL
fi
}
stop() {
echo -n "Stopping $PROG: "
killproc $EXEC
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -r $LOCKFILE && success || failure
config file
cat /usr/local/bin/promtailconf.yml
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://xxx.xxx.xxx.xxx:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*log
https://dgraph.io/tour/intro/2/
asks for three docker commands being run in different terminals:
zero
alpha
ratel
I'd rather start things from a single script dgraph within Mac OSX 10.3.6 and other environments and use e.g. the terminal app to call the different scripts along the lines of Running a command in a new Mac OS X Terminal window
How can the script below adapted for other environments like Unix and Windows?
dgraph
#!/bin/bash
# WF 2020-08-05
# see https://dgraph.io/tour/intro/2/
version=v20.03.0
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show the given error message on stderr and exit
#
# params:
# 1: l_msg - the error message to display
#
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error:" 1>&2
color_msg $red "\t$l_msg" 1>&2
exit 1
}
# show usage
#
usage() {
echo "$0 [-h|--help|-k|--kill"
echo ""
echo "-b | --bash: start a bash terminal shell within the currently running container"
echo "-h | --help: show this usage"
echo "-k | --kill: stop the docker image"
exit 1
}
#
# stop the docker image
#
stopImage() {
color_msg $blue "stopping and removing dgraph image ..."
docker stop dgraph
docker rm dgraph
color_msg $green "...done"
}
#
# start a bash shell within the currently running container
#
bashInto() {
sudo docker exec -it dgraph bash
}
#
# dgraph zero
#
zero() {
docker run -it -p 5080:5080 -p 6080:6080 -p 8080:8080 \
-p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph \
dgraph/dgraph:$version dgraph zero
}
#
# dgraph alpha
#
alpha() {
docker exec -it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0
}
#
# dgraph ratel
#
ratel() {
docker exec -it dgraph dgraph-ratel
}
me=$0
dir=$(dirname $0)
base=$(basename $0)
if [ $# -lt 1 ]
then
case $base in
dgraph)
# Run Dgraph zero
# And in another, run ratel (Dgraph UI)
# In another terminal, now run Dgraph alpha
for option in zero ratel alpha
do
#echo $dir $option
open -a terminal.app $dir/$option
# wait a bit
sleep 2
done
;;
alpha) alpha;;
ratel) ratel;;
zero) zero;;
esac
fi
# commandline option
while [ "$1" != "" ]
do
option="$1"
case $option in
alpha) alpha;;
ratel) ratel;;
zero) zero;;
-b|--bash) bashInto;;
-k|--kill) stopImage;;
-h|--help) usage;;
esac
shift
done
https://github.com/WolfgangFahl/DgraphAndWeaviateTest/blob/master/scripts/dgraph now has a script working on Linux (tested with travis/Ubuntu 18.04 LTS - bionic).
See also https://travis-ci.org/github/WolfgangFahl/DgraphAndWeaviateTest/jobs/715131236
usage
scripts/dgraph -h
scripts/dgraph [-b|--bash|-h|--help|-k|--kill|-p|--pull]
-b | --bash: start a bash terminal shell within the currently running container
-h | --help: show this usage
-k | --kill: stop the docker image
-p | --pull: pull the docker image
dgraph
#!/bin/bash
# WF 2020-08-05
#
# Starts zero alpha and ratel docker based with one script
#
# see https://dgraph.io/tour/intro/2/
# see https://stackoverflow.com/questions/63260073/starting-zero-alpha-and-ratel-in-a-single-command-e-g-in-macosx-and-other-envir
# see https://discuss.dgraph.io/t/dgraph-start-script/9231
version=v20.03.0
# interative tty option for docker
it="-it"
os=$(uname)
case $os in
Linux)
docker="docker"
it="";;
#docker="sudo docker";;
Darwin)
docker="docker";;
*)
docker="docker";;
esac
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show the given error message on stderr and exit
#
# params:
# 1: l_msg - the error message to display
#
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error:" 1>&2
color_msg $red "\t$l_msg" 1>&2
exit 1
}
# show usage
#
usage() {
echo "$0 [-h|--help|-k|--kill"
echo ""
echo "-b | --bash: start a bash terminal shell within the currently running container"
echo "-h | --help: show this usage"
echo "-k | --kill: stop the docker image"
echo "-p | --pull: pull the docker image"
exit 1
}
#
# stop the docker image
#
stopImage() {
color_msg $blue "stopping and removing dgraph image ..."
$docker stop dgraph
$docker rm dgraph
color_msg $green "...done"
}
#
# pull the docker image
#
pullImage() {
color_msg $blue "pulling dgraph image $version ..."
$docker pull dgraph/dgraph:$version
color_msg $green "...done"
}
#
# start a bash shell within the currently running container
#
bashInto() {
$docker exec -it dgraph bash
}
#
# dgraph zero
#
zero() {
# Let’s create a folder for storing Dgraph data outside of the container:
mkdir -p ~/dgraph
$docker run $it -p 5080:5080 -p 6080:6080 -p 8080:8080 \
-p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph \
dgraph/dgraph:$version dgraph zero
}
#
# dgraph alpha
#
alpha() {
$docker exec $it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0
}
#
# dgraph ratel
#
ratel() {
$docker exec $it dgraph dgraph-ratel
}
me=$0
dir=$(dirname $0)
base=$(basename $0)
if [ $# -lt 1 ]
then
case $base in
dgraph)
# Run Dgraph zero
# And in another, run ratel (Dgraph UI)
# In another terminal, now run Dgraph alpha
for option in zero ratel alpha
do
# make sure linked versions of command are available
if [ ! -f $dir/$option ]
then
color_msg $blue "creating link $dir/$option"
ln $dir/dgraph $dir/$option
fi
#echo $dir $option
color_msg $blue "starting dgraph $option ..."
case $os in
Darwin)
open -a terminal.app $dir/$option
;;
# https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the
Linux)
#for terminal in gnome-terminal xterm konsole
#do
# which $terminal > /dev/null
# if [ $? -eq 0 ]
# then
# $terminal -e $dir/$option
# break
# fi
#done
nohup $dir/$option > /tmp/$option.log 2>&1 &
sleep 2
tail /tmp/$option.log
;;
*)
error "unsupported operating system $os"
esac
# wait a bit
sleep 2
done
;;
alpha) alpha;;
ratel) ratel;;
zero) zero;;
esac
fi
# commandline option
while [ "$1" != "" ]
do
option="$1"
case $option in
alpha) alpha;;
ratel) ratel;;
zero) zero;;
-p|--pull) pullImage;;
-b|--bash) bashInto;;
-k|--kill) stopImage;;
-h|--help) usage;;
esac
shift
done
In a shell script I need to know the value of a variable further down in the code, without running through it first.
This pings $IP which is extracted from $VAR below the while loop.
The $VAR is unknown at the time this is extracted (IP=$(echo $VAR | awk '{print $1}'))
Is it possible to read VAR in before the while loop runs?
The code:
#!/bin/sh
TIMEOUT=10
IP=$(echo $VAR | awk '{print $1}')
while [ $TIMEOUT -ne 0 ];do
ping -c 1 -W 1 "$IP" >/dev/null
rc=$?
if [ $rc -eq 0 ];then
TIMEOUT=0
else
TIMEOUT=$(($TIMEOUT - 1))
echo $TIMEOUT
sleep 1
fi
done
# rest of code to run after while loop
VAR="192.168.0.1 t,r 20,e"
I'm writing a init-script for a microservice and have the problem, that the PID of the process, that the program gives out (via echo) is not the process ID the process is having. The code:
#!/bin/bash
### BEGIN INIT INFO
# Provides: temp
# Description: temp
# required-start: $local_fs $remote_fs $network $syslog
# required-stop: $local_fs $remote_fs $network $syslog
# default-start: 3 5
# default-stop: 0 1 2 6
# chkconfig: 35 99 1
# description: Microservice init-script
### END INIT INFO
START_SCRIPT=${applicationDirectory}/script/start.sh
STOP_SCRIPT=${applicationDirectory}/script/stop.sh
PID_FILE=${runDirectory}/${microserviceName}_${environment}_${servicePort}
# ***********************************************
# ***********************************************
DAEMON=$START_SCRIPT
# colors
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[0;33m'
reset='\e[0m'
echoRed() { echo -e "${red}$1${reset}"; }
echoGreen() { echo -e "${green}$1${reset}"; }
echoYellow() { echo -e "${yellow}$1${reset}"; }
start() {
#PID=`bash ${START_SCRIPT} > /dev/null 2>&1 & echo $!`
PID=`$DAEMON $ARGS > /dev/null 2>&1 & echo $!`
}
stop() {
STOP_SCRIPT $1
}
case "$1" in
start)
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
if [ -z "`echo kill -0 ${PID}`" ]; then
echoYellow "Microservice is already running [$PID]."
exit 1
else
rm -f $PID_FILE
fi
fi
start
if [ -z $PID ]; then
echoRed "Failed starting microservice."
exit 3
else
echo $PID > $PID_FILE
echoGreen "Microservice successfully started [$PID]."
exit 0
fi
;;
status)
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
if [ ! -z "`echo kill -0 ${PID}`" ]; then
echoRed "Microservice is not running (process dead but pidfile exists)."
exit 1
else
echoGreen "Microservice is running [$PID]."
exit 0
fi
else
echoRed "Microservice is not running."
exit 3
fi
;;
stop)
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
if [ ! -z "`echo kill -0 ${PID}`" ]; then
echoRed "Microservice is not running (process dead but pidfile exists)."
exit 1
else
PID=`cat $PID_FILE`
stop $PID
echoGreen "Microservice successfully stopped [$PID]."
rm -f $PID_FILE
exit 0
fi
else
echoRed "Microservice is not running (pid not found)."
exit 3
fi
;;
*)
echo "Usage: $0 {status|start|stop}"
exit 1
esac
Now, the program gives for example 2505 as PID. But when I use
ps aux | grep trans | grep -v grep
It outputs a number, that is the previously outputted number +1.
Can anyone give a guess? Any help is appreciated!
Your PID variable gets the pid of the shell that executes start.sh. The actual program executed by the script gets a different pid.
#!/bin/bash
. /etc/init.d/functions
NAME=foo
DIR=/home/amit/Desktop
EXEC=foo.pl
PID_FILE=/var/run/foo.pid
IEXE=/etc/init.d/foo
RUN_AS=root
if [ ! -f $DIR/$EXEC ]
then
echo "$DIR/$EXEC not found."
exit
fi
case "$1" in
start)
echo -n "Starting $NAME"
cd $DIR
/home/amit/Desktop/foo.pl
echo "$NAME are now running."
;;
stop)
echo -n "Stopping $NAME"
kill -TERM `cat $PID_FILE`
rm $PID_FILE
echo "$NAME."
;;
force-reload|restart)
$0 stop
$0 start
;;
submit)
echo $2 >> /tmp/jobs
;;
*)
echo "Use: /etc/init.d/$NAME {start|stop|restart|force-reload}"
exit 1
;;
esac
exit 0
i have created a daemon with start and stop options(service foo start/stop) and it works fine. Now I want to send an input to the dameon. something like "service foo submit [argument]" . I want to to know - if user types "service foo submit alexander" , how alexander can be sent to the running daemon ?
If I get the question right - just as you already use positional var $1 you can supply the rest of the arguments to the command as well, i.e.:
"your_start_up_script" [switch to the script as $1] [arg 2 will be accessible via $2] [arg 3 will be accessible via $3]
Then inside script you do:
case "$1" in
start)
echo -n "Starting $NAME"
cd $DIR
/home/amit/Desktop/foo.pl "$2" "$3"