1

I know what the problem is but I am unsure what a good solution is.

So, I have a uwsgi process that I am using for Graphite to work with Nginx. Here is some background information:

I installed uwsgi from pip and I run uwsgi with the -configfile (.ini):

[uwsgi]
processes = 1
socket = 127.0.0.1:3031
gid = root
uid = root
chdir = /opt/graphite/conf
daemonize = /var/log/graphite/uwsgi.log
#pidfile = /var/run/uwsgi.pid
module = wsgi:application

I have an init script that does the following (I found it on the internet AND this is not done):

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/opt/graphite/conf/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

start() {
    if [ -f "$PIDFILE" ];then
       echo "${NAME} Service already running" >&2
       return 1
    fi
    echo "Starting $NAME" >&2
    $DAEMON $CONFIGFILE || echo -n "uwsgi already running"     
}

stop() {
    $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
    rm -f $PIDFILE
    echo "$DAEMON STOPPED."
}

Here is the situation: if I have the .ini file create the PID - it comes up with process ID that is not even close to what the actual PID is. The way it works, as from what I found is that uwsgi starts and then picks up the config file and daemonizes the process. So, if I were to use something along the lines of

ps -ef |awk '/[u]wsgi/{print $2}' > $PIDFILE

it will have 2 process IDs. e.g.

cat /opt/uwsgi/uwsgi.ini
4121
4141

Now the second PID # is the actual running process.

How can I use the awk command to grab the PID but then cut it so that I only touch the PIDFILE with the second #?

I am not sure what command to use. Any ideas?

E.Z.
  • 139
  • 2
  • 14
  • 1
    There is no guarantee that it will always be the second. I assume the parent will exit and the child will persist, but it could be the other way around. Anyway, examine the parents and then decide which one to capture. – tripleee Aug 19 '15 at 16:30

1 Answers1

1

I wasn't searching for the right keyword or something but I found the answer here: How to use SED to print a specific line

So, what I did was added | sed -n '2p' to my init script and I got the script to input the correct PID into the /var/run/.pid process.

My init script is as follows:

#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/opt/graphite/conf/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

start() {
    if [ -f "$PIDFILE" ];then
       echo "${NAME} Service already running" >&2
       return 1
    fi
    echo "Starting $NAME" >&2
    $DAEMON $CONFIGFILE || echo -n "uwsgi already running"
    sleep 2
    ps -ef |awk '/[u]wsgi/{print $2}'| sed -n '2p' > $PIDFILE
}

stop() {
    if [ ! -f "$PIDFILE" ]; then
       echo "${NAME} Service not running" >&2
       return 1
    fi
    $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
    rm -f $PIDFILE
    echo "$DAEMON STOPPED."
}

status() {
    ps aux |grep $DAEMON |sed -n '1p'
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  restart)
    stop
    sleep 1
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
esac
Community
  • 1
  • 1
E.Z.
  • 139
  • 2
  • 14
  • 1
    This has the obvious problem that you are hard-coding the second instance, which may not always be correct. See my comment for a hopefully more robust approach. – tripleee Aug 20 '15 at 04:25
  • 1
    Triplee - took me a bit to wrap my head around this but you're right and thank you for your assistance! – E.Z. Sep 23 '15 at 16:25