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?
2 Answers
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)
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 - 1
tail -n +6will start at line 6. (Yes, bizarre syntax.) Or, untested,sed -ne '6,$p'Ulrich Schwarz– Ulrich Schwarz2015-04-18 17:24:47 +00:00Commented Apr 18, 2015 at 17:24 - @UlrichSchwarz cool I experimented and
head -n -5does all but last 5.ctrl-alt-delor– ctrl-alt-delor2015-04-18 17:29:59 +00:00Commented Apr 18, 2015 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– Chris Davies2015-04-18 22:11:18 +00:00Commented Apr 18, 2015 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– ctrl-alt-delor2015-04-18 22:20:23 +00:00Commented Apr 18, 2015 at 22:20
- @Richard I was responding to your comment.Chris Davies– Chris Davies2015-04-19 12:14:14 +00:00Commented Apr 19, 2015 at 12:14
at -l | head -5(which is more or less random)