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?

derobert
  • 109,670
KK Patel
  • 1,855

2 Answers2

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)
    
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