I'm trying to write init.d script to run solr as daemon, but unfortunately ps aux shows that there is no such process.
Here is the code:
#!/bin/sh
start_path=/opt/solr/example/start.jar
JAVA_PATH=/usr/bin/java
PID=/tmp/.solr/pid
ARGS="-jar $start_path"
if [ ! -d /tmp/.solr ]
then
mkdir /tmp/.solr
fi
start(){
echo -n "Starting solr..."
start-stop-daemon --start --background --name "solr" --make-pidfile --pidfile $PID --exec ${JAVA_PATH} -- ${ARGS}
RETVAL="$?"
if [ "$RETVAL" = 0 ]
then
echo "done."
else
echo "failed. See error code for more information."
fi
return $RETVAL
}
case "$1" in
start)
start
;;
*)
echo $"Usage: solr {start}"
exit 3
;;
esac
exit $RETVAL
How about using the command
java jar start.jar & disown
This would start the process and move it to the background...
Hope I helped!
Well, thanks to this blog post, I finally made it.
Here's how it looks:
#!/bin/sh
PIDFILE=/tmp/.solr/pid
if [ ! -d /tmp/.solr ]
then
mkdir /tmp/.solr
fi
cd /opt/solr/example
start(){
if [ ! -f $PIDFILE ]
then
echo "Starting solr..."
nohup java -jar start.jar &
echo $! > $PIDFILE
RETVAL=$?
if [ "$RETVAL" = 0 ]
then
echo "Done."
else
echo "Failed. See error code for more information."
fi
return $RETVAL
echo $! > $PIDFILE
else
echo "Solr is already running"
return 1
fi
}
stop(){
if [ ! -f $PIDFILE ]
then
echo "Solr is not running"
return 1
else
PID=$(cat $PIDFILE)
echo "Stopping solr..."
kill $PID
RETVAL=$?
if [ "$RETVAL" = 0 ]
then
echo "Solr stopped."
rm $PIDFILE
else
echo "Can't stop Solr."
fi
return $RETVAL
fi
}
restart(){
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: solr {start|stop|restart}"
exit 3
;;
esac
exit $RETVAL
Related
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
Im trying to start kibana 4 as a service in ubuntu 12.04. Please any one help on how to set as service .
I referred these links to write the script , but it wont work.
https://github.com/akabdog/scripts/blob/master/kibana4_init
https://github.com/chovy/node-startup/blob/master/init.d/node-app
Below code is worked for me.
#!/bin/sh
KIBANA_EXEC="/opt/kibana/bin/kibana"
LOG_FILE="/var/log/kibana/.log"
PID_FILE="/var/run/kibana.pid"
PID_DIR="$APP_DIR/pid"
LOG_DIR="$APP_DIR/log"
USAGE="Usage: $0 {start|stop|restart|status} [--force]"
FORCE_OP=false
start_it() {
mkdir -p "$PID_DIR"
mkdir -p "$LOG_DIR"
sleep 10
echo "Starting Kibana..."
$KIBANA_EXEC 1>"$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "Kibana started with pid $!"
}
pid_file_exists() {
[ -f "$PID_FILE" ]
}
get_pid() {
echo "$(cat "$PID_FILE")"
}
is_running() {
PID=$(get_pid)
! [ -z "$(ps aux | awk '{print $2}' | grep "^$PID$")" ]
}
remove_pid_file() {
echo "Removing pid file"
rm -f "$PID_FILE"
}
start_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "Kibana already running with pid $PID"
exit 1
else
echo "Kibana stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing start anyways"
remove_pid_file
start_it
fi
fi
else
start_it
fi
}
stop_process() {
PID=$(get_pid)
echo "Killing process $PID"
kill $PID
}
stop_app() {
if pid_file_exists
then
if is_running
then
echo "Stopping kibana ..."
stop_process
remove_pid_file
echo "Kibana stopped"
else
echo "Kibana already stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing stop anyways ..."
remove_pid_file
echo "Kibana stopped"
else
exit 1
fi
fi
else
echo "Kibana already stopped, pid file does not exist"
exit 1
fi
}
status_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "Kibana running with pid $PID"
else
echo "Kibana stopped, but pid file exists"
fi
else
echo "Kibana stopped"
fi
}
case "$2" in
--force)
FORCE_OP=true
;;
"")
;;
*)
echo $USAGE
exit 1
;;
esac
case "$1" in
start)
start_app
;;
stop)
stop_app
;;
restart)
stop_app
start_app
;;
status)
status_app
;;
*)
echo $USAGE
exit 1
;;
esac
Small error with the script.In the LogFile path you need to specify at the end .log
There seems to be a small error in the init script. The PID_FILE varible uses the $NAME varible but NAME isn't defined until later. Move the NAME variable before the PID_FILE variable.
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
supervise is one of the powerful tool in daemontools, I wonder how to implement it using bash script. Anyone has suggestions? I need help!
supervise performs a number of tasks, and interoperates with svscan, svcok, svstat.
A fully-featured implementation in bash would be non-trivial, but a daemon-restarting script is a fairly straight-forward task.
#!/bin/bash
DAEMON=/usr/sbin/whatever # "/bin/sleep" for demo
DAEMON_ARGS="xxx" # "15" for sleep demo
case "$1" in
start)
echo "Starting $DAEMON"
(
trap 'logger -i -p daemon.info "INFO: $DAEMON shutting down..."; exit 1' 1 2 3 15
logger -i -p daemon.info "INFO: Starting $DAEMON"
while : ; do
$DAEMON $DAEMON_ARGS &
pid=$!
echo $pid > /var/run/mydaemon.pid
wait $pid
rc=$?
logger -i -p daemon.warn "WARNING: $DAEMON exited, rc=$rc"
sleep 1 # adjust as required
logger -i -p daemon.warn "WARNING: Restarting $DAEMON"
done
) &
echo $! > /var/run/myscript.pid
;;
stop)
echo "Stopping $DAEMON"
[ -f /var/run/myscript.pid ] && kill $(</var/run/myscript.pid)
[ -f /var/run/mydaemon.pid ] && kill $(</var/run/mydaemon.pid)
;;
esac
The above has pretty much no error handling, doesn't properly do real daemon things like chdir() and close unused FDs, but it does log via logger/syslog so you can see what it's doing. It assumes $DAEMON does not fork into the background itself (as does supervise).
You haven't stated your platform, but if you want something really, really simple, inittab may do the trick, see how to use inittab to auto-restart a PHP programme? for some tips. Otherwise we're veering out of Stack Overflow territory, so check out https://unix.stackexchange.com/ .
I changed the code provided by mr.spuratic around a little bit:
echo "true" > $STATUSFILE
case "$1" in
start)
echo "Starting $DAEMON_NAME"
(
trap 'logger -t italoService "INFO: $DAEMON_NAME shutting down..."; exit 1' 1 2 3 15
logger -t italoService "INFO: Starting $DAEMON_NAME"
while : ; do
#do_start
read STATUS <$STATUSFILE
if [ "$STATUS" = "false" ] ; then
logger -t italoService "INFO: $DAEMON_NAME stopped by user"
break
fi
$DAEMON $DAEMON_OPTS &
pid=$!
echo $pid > $PIDFILE
logger -t italoService "WARNING: pid File: $pid"
wait $pid
rc=$?
logger -t italoService "WARNING: $DAEMON_NAME exited, rc=$rc"
sleep 1 # adjust as required
read STATUS <$STATUSFILE
if [ "$STATUS" = "true" ] ; then
logger -t italoService "WARNING: Restarting $DAEMON_NAME"
fi
done
) &
echo $! > $PIDFILE
;;
stop)
echo "Stopping $DAEMON_NAME"
echo "false" > $STATUSFILE
[ -f $PIDFILE ] && kill $(<$PIDFILE)
[ -f $PIDFILE ] && kill $(<$PIDFILE)
;;
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