bash - How to create PID for forked process in init script? -
i know problem unsure solution is.
so, have uwsgi process using graphite work nginx. here background information:
i installed uwsgi pip , run uwsgi -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 init script following (i found on internet , 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 running" >&2 return 1 fi echo "starting $name" >&2 $daemon $configfile || echo -n "uwsgi running" } stop() { $daemon --stop $pidfile || echo -n "uwsgi not running" rm -f $pidfile echo "$daemon stopped." }
here situation: if have .ini file create pid - comes process id not close actual pid is. way works, found uwsgi starts , picks config file , daemonizes process. so, if use along lines of
ps -ef |awk '/[u]wsgi/{print $2}' > $pidfile
it have 2 process ids. e.g.
cat /opt/uwsgi/uwsgi.ini 4121 4141
now second pid # actual running process.
how can use awk command grab pid cut touch pidfile second #?
i not sure command use. ideas?
i wasn't searching right keyword or found answer here: how use sed print specific line
so, did added | sed -n '2p' init script , got script input correct pid /var/run/.pid process.
my init script 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 running" >&2 return 1 fi echo "starting $name" >&2 $daemon $configfile || echo -n "uwsgi running" sleep 2 ps -ef |awk '/[u]wsgi/{print $2}'| sed -n '2p' > $pidfile } stop() { if [ ! -f "$pidfile" ]; 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
Comments
Post a Comment