programing

Linux에서 자동으로 명령 반복

telebox 2023. 5. 27. 10:37
반응형

Linux에서 자동으로 명령 반복

리눅스 명령줄에서 n초마다 명령을 반복할 수 있습니까?

예를 들어, 나는 가져오기를 실행 중이고, 나는 수행 중입니다.

ls -l

파일 크기가 증가하는지 확인합니다.이 작업을 자동으로 반복하는 명령을 받고 싶습니다.

5초마다 보기...

watch -n 5 ls -l

변경 사항을 시각적으로 확인하려면 추가--differences에 앞서ls지휘권

OSX man 페이지에 따르면 다음과 같은 것들이 있습니다.

--cumulative 옵션을 사용하면 "sticky"를 강조 표시하여 변경된 모든 위치를 표시할 수 있습니다.-t 또는 --no-title 옵션은 다음 빈 줄뿐만 아니라 간격, 명령, 현재 시간을 디스플레이 맨 위에 표시하는 헤더를 해제합니다.

Linux/Unix man 페이지는 여기에서 찾을 수 있습니다.

while true; do
    sleep 5
    ls -l
done

"watch"는 "sleep"이 허용하는 반면, "watch"는 Busybox에서 몇 초의 분수를 허용하지 않습니다.문제가 되는 경우 다음을 수행합니다.

while true; do ls -l; sleep .5; done

sleep이미 돌아온0따라서 다음을 사용합니다.

while sleep 3 ; do ls -l ; done

이것은 미하일의 해결책보다 조금 더 짧습니다.작은 단점은 대상 명령을 처음 실행하기 전에 절전 모드로 전환된다는 것입니다.

명령에 파이프 및 따옴표와 같은 특수 문자가 포함된 경우 명령에 따옴표를 추가해야 합니다.예를 들어, 반복합니다.ls -l | grep "txt"watch 명령은 다음과 같아야 합니다.

watch -n 5 'ls -l | grep "txt"'

cron 없이 정기적으로 명령을 실행하는 것은 다음과 같이 할 때 가능합니다.while.

명령어로:

while true ; do command ; sleep 100 ; done &
[ ex: # while true;  do echo `date` ; sleep 2 ; done & ]

예:

while true
do echo "Hello World"
sleep 100
done &

마지막을 잊지 마세요.&당신의 루프를 배경에 넣을 것이기 때문입니다.그러나 "ps -ef | grep your_script" 명령을 사용하여 프로세스 ID를 찾은 다음 이를 삭제해야 합니다.그러니 스크립트를 실행할 때 '&'을 추가해주세요.

# ./while_check.sh &

여기 스크립트와 동일한 루프가 있습니다.파일 "while_check.sh"를 생성하고 이 파일에 넣습니다.

#!/bin/bash
while true; do 
    echo "Hello World" # Substitute this line for whatever command you want.
    sleep 100
done

그런 다음 입력하여 실행bash ./while_check.sh &

시계는 좋지만 화면을 청소할 것입니다.

watch -n 1 'ps aux | grep php'

"drifting"을 피하고자 하는 경우, 즉 명령이 소요되는 시간(N초 미만의 시간이 걸린다고 가정할 때)에 관계없이 명령이 N초마다 실행되도록 하려면 다음과 같이 5초마다 1초의 정확도로 명령을 반복합니다(따라가지 못할 경우 경고를 출력합니다).

PERIOD=5

while [ 1 ]
do
    let lastup=`date +%s`
    # do command
    let diff=`date +%s`-$lastup
    if [ "$diff" -lt "$PERIOD" ]
    then
        sleep $(($PERIOD-$diff))
    elif [ "$diff" -gt "$PERIOD" ]
    then
        echo "Command took longer than iteration period of $PERIOD seconds!"
    fi
done

수면 시간이 1초로 정확하기 때문에 아직 약간 표류할 수 있습니다.date 명령을 창의적으로 사용하여 이 정확도를 향상시킬 수 있습니다.

.zsh:

repeat 300 (command1; command2) && sleep 1.5

:repeatbash 명령이 아닙니다.

다음을 실행하고 크기만 필터링할 수 있습니다.파일이 호출된 경우somefilename과 같이 할 수 .

while :; do ls -lh | awk '/some*/{print $5}'; sleep 5; done

많은 아이디어 중 하나.

보다 쉽게 드리프트를 최소화하려면 다음을 사용합니다.

while :; do sleep 1m & some-command; wait; done

bash가 루프 구조를 실행하는 시간과 실제로 실행하는 sleep 명령으로 인해 여전히 소량의 드리프트가 있을 것입니다.

힌트: ':'는 0 ie true입니다.

명령이 실패할 때까지 반복적으로 실행하고 모든 출력을 볼 수 있는 간결한 솔루션입니다.

while ls -l; do
    sleep 5
done
watch -n 5 'ls -l 

예정 »ls -l 간격으로 실행됩니다.

출력 :-

Every 5.0s: ls -l                                                                                                      Fri Nov 17 16:28:25 2017

total 169548
-rw-rw-r--  1 sachin sachin     4292 Oct 18 12:16 About_us_Admission.doc
-rw-rw-r--  1 sachin sachin      865 Oct 13 15:26 About_us_At_glance.doc
-rw-rw-r--  1 sachin sachin     1816 Oct 13 16:11 About_us_Principle.doc
-rw-rw-r--  1 sachin sachin     1775 Oct 13 15:59 About_us_Vission_mission.doc
-rw-rw-r--  1 sachin sachin     1970 Oct 13 16:41 Academic_Middle_school.doc
-rw-rw-r--  1 sachin sachin      772 Oct 16 16:07 academics_High_School.doc
-rw-rw-r--  1 sachin sachin      648 Oct 16 13:34 academics_pre_primary.doc
-rw-rw-r--  1 sachin sachin      708 Oct 16 13:39 academics_primary.doc
-rwxrwxr-x  1 sachin sachin     8816 Nov  1 12:10 a.out
-rw-rw-r--  1 sachin sachin    23956 Oct 23 18:14 Ass1.c++
-rw-rw-r--  1 sachin sachin      342 Oct 23 22:13 Ass2.doc
drwxrwxr-x  2 sachin sachin     4096 Oct 19 10:45 Backtracking
drwxrwxr-x  3 sachin sachin     4096 Sep 23 20:09 BeautifulSoup
drwxrwxr-x  2 sachin sachin     4096 Nov  2 00:18 CL_1
drwxrwxr-x  2 sachin sachin     4096 Oct 23 20:16 Code
drwxr-xr-x  2 sachin sachin     4096 Nov 15 12:05 Desktop
-rw-rw-r--  1 sachin sachin        0 Oct 13 23:12 doc
drwxr-xr-x  4 sachin sachin     4096 Nov  6 21:18 Documents
drwxr-xr-x 27 sachin sachin    12288 Nov 17 13:23 Downloads
-rw-r--r--  1 sachin sachin     8980 Sep 19 23:58 examples.desktop

x초마다 전달되는 명령을 감시하는 셸 별칭을 만들었습니다.명령은 별도의 인수이거나 세미콜론으로 구분할 수 있습니다.

# execute commands at a specified interval of seconds
function watch.command {
  # USAGE: watch.commands [seconds] [commands...]
  # EXAMPLE: watch.command 5 date
  # EXAMPLE: watch.command 5 date echo 'ls -l' echo 'ps | grep "kubectl\\\|node\\\|npm\\\|puma"'
  # EXAMPLE: watch.command 5 'date; echo; ls -l; echo; ps | grep "kubectl\\\|node\\\|npm\\\|puma"' echo date 'echo; ls -1'
  local cmds=()
  for arg in "${@:2}"; do
    echo $arg | sed 's/; /;/g' | tr \; \\n | while read cmd; do
      cmds+=($cmd)
    done
  done
  while true; do
    clear
    for cmd in $cmds; do
      eval $cmd
    done
    sleep $1
  done
}

https://gist.github.com/Gerst20051/99c1cf570a2d0d59f09339a806732fd3

언급URL : https://stackoverflow.com/questions/13593771/repeat-command-automatically-in-linux

반응형