처음 5개 위치를 제외한 모든 "at" 위치를 제거합니다.

처음 5개 위치를 제외한 모든 "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

관련 정보