Click here to Skip to main content
15,885,978 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have created a C++ daemon in lunux ubuntu system. My daemon is implemented in such a way that the C++ executable is calling from a shell script in init.d. The daemon working fine on my system. But when I am stopping(sudo service mydaemon stop) the daemon it takes 10 more seconds to kill the daemon application. Any one know why this is taking more time to kill the daemon ?
Posted
Comments
Jochen Arndt 18-Aug-14 9:27am    
How did you call start-stop-daemon in your init script?

When using --retry=TERM/10/KILL/5 your daemon is probably not processing the SIGTERM signal.

Otherwise you can print out diagnostic information with time stamps from your daemon when it recives the signal to see where the time is consumed (e.g. by using syslog).
Arun Kumar K S 18-Aug-14 12:00pm    
DAEMON=/sbin/mydaemon
DAEMON_ARGS="--daemon"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started

#val=$(netstat -ano|grep "$SERVERPORT"|grep LISTEN)

#if [ "$val" != "" ]; then
# echo "Port $SERVERPORT is already used by another process. Please stop that process before running #the accutrac"
#exit 0
#fi
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2

}

#
# Function that stops the daemon/service
#
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
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
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"
}
Jochen Arndt 18-Aug-14 13:01pm    
The relevant part is '--retry=TERM/30/KILL/5'.
This means that there is a timeout of 30 seconds for the SIGTERM signal. If your process terminates before this time it handles the signal.

So you must debug your code to see which part consumes the time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900