Skip to content

Commit

Permalink
svps: added start/stop all services
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhangui committed Jul 1, 2024
1 parent c6fefa8 commit 19c522c
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 50 deletions.
6 changes: 4 additions & 2 deletions daemontools-x/daemontools.spec.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
#
# $Id: daemontools.spec.in,v 1.47 2024-05-15 10:51:30+05:30 Cprogrammer Exp mbhangui $
# $Id: daemontools.spec.in,v 1.48 2024-07-01 18:21:51+05:30 Cprogrammer Exp mbhangui $
%undefine _missing_build_ids_terminate_build
%global _unpackaged_files_terminate_build 1

Expand Down Expand Up @@ -356,8 +356,9 @@ if [ $? -ne 0 ] ; then
log_it "svscan not running"
exit 0
else
touch %{servicedir}/.svscan.down
log_it "Giving svscan exactly 5 seconds to exit nicely" >> /var/log/%{name}-setup.log
%{_prefix}/bin/svps -s
touch %{servicedir}/.svscan.down
if test -f %{_sysconfdir}/init/svscan.conf
then
/sbin/initctl emit qmailstop > /dev/null 2>&1
Expand Down Expand Up @@ -506,6 +507,7 @@ if [ $argv1 -eq 2 ] ; then # upgrade
fi
fi
fi
%{_prefix}/bin/svps -r
fi
# refresh svscanlog services
%{_prefix}/sbin/minisvc --servicedir=%{servicedir} --refreshsvc="/service/.svscan"
Expand Down
1 change: 1 addition & 0 deletions daemontools-x/debian/postinst.in
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ if [ $upgrade -eq 1 ] ; then # upgrade
fi
fi
fi
${prefix}/bin/svps -r
fi
) >> /var/log/daemontools-setup.log 2>&1
exit 0
Expand Down
1 change: 1 addition & 0 deletions daemontools-x/debian/preinst.in
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ case "$1" in
fi
fi
fi
${prefix}/bin/svps -s
set -e
sleep 5
else
Expand Down
4 changes: 3 additions & 1 deletion daemontools-x/debian/prerm.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ case "$1" in
set +e
${prefix}/bin/svok /service/.svscan/log
if [ $? -eq 0 ] ; then
touch ${servicedir}/.svscan.down
log_it "prerm: Giving svscan exactly 5 seconds to exit nicely"
touch ${servicedir}/.svscan.down
${prefix}/bin/svps -s
if test -f /etc/init/svscan.conf
then
/sbin/initctl emit qmailstop
Expand Down Expand Up @@ -77,6 +78,7 @@ case "$1" in
fi
fi
fi
${prefix}/bin/svps -s
set -e
sleep 5
else
Expand Down
5 changes: 5 additions & 0 deletions daemontools-x/doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Release @version@-@release@ Start 05/02/2024 End XX/XX/XXXX
10. daemontools.spec: fixed syntax error for opensuse
- 20/06/2024
11. svps.in: display services for all svscan instances
- 01/07/2024
12. svps.in: added stop/start all services functions
13. daemntools.spec: stop/start all services during upgrade
14. debian/postinst.in, debian/prerm.in: stop/start all services during
upgrade

* Sun Mar 03 2024 12:20:15 +0000 Manvendra Bhangui <daemontools@indimail.org> 1.1.4-1.1%{?dist}
Release 1.1.4-1.1 Start 09/02/2024 End 03/02/2024
Expand Down
22 changes: 15 additions & 7 deletions daemontools-x/svps.1
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,45 @@ The process id can be of the service (pid) or the supervise process (spid)

.SH OPTIONS
.TP
\-\-\fIall\fR or -a
\-\-\fIall\fR or \-a

Print status for both main and log processes

.TP
.B \-\-main or -m
.B \-\-main or \-m

Print status for main processes

.TP
\-\-\fIhup\fR or -h
\-\-\fIhup\fR or \-h

Send HUP signal to svscan

.TP
\-\-\fIterm\fR or -t
\-\-\fIterm\fR or \-t

Send TERM signal to svscan

.TP
.B \-\-logs or -l
.B \-\-logs or \-l

Print status for log processes

.TP
.B \-\-days or -d
.B \-\-days or \-d

Print uptime/downtime in days, hours, minutes and seconds.

.TP
.B \-\-no-pager or -n
.B \-\-stop or \-s
Stop all currently running services

.TP
.B \-\-resume or \-r
Resume all services that were stopped earlier using -s option.

.TP
.B \-\-no-pager or \-n

Do not use print output through a pager

Expand Down
154 changes: 114 additions & 40 deletions daemontools-x/svps.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# $Id: svps.in,v 1.19 2024-06-20 11:07:51+05:30 Cprogrammer Exp mbhangui $
# $Id: svps.in,v 1.20 2024-07-01 09:34:06+05:30 Cprogrammer Exp mbhangui $
#

do_days()
Expand Down Expand Up @@ -45,14 +45,16 @@ doit()
echo
printf "============ %-"$t"s state uptime ===== pid/spid ==\n" $svdir

grep -w down /tmp/svps.$$ | grep -v "normally down" | sort -n -k3 | grep -v "/log" | while read line
grep -w down /tmp/svps.$$ | grep -E -v "normally down|want down" | sort -n -k3 | grep -v "/log" | while read line
do
set $line
name=`echo $1|cut -d: -f1`
secs=$3
do_days $secs
if [ "$5" = "normally" ] ; then
printf "%-"$max"s down %s spid %7d\n" $name "$tstr" $8
elif [ "$5" = "want" ] ; then
printf "%-"$max"s down %s spid %7d\n" $name "$tstr" $8
else
printf "%-"$max"s down %s spid %7d\n" $name "$tstr" $6
fi
Expand All @@ -77,14 +79,16 @@ doit()
fi
done

grep -Ew "up" /tmp/svps.$$ | grep -v "normally up" | sort -n -k3 | grep -v "/log" | while read line
grep -Ew "up" /tmp/svps.$$ | grep -E -v "normally up|want up" | sort -n -k3 | grep -v "/log" | while read line
do
set $line
name=`echo $1|cut -d: -f1`
secs=$3
do_days $secs
if [ "$5" = "normally" ] ; then
printf "%-"$max"s up %s pid %7d\n" $name "$tstr" $8
elif [ "$5" = "want" ] ; then
printf "%-"$max"s up %s pid %7d\n" $name "$tstr" $8
else
printf "%-"$max"s up %s pid %7d\n" $name "$tstr" $6
fi
Expand Down Expand Up @@ -119,32 +123,51 @@ doit()
done
;;
esac
case "$option" in
hup)
if [ ! -f $pidfile ] ; then
echo "$pidfile: No such file or directory" 1>&2
exit 1
else
echo "Sending HUP to svscan with PID $svpid"
kill -1 $svpid
if [ $? -ne 0 ] ; then
echo "Failed to send HUP signal to pid $svpid" 1>&2
}

stop_all()
{
flag=0
for i in $sv_dirs
do
for j in $i/*
do
if [ -d $j ] ; then
@prefix@/bin/svstat $j >/dev/null
if [ $? -eq 0 ] ; then
mkdir -p /var/tmp/$j
if [ $? -ne 0 ] ; then
continue
fi
[ $flag -eq 0 ] && echo "Stopping all running services"
flag=1
@prefix@/bin/svc -d $j
if [ $? -eq 0 ] ; then
touch /var/tmp/$j/down
fi
fi
fi
fi
;;
term)
if [ ! -f $pidfile ] ; then
echo "$pidfile: No such file or directory" 1>&2
exit 1
else
echo "Sending TERM to svscan with PID $svpid"
kill -TERM $svpid
if [ $? -ne 0 ] ; then
echo "Failed to send HUP signal to pid $svpid" 1>&2
done
done
[ $flag -eq 0 ] && echo "No running services found" 1>&2
}

resume_all()
{
flag=0
for i in $sv_dirs
do
for j in $i/*
do
if [ -f /var/tmp/$j/down ] ; then
[ $flag -eq 0 ] && echo "Starting all stopped services"
flag=1
@prefix@/bin/svc -u $j
/bin/rm -rf /var/tmp/$j
fi
fi
;;
esac
done
done
[ $flag -eq 0 ] && echo "No stopped services found" 1>&2
}

if [ -z "$PAGER" ] ; then
Expand Down Expand Up @@ -186,9 +209,16 @@ while test $# -gt 0; do
-d|--days)
mday=1
;;
-s|--stop)
option=stop
;;
-r|--resume)
option=resume
;;
*)
echo "invalid option [$1]" 1>&2
echo "usage: svps [-a|--all | -m|--main | -h|--hup | -t|--term | -n|--no-pager | -d|--days | -l|--logs]" 1>&2
echo "usage: svps [-a|--all | -m|--main | -h|--hup | -t|--term | -n|--no-pager] |"
echo " [-d|--days -s|--stop | -r|--resume | -l|--logs]" 1>&2
read key
exit 1
;;
Expand All @@ -205,29 +235,20 @@ if [ $ID -ne 0 ] ; then
fi
if [ -d /run/svscan ] ; then
pidfile=/run/svscan/.svscan.pid
rundir=/run
elif [ -d /var/run/svscan ] ; then
pidfile=/var/run/svscan/.svscan.pid
rundir=/var/run
else
pidfile=@servicedir@/.svscan.pid
rundir=@servicedir@
fi
if [ -f $pidfile ] ; then
svpid=$(sed -n '$p' $pidfile)
else
echo "svscan not running: $pdfile: No such file or directory" 1>&2
exit 1
fi
SYSTEM=$(uname -s)
case "$SYSTEM" in
Darwin|FreeBSD)
mod=$(stat -f "%m" $pidfile)
;;
*)
if [ -f /etc/alpine-release ] ; then
mod=$(stat -c "%Y" $pidfile)
else
mod=$(stat --printf="%Y\n" $pidfile)
fi
esac

ps ax|grep "sbin/svscan"|grep -Ev "grep|multilog|supervise" > /tmp/svps.$$
max=0
Expand All @@ -246,6 +267,43 @@ do
fi
done < /tmp/svps.$$

case "$option" in
stop)
stop_all
exit 0
;;
resume)
resume_all
exit 0
;;
hup)
if [ ! -f $pidfile ] ; then
echo "$pidfile: No such file or directory" 1>&2
exit 1
else
echo "Sending HUP to svscan with PID $svpid"
kill -1 $svpid
if [ $? -ne 0 ] ; then
echo "Failed to send HUP signal to pid $svpid" 1>&2
fi
fi
exit 0
;;
term)
if [ ! -f $pidfile ] ; then
echo "$pidfile: No such file or directory" 1>&2
exit 1
else
echo "Sending TERM to svscan with PID $svpid"
kill -TERM $svpid
if [ $? -ne 0 ] ; then
echo "Failed to send HUP signal to pid $svpid" 1>&2
fi
fi
exit 0
;;
esac

(
for i in $sv_dirs
do
Expand Down Expand Up @@ -279,6 +337,19 @@ done < /tmp/svps.$$
if [ "$option" = "hup" -o "$option" = "term" ] ; then
PAGER=""
fi

SYSTEM=$(uname -s)
case "$SYSTEM" in
Darwin|FreeBSD)
mod=$(stat -f "%m" $pidfile)
;;
*)
if [ -f /etc/alpine-release ] ; then
mod=$(stat -c "%Y" $pidfile)
else
mod=$(stat --printf="%Y\n" $pidfile)
fi
esac
if [ -n "$PAGER" ] ; then
(
do_svscan
Expand Down Expand Up @@ -308,6 +379,9 @@ fi
/bin/rm -f /tmp/svps.$$
#
# $Log: svps.in,v $
# Revision 1.20 2024-07-01 09:34:06+05:30 Cprogrammer
# added stopall, startall functions
#
# Revision 1.19 2024-06-20 11:07:51+05:30 Cprogrammer
# display services for all svscan instances
#
Expand Down

0 comments on commit 19c522c

Please sign in to comment.