start-stop-daemon doesn't detect already running daemon - sh

I tried to copy /etc/init.d/skeleton and modify it to start a program in background. I came up with the following:
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE -b --make-pidfile --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE -b --make-pidfile --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
}
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
I have a problem and several questions about this. My problem is, that the start function starts a daemon and returns 0 regardless of wether the daemon was already running. My question is if -b and --make-pidfile is required at the first --test start. And am I right with the assumption that the first command in do_stop sends a TERM signal to the daemon and the second command kills the daemon forcefully? In this case I would have to wait on the daemon to quit.

My problem was --exec because my script is an interpreted script start-stop-daemon couldn't find a program with this name running since the interpreter was only running. I fixed this by using --startas except for --exec.

Related

nginx configuration file throws error while gridfs configure?

I have installed nginx-gridfs and followed https://github.com/mdirolf/nginx-gridfs.
Nginx installed successfully.
In nginx.conf file I have specified as below:
server{
location /gridfs/ {
gridfs my_app;
}
}
It throws an error gridfs is unknown directive.
Restarting nginx: nginx: [emerg] unknown directive "gridfs" in /etc/nginx/nginx.conf:69
nginx: configuration file /etc/nginx/nginx.conf test failed
Any one suggest solution.
Thanks,
Prasad.
While compiling nginx from its source code and then installing it using make install it gets installed in /usr/local/nginx instead of the /etc/nginx so the path and daemon created needs to be changed in the nginx scipt(which you have to create manually and link it since compiling from source code won't create its own).
I assume you've already installed nginx from its source code adding the gridfs module.
Now unfortunately, you can't start or stop the service of nginx using the script in /etc/init.d/nginx because installing from the source code won't create a script(if found in /etc/init.d/, then this script is previously generated while installing nginx using package manager)
Then check your nginx status. If it's running then use:
sudo kill `cat /usr/local/nginx/logs/nginx.pid`
then go ahead and create a script:
sudo nano /etc/init.d/nginx
Inside the bank file use this code snippet:
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/nginx/sbin
#DAEMON=/usr/sbin/nginx
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
. /lib/lsb/init-functions
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
sleep 1
start-stop-daemon --start --quiet --pidfile \
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
status)
status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
exit 1
;;
esac
exit 0
Then make the file executable:
sudo chmod +x /etc/init.d/nginx
Now add the script to the default run levels:
sudo /usr/sbin/update-rc.d -f nginx defaults
That's it. You are done! You can now use the script to start, stop and restart nginx:
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart
Follow this link if you need more help: 1
At the bottom of the page you'll find the link to create the nginx script manually.

Where should I add the --rest option for MongoDB?

I need to use mongodb with the --rest option. But mongodb is started automatically on boot, so I guess I need to modify a file or something.
Where can I add this --rest option?
I have this file at /etc/init/mongodb.conf, not sure what to edit:
# Ubuntu upstart file at /etc/init/mongodb.conf
limit nofile 20000 20000
kill timeout 300 # wait 300s between SIGTERM and SIGKILL.
pre-start script
mkdir -p /var/lib/mongodb/
mkdir -p /var/log/mongodb/
end script
start on runlevel [2345]
stop on runlevel [06]
script
ENABLE_MONGODB="yes"
if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi
if [ "x$ENABLE_MONGODB" = "xyes" ]; then exec start-stop-daemon --start --quiet --chuid mongodb --exec /usr/bin/mongod -- --config /etc/mongodb.conf; fi
end script
And this file at /etc/init.d/mongodb:
#!/bin/sh -e
# upstart-job
#
# Symlink target for initscripts that have been converted to Upstart.
set -e
INITSCRIPT="$(basename "$0")"
JOB="${INITSCRIPT%.sh}"
if [ "$JOB" = "upstart-job" ]; then
if [ -z "$1" ]; then
echo "Usage: upstart-job JOB COMMAND" 1>&2
exit 1
fi
JOB="$1"
INITSCRIPT="$1"
shift
else
if [ -z "$1" ]; then
echo "Usage: $0 COMMAND" 1>&2
exit 1
fi
fi
COMMAND="$1"
shift
if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
ECHO=echo
else
ECHO=:
fi
$ECHO "Rather than invoking init scripts through /etc/init.d, use the service(8)"
$ECHO "utility, e.g. service $INITSCRIPT $COMMAND"
# Only check if jobs are disabled if the currently _running_ version of
# Upstart (which may be older than the latest _installed_ version)
# supports such a query.
#
# This check is necessary to handle the scenario when upgrading from a
# release without the 'show-config' command (introduced in
# Upstart for Ubuntu version 0.9.7) since without this check, all
# installed packages with associated Upstart jobs would be considered
# disabled.
#
# Once Upstart can maintain state on re-exec, this change can be
# dropped (since the currently running version of Upstart will always
# match the latest installed version).
UPSTART_VERSION_RUNNING=$(initctl version|awk '{print $3}'|tr -d ')')
if dpkg --compare-versions "$UPSTART_VERSION_RUNNING" ge 0.9.7
then
initctl show-config -e "$JOB"|grep -q '^ start on' || DISABLED=1
fi
case $COMMAND in
status)
$ECHO
$ECHO "Since the script you are attempting to invoke has been converted to an"
$ECHO "Upstart job, you may also use the $COMMAND(8) utility, e.g. $COMMAND $JOB"
$COMMAND "$JOB"
;;
start|stop)
$ECHO
$ECHO "Since the script you are attempting to invoke has been converted to an"
$ECHO "Upstart job, you may also use the $COMMAND(8) utility, e.g. $COMMAND $JOB"
if status "$JOB" 2>/dev/null | grep -q ' start/'; then
RUNNING=1
fi
if [ -z "$RUNNING" ] && [ "$COMMAND" = "stop" ]; then
exit 0
elif [ -n "$RUNNING" ] && [ "$COMMAND" = "start" ]; then
exit 0
elif [ -n "$DISABLED" ] && [ "$COMMAND" = "start" ]; then
exit 0
fi
$COMMAND "$JOB"
;;
restart)
$ECHO
$ECHO "Since the script you are attempting to invoke has been converted to an"
$ECHO "Upstart job, you may also use the stop(8) and then start(8) utilities,"
$ECHO "e.g. stop $JOB ; start $JOB. The restart(8) utility is also available."
if status "$JOB" 2>/dev/null | grep -q ' start/'; then
RUNNING=1
fi
if [ -n "$RUNNING" ] ; then
stop "$JOB"
fi
# If the job is disabled and is not currently running, the job is
# not restarted. However, if the job is disabled but has been forced into the
# running state, we *do* stop and restart it since this is expected behaviour
# for the admin who forced the start.
if [ -n "$DISABLED" ] && [ -z "$RUNNING" ]; then
exit 0
fi
start "$JOB"
;;
reload|force-reload)
$ECHO
$ECHO "Since the script you are attempting to invoke has been converted to an"
$ECHO "Upstart job, you may also use the reload(8) utility, e.g. reload $JOB"
reload "$JOB"
;;
*)
$ECHO
$ECHO "The script you are attempting to invoke has been converted to an Upstart" 1>&2
$ECHO "job, but $COMMAND is not supported for Upstart jobs." 1>&2
exit 1
esac
It's probably cleaner to enable the REST interface via /etc/mongodb.conf by adding a line of:
rest = true
That setting is documented here.
MongoDB version 2.6 has switched to a YAML config file. The following two entries are required to prevent the following startup warning:
mongodb WARNING: --rest is specified without --httpinterface
net:
http:
enabled: true
RESTInterfaceEnabled: true
When u start the server using command mongod , add --rest option with command mongod like this mongod --rest.
refer mongod - MongoDB Manual 2.6.
After run command complete , u can use the following the simple Restful API:
http://127.0.0.1:28017/databaseName/collectionName/
Here is simple RestFul API Doc.
Just start the server using mongod --rest
Note: By default, the rest API's are inaccessible due to security issues. The web interface is accessible at localhost:<port>, where the number is 1000 more than the mongod port. For example, your mongodb server is running at 27017 (by default) then you can access mongodb at
http://127.0.0.1:28017/<db-name>/<collection-name>/

Mongodb is not starting in ubuntu

I am having a weird problem with mongodb after installation it is ending with a message
invoke-rc.d: unknown initscript, /etc/init.d/mongodb not found.
dpkg: error processing mongodb-10gen (--configure):
What is wrong here I followed the steps given here: http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages
The issue is that you are trying to install a version packaged for Upstart init services, but Debian Squeeze still uses SysV init by default.
There is a note on this in the install docs:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux/#installing-mongodb
If you are using Debian or Ubuntu that uses SysV style init process, use the following line:
deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen
This means that you need to create mongodb start script in /etc/init.d/
Try this script
#!/bin/bash
#
# mongodb Startup script for the mongodb server
#
# chkconfig: - 64 36
# description: MongoDB Database Server
#
# processname: mongodb
#
# Source function library
. /lib/lsb/init-functions
if [ -f /etc/sysconfig/mongodb ]; then
. /etc/sysconfig/mongodb
fi
prog="mongod"
mongod="/usr/local/mongodb/bin/mongod"
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon $mongod "--fork --logpath /var/log/mongodb.log --logappend 2>&1 >>/var/log/mongodb.log"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
killproc $prog -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
reload)
reload
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|reload|status}"
RETVAL=1
esac
exit $RETVAL
after type in terminal:
sudo chmod +x /etc/init.d/mongodb
sudo /etc/init.d/mongodb start
ps -A | grep mongod

Best init script for running an application as a separate user

I have an application that runs in a user account (Plack-based) and want an init script.
It seems as easy as "sudo $user start_server ...". I just wrote an LSB script using start-stop-daemon and it is really clumsy and verbose. It doesn't feel like the right way.
After scouring for a bit and looking at a log of examples, I'm still not sure what the best way to do this is and there isn't a cohesive guide that I've found.
Right now I have it working with:
start-stop-daemon --background --quiet --start --pidfile $PIDFILE \
--make-pidfile --chuid $DAEMONUSER \
--exec $DAEMON -- $DAEMON_OPTS
With DAEMON and DAEMON_OPTS as:
DAEMON="/home/mediamogul/perl5/perlbrew/perls/current/bin/start_server"
DAEMON_OPTS="--port $PORT -- starman --workers $WORKERS /home/mediamogul/MediaMogul/script/mediamogul.psgi"
This then requires me to adjust how to detect running, because it's a perl script so perl is showing up as the command and not "start_server".
(I'm running this out of a perlbrew on that user account so it is completely separate from the system perl, that's why the paths are pointing to a perl in the user dir)
Is this really the best way to go about doing this? It seems very clunky to me, but I'm not an admin type.
You can use the --pid option to starman to have it write the PID when the app starts, if you use the same filename as you give start-stop-daemon then it will work nicly.
For example, from one of my init.d scripts:
SITENAME=mysite
PORT=5000
DIR=/websites/mysite
SCRIPT=bin/app.pl
USER=davidp
PIDFILE=/var/run/site-$SITENAME.pid
case "$1" in
start)
start-stop-daemon --start --chuid $USER --chdir $DIR \
--pidfile=$PIDFILE \
--exec /usr/local/bin/starman -- -p $PORT $SCRIPT -D --pid $PIDFILE
;;
stop)
start-stop-daemon --stop --pidfile $PIDFILE
;;
*)
echo "Usage: $SCRIPTNAME {start|stop}" >&2
exit 3
;;
esac
It's very close to what you are already doing, and I'll admit it is a little clumsy, granted, but it works - having Starman write the PID file means that start-stop-daemon can reliably start & stop it.

init.d celery script for CentOS?

I'm writing a Django app that uses celery. So far I've been running on Ubuntu, but I'm trying to deploy to CentOS.
Celery comes with a nice init.d script for Debian-based distributions, but it doesn't work on RedHat-based distributions like CentOS because it uses start-stop-daemon. Does anybody have an equivalent one for RedHat that uses the same variable conventions so I can reuse my /etc/default/celeryd file?
Is better solved here:
Celery CentOS init script
You should be good using that one
Since I didn't get an answer, I tried to roll my own:
#!/bin/sh
#
# chkconfig: 345 99 15
# description: celery init.d script
# Defines the following variables
# CELERYD_CHDIR
# DJANGO_SETTINGS_MODULE
# CELERYD
# CELERYD_USER
# CELERYD_GROUP
# CELERYD_LOG_FILE
CELERYD_PIDFILE=/var/run/celery.pid
if test -f /etc/default/celeryd; then
. /etc/default/celeryd
fi
# Source function library.
. /etc/init.d/functions
# Celery options
CELERYD_OPTS="$CELERYD_OPTS -f $CELERYD_LOG_FILE -l $CELERYD_LOG_LEVEL"
if [ -n "$2" ]; then
CELERYD_OPTS="$CELERYD_OPTS $2"
fi
start () {
cd $CELERYD_CHDIR
daemon --user $CELERYD_USER --pidfile $CELERYD_PIDFILE $CELERYD $CELERYD_OPTS &
}
stop () {
if [[ -s $CELERYD_PIDFILE ]] ; then
echo "Stopping Celery"
killproc -p $CELERYD_PIDFILE python
echo "done!"
rm -f $CELERYD_PIDFILE
else
echo "Celery not running."
fi
}
check_status() {
status -p $CELERYD_PIDFILE python
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
check_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac