summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--NEWS.md4
-rw-r--r--nq.13
-rw-r--r--nq.c20
3 files changed, 22 insertions, 5 deletions
diff --git a/NEWS.md b/NEWS.md
index df29f85..49de6f1 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,3 +1,7 @@
+## HEAD
+
+* nq: add `-c` to clean job file when the process succeeded.
+
 ## 0.2.2 (2017-12-21)
 
 * fq: fix when `$NQDIR` is set and inotify is used.  (Thanks to Sebastian Reuße)
diff --git a/nq.1 b/nq.1
index b3d933b..2a74749 100644
--- a/nq.1
+++ b/nq.1
@@ -6,6 +6,7 @@
 .Nd job queue utility
 .Sh SYNOPSIS
 .Nm
+.Op Fl c
 .Op Fl q
 .Ar command\ line ...
 .Nm
@@ -51,6 +52,8 @@ can be used to conveniently watch the log files.
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
+.It Fl c
+Clean up job id file when process exited with status 0.
 .It Fl q
 Suppress output of the job id after spawning new job.
 .It Fl t
diff --git a/nq.c b/nq.c
index 1f28a0c..d83fa02 100644
--- a/nq.c
+++ b/nq.c
@@ -3,6 +3,7 @@
  * -w ...  wait for all jobs/listed jobs queued so far to finish
  * -t ...  exit 0 if no (listed) job needs waiting
  * -q      quiet, do not output job id
+ * -c      clean, don't keep output if job exited with status 0
  *
  * - requires POSIX.1-2008 and having flock(2)
  * - enforcing order works like this:
@@ -19,6 +20,8 @@
  * http://creativecommons.org/publicdomain/zero/1.0/
 */
 
+#define _XOPEN_SOURCE 700
+
 /* for FreeBSD.  */
 #define _WITH_DPRINTF
 
@@ -96,7 +99,8 @@ int
 main(int argc, char *argv[])
 {
 	int64_t ms;
-	int dirfd = 0, lockfd = 0, opt = 0, qflag = 0, tflag = 0, wflag = 0;
+	int dirfd = 0, lockfd = 0;
+	int opt = 0, cflag = 0, qflag = 0, tflag = 0, wflag = 0;
 	int pipefd[2];
 	char lockfile[64];
 	pid_t child;
@@ -108,8 +112,11 @@ main(int argc, char *argv[])
 	gettimeofday(&started, NULL);
 	ms = (int64_t)started.tv_sec*1000 + started.tv_usec/1000;
 
-	while ((opt = getopt(argc, argv, "+hqtw")) != -1) {
+	while ((opt = getopt(argc, argv, "+chqtw")) != -1) {
 		switch (opt) {
+		case 'c':
+			cflag = 1;
+			break;
 		case 'w':
 			wflag = 1;
 			break;
@@ -127,7 +134,7 @@ main(int argc, char *argv[])
 
 	if (argc <= 1) {
 usage:
-		swrite(2, "usage: nq [-q] [-w ... | -t ... | CMD...]\n");
+		swrite(2, "usage: nq [-c] [-q] [-w ... | -t ... | CMD...]\n");
 		exit(1);
 	}
 
@@ -211,12 +218,15 @@ usage:
 		}
 
 		fchmod(lockfd, 0600);
-		if (WIFEXITED(status))
+		if (WIFEXITED(status)) {
 			dprintf(lockfd, "\n[exited with status %d.]\n",
 			    WEXITSTATUS(status));
-		else
+			if (cflag && WEXITSTATUS(status) == 0)
+				unlinkat(dirfd, lockfile, 0);
+		} else {
 			dprintf(lockfd, "\n[killed by signal %d.]\n",
 			    WTERMSIG(status));
+		}
 
 		exit(0);
 	}