182c96a
diff -up nut-2.8.1/clients/upsmon.c.rmpidf nut-2.8.1/clients/upsmon.c
182c96a
--- nut-2.8.1/clients/upsmon.c.rmpidf	2023-11-01 10:10:03.039782733 +0100
182c96a
+++ nut-2.8.1/clients/upsmon.c	2023-11-01 10:14:48.824806113 +0100
182c96a
@@ -3059,6 +3059,7 @@ int main(int argc, char *argv[])
59230cd
 	upslogx(LOG_INFO, "Signal %d: exiting", exit_flag);
182c96a
 	upsnotify(NOTIFY_STATE_STOPPING, "Signal %d: exiting", exit_flag);
59230cd
 	upsmon_cleanup();
59230cd
+	cleanpid(prog);
59230cd
 
59230cd
 	exit(EXIT_SUCCESS);
59230cd
 }
182c96a
diff -up nut-2.8.1/common/common.c.rmpidf nut-2.8.1/common/common.c
182c96a
--- nut-2.8.1/common/common.c.rmpidf	2023-10-30 00:02:33.000000000 +0100
182c96a
+++ nut-2.8.1/common/common.c	2023-11-01 10:10:03.040782744 +0100
182c96a
@@ -519,6 +519,20 @@ pid_t parsepid(const char *buf)
4868536
 	return pid;
59230cd
 }
59230cd
 
59230cd
+/* remove pid file */
59230cd
+void cleanpid(const char *name)
59230cd
+{
4868536
+       char    fn[SMALLBUF];
59230cd
+
4868536
+       /* use full path if present, else build filename in PIDPATH */
4868536
+       if (*name == '/')
4868536
+               snprintf(fn, sizeof(fn), "%s", name);
4868536
+       else
4868536
+               snprintf(fn, sizeof(fn), "%s/%s.pid", PIDPATH, name);
59230cd
+
4868536
+       unlink(fn);
59230cd
+}
59230cd
+
4868536
 /* open pidfn, get the pid, then send it sig
4868536
  * returns negative codes for errors, or
4868536
  * zero for a successfully sent signal
182c96a
diff -up nut-2.8.1/include/common.h.rmpidf nut-2.8.1/include/common.h
182c96a
--- nut-2.8.1/include/common.h.rmpidf	2023-10-24 10:45:21.000000000 +0200
182c96a
+++ nut-2.8.1/include/common.h	2023-11-01 10:10:03.040782744 +0100
182c96a
@@ -211,6 +211,9 @@ void chroot_start(const char *path);
59230cd
 /* write a pid file - <name> is a full pathname *or* just the program name */
59230cd
 void writepid(const char *name);
59230cd
 
59230cd
+/* remove pid file */
59230cd
+void cleanpid(const char *name);
59230cd
+
4868536
 /* parses string buffer into a pid_t if it passes
4868536
  * a few sanity checks; returns -1 on error */
4868536
 pid_t parsepid(const char *buf);
182c96a
diff -up nut-2.8.1/server/upsd.c.rmpidf nut-2.8.1/server/upsd.c
182c96a
--- nut-2.8.1/server/upsd.c.rmpidf	2023-11-01 10:10:03.040782744 +0100
182c96a
+++ nut-2.8.1/server/upsd.c	2023-11-01 10:15:57.176529218 +0100
182c96a
@@ -2137,5 +2137,6 @@ int main(int argc, char **argv)
182c96a
 	upsnotify(NOTIFY_STATE_STOPPING, "Signal %d: exiting", exit_flag);
59230cd
 
182c96a
 	ssl_cleanup();
59230cd
+	if (*pidfn) cleanpid(pidfn);
59230cd
 	return EXIT_SUCCESS;
59230cd
 }