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
Related
I'm trying to start a service over ssh in a bash script that I just call from the command line. I can do the following commands: (I have ssh keys in place so I don't need to put a p/w in)
ssh -t -t user#server 'sudo /sbin/service test stop' -- Works fine, stops the service
ssh -t -t user#server 'sudo /sbin/service test status' -- Works fine, status's the service
ssh -t -t user#server 'sudo /sbin/service test start' -- Doesn't start the service ???
This is with a custom init.d script running on centos 6.7
Does anyone have any ideas?
or see what I might be missing?
Thanks!
EDIT: Here is the init.d script:
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: test
# Required-Start: networking
# Required-Stop: networking
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description:
### END INIT INFO
#
###source function library
source /etc/init.d/functions
APPNAME="test"
APPDIR="/tmp"
CONFIGDIR="/tmp/config"
LOCKFILE=/var/lock/subsys/$APPNAME
PIDFILE="/var/run/$APPNAME.pid"
###Declare variables for test
CONFIG="test.config"
start() {
echo -n "Starting $APPNAME: "
source "${CONFIGDIR}/source.txt"
daemon --pidfile="$PIDFILE" "/tmp/${APPNAME} ${CONFIGDIR}/${CONFIG} >> /tmp/console.log 2>&1 &"
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch $LOCKFILE
pidof $APPNAME > $PIDFILE
}
return $RETVAL
}
stop() {
echo -n $"Stopping $APPNAME:"
killproc -p "$PIDFILE" $APPNAME
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE $PIDFILE
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p "$PIDFILE" "$APPNAME"
;;
restart)
stop
start
;;
*)
echo "Usage $prg {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
I found a work around:
ssh -t -t user#server 'sudo bash -s' < '/tmp/start_test.sh'
and then on the server:
/tmp/start_test.sh
#!/bin/bash
sudo service test start >> /tmp/start.log
exit 0
I tested this one successfully:
echo "sudo service test start && exit" | python -c 'import pty, sys; pty.spawn(sys.argv[1:])' ssh user#server
I had created a deamon in centos 7 to send an email from email queue.
Email queue is implemented in Yii1. That works fine but when I tried to create and run daemon in server but it fails and shows an error :
echo "Error! Could not start MyStaging!"
I am following instructions which I found here.
On inspection I found that PID gets the value 1232 and pgrep -u $RUNAS -f $NAME > /dev/null command returns empty.
Below is my script:
#!/bin/bash
### BEGIN INIT INFO
# Provides: MyStaging
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: This service having purpose to send email from email queue where relevant email push by particular module operation in queue.
### END INIT INFO
SCRIPT="/usr/bin/php5 /home/my/public_html/staging/protected/yiic mailqueue run"
RUNAS=root
NAME=MyStagingMailQueue
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log
start() {
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service ' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
# su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
# Try with this command line instead of above if not workable
su -c "$CMD" $RUNAS > "$PIDFILE"
sleep 2
PID=$(cat $PIDFILE)
if pgrep -u $RUNAS -f $NAME > /dev/null
then
echo "$NAME is now running, the PID is $PID"
else
echo "Error! Could not start $NAME!"
fi
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service ' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
status() {
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
I don't know what is the issue. Please help me sort it out.
Since you're using CentOS 7, it's better to create a systemd service.
Step1:
Create a service file for your service say yiicmail.service. Do :
sudo touch /etc/systemd/system/yiicmail.service
Step2:
Open the the above file with your favourite editor and put the below content in it:
[Unit]
Description=yiic service
After=network.target
[Service]
Type=simple
User=root
ExecStart="/usr/bin/php5 /home/whizbite/public_html/staging/protected/yiic mailqueue run"
#If there are spaces, I would strings within quotes like above
Restart=on-abort
[Install]
WantedBy=multi-user.target
Step3:
Now it is the time to play with the service. To start it, do :
sudo systemctl start yiicmail # You don't have to type full name, that is, yiicmail.service
To check the status, do :
sudo systemctl status yiicmail
To stop it, do :
sudo systemctl stop yiicmail
To start the service at boot, do :
sudo systemctl enable yiicmail
To disable the service at boot, do:
sudo systemctl disable yiicmail
Hope this helps.
I created this init.d script for unicorn according to this digitalocean tutorial.
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn app server
# Description: starts unicorn using start-stop-daemon
### END INIT INFO
set -e
USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
# app settings
USER="deploy"
APP_NAME="appname"
APP_ROOT="/home/$USER/$APP_NAME"
ENV="production"
# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
# make sure the app exists
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting $APP_NAME"
su - $USER -c "$CMD"
;;
stop)
echo "Stopping $APP_NAME"
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
echo "Force stopping $APP_NAME"
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload|upgrade)
sig USR2 && echo "reloaded $APP_NAME" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
Now (the script's name is uicorn_app)
sudo update-rc.d `unicorn_app` defaults
works. But whenever i try
$ sudo service unicorn_app start
Starting app
-su bundle: command not found
However i am able to stop it via
$ sudo service unicorn_app stop
after i started it manually with
RAILS_ENV=production rails s -b ip.ip.ip.ip
I installed ruby on rails on /etc/local via rbenv and the PATH
first entries redirect to the proper directories:
/usr/local/rbenv/shims
/usr/lcoal/rbenv/bin
What do i need to change that my scripts finds bundle? Since i think the PATH is correct what else could go wrong? Thanks in advance for help!
OK the solution was to do with the rbenv installation. I needed to add the lines PATH and RBENV_ROOT to my ~/.bash_profile. After adding them there i was able to start unicorn via sudo service unicorn_app start
I have installed mumudvb manually based on instructions in here
https://github.com/braice/MuMuDVB
to get the starting scripts working i executed these commands
cp scripts/debian/etc/default/mumudvb /etc/default/mumudvb
cp scripts/debian/etc/init.d/mumudvb /etc/init.d/mumudvb
/etc/default/mumudvb :
#Mumudvb init config file
#
# This file is used to specify the locations of mumudvb config files for each card
#
#If you don't want to automatically start mumudvb, uncomment this line
#DONTSTARTMUMU=true
#If you want to launch a command before mumudvb (for example for automatic configuration generation)
#LAUNCH_BEFORE_MUMU=""
#Options for mumudvb
DAEMON_OPTS=""
#The user to launch mumudvb
DAEMONUSER="username"
#Change this line to reflect your configuration
#Ex : ADAPTERS="0 1 2 4"
ADAPTERS="0"
#Location of the config files
#Ex : MUMUDVB_CONF_1="/etc/mumudvb/card1.conf"
MUMUDVB_CONF_0="/etc/mumudvb/card0.conf"
this is the init.d/mumudvb script :
#!/bin/sh
### BEGIN INIT INFO
# Provides: mumudvb
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mumudvb
# Description: Digital television streaming program
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/mumudvb
PIDDIR=/var/run/mumudvb
DEFAULT_FILE=/etc/default/mumudvb
NAME=mumudvb
DESC="television streaming program"
#Reading of the config file
if [ -f "$DEFAULT_FILE" ] ; then
. "$DEFAULT_FILE"
fi
if [ "$DONTSTARTMUMU" = "true" ]; then exit 0; fi
. /lib/lsb/init-functions
test -x $DAEMON || exit 5
set -e
do_start() {
if [ ! -d $PIDDIR ]; then
mkdir -p $PIDDIR
fi
chown $DAEMONUSER $PIDDIR
if [ -x "$LAUNCH_BEFORE_MUMU" ]; then
log_daemon_msg "Launching pre script ..."
eval $LAUNCH_BEFORE_MUMU
log_daemon_msg "Done."
fi
for ADAPTER in $ADAPTERS; do
#Todo : fails if all card fails
log_daemon_msg " Starting card $ADAPTER"
eval CONFIG_FILE = MUMUDVB_CONF_0
if [ ! -f $CONFIG_FILE ]; then
log_warning_msg " Card $ADAPTER: Config file $CONFIG_FILE not found."
else
start-stop-daemon --start --oknodo --name mumudvb_$ADAPTER\
--make-pidfile --pidfile=$PIDDIR/mumudvb_init_$ADTAPTER.pid\
--chuid $DAEMONUSER --exec $DAEMON -- $DAEMON_OPTS --card $ADAPTER -c $CONFIG_FILE
fi
done
}
do_stop() {
for PIDFILE in `ls $PIDDIR/mumudvb_init_*.pid 2> /dev/null`; do
start-stop-daemon --stop --oknodo --pidfile "$PIDFILE" \
--exec $DAEMON
done
}
case "$1" in
start)
if [ ! -f "$DEFAULT_FILE" ]; then
log_failure_msg "$DEFAULT_FILE not found, Can't start $NAME"
exit 6
fi
log_daemon_msg "Starting $DESC: $NAME"
do_start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC: $NAME"
do_stop
log_end_msg $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC: $NAME"
do_stop
sleep 1
do_start
log_end_msg $?
;;
status)
status_of_proc "$DAEMON" "$DESC: $NAME" && exit 0 || exit $?
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
when i run :
sudo service mumudvb status
or
sudo service mumudvb start
nothing returns ...
shouldn't it at least return an error or something ?
**note: runing
mumudvb -c /etc/mumudvb/card0.conf
works fine**
in the file /etc/default/mumudvb set DONTSTARTMUMU to false
DONTSTARTMUMU=false
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