modifying python daemon script, stop does not return OK (but does kill the process)

2024/10/12 0:28:28

Following on from the previous post, the script now start and stops the python script (and only that particular script) correctly but does not report the OK back to the screen...

USER="root"
APPNAME="myPythonApp1"
APPBIN="/usr/bin/python"
APPARGS="/usr/local/sbin/app1/app.py"
LOGFILE="/var/log/$APPNAME/error.log"
LOCKFILE="/var/lock/subsys/$APPNAME"LOGPATH=$(dirname $LOGFILE)prog=$APPBINstart() {[ -x $prog ] || exit 5[ -d $LOGPATH ] || mkdir $LOGPATH[ -f $LOGFILE ] || touch $LOGFILEecho -n $"Starting $APPNAME: "daemon --user=$USER "$APPBIN $APPARGS >>$LOGFILE &"RETVAL=$?echo[ $RETVAL -eq 0 ] && touch $LOCKFILEreturn $RETVAL
}stop() {echo -n $"Stopping $APPNAME: "pid=`ps -ef | grep "[p]ython $APPARGS" | awk '{ print $2 }'`echo $pidkill $pidsleep 1RETVAL=$?echo[ $RETVAL -eq 0 ] && rm -f $LOCKFILEreturn $RETVAL
}

Starting:

Starting indigolinkserver: [ OK ]

Stopping:

Stopping indigolinkserver:

Within the app.py I have added:

[...]
def set_exit_handler(func):signal.signal(signal.SIGTERM, func)
[...]if __name__ == '__main__':def on_exit(sig, func=None):#print "exit handler triggered"sys.exit(1)set_exit_handler(on_exit)

At command line I get the print (when uncommented) but within the daemon script I get nothing... something is not going back to RETVAL... is it fixable?

There is a post (thanks @robert) about being able only to use killproc with daemons to have this behaviour?

Thanks!

Answer

Couldn't get it to work with the exit handlers so I ended up doing it with .pid files instead...

USER="root"
APPNAME="myPythonApp1"
APPBIN="/usr/bin/python"
APPARGS="/usr/local/sbin/app1/app.py"
LOGFILE="/var/log/$APPNAME/error.log"
LOCKFILE="/var/lock/subsys/$APPNAME"LOGPATH=$(dirname $LOGFILE)prog=$APPBINstart() {[ -x $prog ] || exit 5[ -d $LOGPATH ] || mkdir $LOGPATH[ -f $LOGFILE ] || touch $LOGFILEecho -n $"Starting $APPNAME: "daemon --user=$USER "$APPBIN $APPARGS >>$LOGFILE &"RETVAL=$?echo[ $RETVAL -eq 0 ] && touch $LOCKFILEreturn $RETVAL
}stop() {echo -n $"Stopping $APPNAME: "pid=`ps -ef | grep "[p]ython $APPARGS" | awk '{ print $2 }'`killproc -p /var/run/$APPNAME.pidRETVAL=$?echo[ $RETVAL -eq 0 ] && rm -f $LOCKFILEreturn $RETVAL
}

and within the python code:

if __name__ == '__main__':pid = str(os.getpid())pidfile = "/var/run/myPythonApp1.pid"if os.path.isfile(pidfile):print "%s already exists" % pidfile#sys.exit()else:file(pidfile, 'w').write(pid)
https://en.xdnf.cn/q/118263.html

Related Q&A

fulfill an empty dataframe with common index values from another Daframe

I have a daframe with a series of period 1 month and frequency one second.The problem the time step between records is not always 1 second.time c1 c2 2013-01-01 00:00:01 5 3 2013-01-0…

How to mix numpy slices to list of indices?

I have a numpy.array, called grid, with shape:grid.shape = [N, M_1, M_2, ..., M_N]The values of N, M_1, M_2, ..., M_N are known only after initialization.For this example, lets say N=3 and M_1 = 20, M_…

Visualize strengths and weaknesses of a sample from pre-trained model

Lets say Im trying to predict an apartment price. So, I have a lot of labeled data, where on each apartment I have features that could affect the price like:city street floor year built socioeconomic s…

Scrapy get result in shell but not in script

one topic again ^^ Based on recommendations here, Ive implemented my bot the following and tested it all in shell :name_list = response.css("h2.label.title::text").extract()packaging_list = r…

How to find a source when a website uses javascript

What I want to achieve I am trying to scrape the website below using Beautiful-soup and when I load the page it does not give the table that shows various quotes. In my previous posts folks have helped…

How to print a list of dicts as an aligned table?

So after going through multiple questions regarding the alignment using format specifiers I still cant figure out why the numerical data gets printed to stdout in a wavy fashion.def create_data(soup_ob…

abstract classes in python: Enforcing type

My question is related to this question Is enforcing an abstract method implementation unpythonic? . I am using abstract classes in python but I realize that there is nothing that stops the user from …

Convert image array to original svs format

Im trying to apply a foreground extraction to a SVS image (Whole Slide Image) usign OpenSlide library.First, I converted my image to an array to work on my foreground extraction:image = np.asarray(oslI…

Printing bytestring via variable

I have the following Unicode text stored in variable:myvariable = Gen\xe8veWhat I want to do is to print myvariable and show this:GenveI tried this but failed:print myvariable.decode(utf-8)Whats the ri…

Loop and arrays of strings in python

I have the following data set:column1HL111 PG3939HL11 HL339PG RC--HL--PGI am attempting to write a function that does the following:Loop through each row of column1 Pull only the alphabet and put into…