I want to keep only first five scheduled jobs (as in the lowest 5 job ID numbers) and remove the rest of the scheduled atq jobs. How to can I do this?
Asked
Active
Viewed 342 times
2 Answers
0
On my Debian system, at
sorts jobs by the time they are scheduled to start and not the order they were given to at
in:
$ for i in 10 20 30 40 50 60 70; do
at now + "$i" min < scripts/foo.sh; sleep 1;
done
warning: commands will be executed using /bin/sh
job 8 at Sat Apr 18 15:31:00 2015
warning: commands will be executed using /bin/sh
job 9 at Sat Apr 18 15:41:00 2015
warning: commands will be executed using /bin/sh
job 10 at Sat Apr 18 15:51:00 2015
warning: commands will be executed using /bin/sh
job 11 at Sat Apr 18 16:01:00 2015
warning: commands will be executed using /bin/sh
job 12 at Sat Apr 18 16:12:00 2015
warning: commands will be executed using /bin/sh
job 13 at Sat Apr 18 16:22:00 2015
warning: commands will be executed using /bin/sh
job 14 at Sat Apr 18 16:32:00 2015
$ atq
9 Sat Apr 18 15:41:00 2015 a terdon
11 Sat Apr 18 16:01:00 2015 a terdon
10 Sat Apr 18 15:51:00 2015 a terdon
12 Sat Apr 18 16:12:00 2015 a terdon
8 Sat Apr 18 15:31:00 2015 a terdon
14 Sat Apr 18 16:32:00 2015 a terdon
13 Sat Apr 18 16:22:00 2015 a terdon
As you can see, at
will number the jobs in the order in which they will be run, but atq
lists them in an apparently random order.
To remove the first 5 jobs as listed by
atq
, you can do:atrm $(atq | head -5 | cut -f 1)
To delete the first 5 jobs based on the order they will be launched in, do:
atrm $(atq | sort -n | head -5 | cut -f 1)

terdon
- 242,166
0
This removes the first 5, so is wrong, if you can findout how to do an inverted head (remove head), then you will have the answer. A combination of wc
and tail
may do it.
atq | sort -g | head -5 | cut -f1 | xargs atrm
Correct answer
atq | sort -g | tail -n +6 | cut -f1 | xargs atrm

ctrl-alt-delor
- 27,993
-
1
tail -n +6
will start at line 6. (Yes, bizarre syntax.) Or, untested,sed -ne '6,$p'
– Ulrich Schwarz Apr 18 '15 at 17:24 -
@UlrichSchwarz cool I experimented and
head -n -5
does all but last 5. – ctrl-alt-delor Apr 18 '15 at 17:29 -
@Richard but that's not what the OP wants either. They want to keep the first five and cancel all the others. – Chris Davies Apr 18 '15 at 22:11
-
@roaima are you commenting my comment or answer, I think the answer is correct, at least the bit that says it is correct. The comment is just commenting about the previous comment (an aside, may be not too relevent). – ctrl-alt-delor Apr 18 '15 at 22:20
-
at -l | head -5
(which is more or less random) – Archemar Apr 18 '15 at 06:58