删除除前五个职位外的所有“at”职位

删除除前五个职位外的所有“at”职位

我只想保留前 5 个计划作业(如最低的 5 个作业 ID 号)并删除其余的计划 atq 作业。我怎样才能做到这一点?

答案1

在我的 Debian 系统上,at按计划启动时间而不是按指定顺序对作业进行排序at

$ 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

正如您所看到的,at将按照作业的运行顺序对作业进行编号,但atq以明显随机的顺序列出它们。

  1. 要删除 列出的前 5 个作业atq,您可以执行以下操作:

    atrm $(atq | head -5 | cut -f 1)
    
  2. 要根据启动顺序删除前 5 个作业,请执行以下操作:

    atrm $(atq | sort -n | head -5 | cut -f 1)
    

答案2

这删除了前5个,所以是错误的,如果你能找出如何做倒立头(删除头),那么你就会有答案。wc和的组合tail可以做到这一点。

atq | sort -g  | head -5 | cut -f1 | xargs atrm

正确答案

atq | sort -g  | tail -n +6 | cut -f1 | xargs atrm

相关内容