1

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?

3
  • what are you calling 'first' ? next to be run ? oldest/latest to have been submitted ? top 5 in at -l | head -5 (which is more or less random) Commented Apr 18, 2015 at 6:58
  • at command schedules job by job id. I want to run only first 5 schedule job Ids and remove others. Commented Apr 18, 2015 at 7:39
  • Please edit your question to make this clear Commented Apr 18, 2015 at 22:09

2 Answers 2

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.

  1. To remove the first 5 jobs as listed by atq, you can do:

    atrm $(atq | head -5 | cut -f 1) 
  2. 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) 
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 
5
  • 1
    tail -n +6 will start at line 6. (Yes, bizarre syntax.) Or, untested, sed -ne '6,$p' Commented Apr 18, 2015 at 17:24
  • @UlrichSchwarz cool I experimented and head -n -5 does all but last 5. Commented 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. Commented 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). Commented Apr 18, 2015 at 22:20
  • @Richard I was responding to your comment. Commented Apr 19, 2015 at 12:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.