2013-06-30 36 views
28

Tôi muốn thực thi một tập lệnh và chạy nó mỗi x phút một lần.Làm cách nào để lệnh bash chạy theo định kỳ?

Ngoài ra, mọi lời khuyên chung về bất kỳ tài nguyên nào để học tập bash đều có thể thực sự tuyệt vời. Tôi sử dụng Linux cho công việc phát triển cá nhân của mình, vì vậy các tập lệnh bash không hoàn toàn xa lạ với tôi, tôi chưa viết bất kỳ từ nào của riêng tôi từ đầu.

+1

Bạn có nhìn vào 'cron' không? – SheetJS

+3

Đối với yêu cầu của bạn về tài nguyên, wiki bash-hacker có [danh sách các tài nguyên] được chăm sóc tốt (http://wiki.bash-hackers.org/scripting/tutoriallist), được cắt tỉa rõ ràng để tránh các hướng dẫn không cẩn thận để tránh [cạm bẫy] cổ điển (http://mywiki.wooledge.org/BashPitfalls). – kojiro

Trả lời

53

Nếu bạn muốn chạy một lệnh định kỳ, có 3 cách:

  • sử dụng crontab lệnh cũ. * * * * * command (chạy mỗi phút)
  • sử dụng một vòng lặp như: while true; do ./my_script.sh; sleep 60; done (không chính xác)
  • sử dụng systemd timer

Xem cron

Một số gợi ý cho các hoạt động bash kịch bản tốt nhất:

http://mywiki.wooledge.org/BashFAQ
Hướng dẫn: http://mywiki.wooledge.org/BashGuide
ref: http://www.gnu.org/software/bash/manual/bash.html
http://wiki.bash-hackers.org/
sử dụng dấu ngoặc THÊM !: http://www.grymoire.com/Unix/Quote.html
Scripts và nhiều hơn nữa: http://www.shelldorado.com/

+1

Crontab ở đây là lựa chọn tốt hơn, đặc biệt nếu bạn muốn chạy "mãi mãi" này (một thời gian dài). – DaGardner

+3

Tôi tự hỏi tại sao câu trả lời này lại bị bỏ phiếu. –

+1

Câu hỏi hay :) +1 để bù đắp – DaGardner

2

Tôi muốn thực thi kịch bản và có nó chạy một lệnh mỗi {thời gian khoảng}

cron (https://en.wikipedia.org/wiki/Cron) được thiết kế cho mục đích này. Nếu bạn chạy man cron hoặc man crontab, bạn sẽ tìm thấy hướng dẫn về cách sử dụng.

bất kỳ lời khuyên chung nào về bất kỳ tài nguyên nào để học tập bash script có thể thực sự tuyệt vời. Tôi sử dụng Linux cho công việc phát triển cá nhân của mình, vì vậy các tập lệnh bash không hoàn toàn xa lạ với tôi, tôi chưa viết bất kỳ từ nào của riêng tôi từ đầu.

Nếu bạn cảm thấy thoải mái khi làm việc với bash, tôi khuyên bạn nên đọc qua trang bash manpage trước (man bash) - có rất nhiều mẩu tin thú vị.

16

Ngoài câu trả lời của @ sputnick, cũng có watch. Từ trang người đàn ông:

Execute a program periodically, showing output full screen 

Theo mặc định, cứ 2 giây một lần. Ví dụ: watch hữu ích cho tail các nhật ký nhập.

+3

Điều quan trọng là chỉ ra rằng 'xem' là không chuẩn và không tồn tại trên hầu hết các hệ thống không phải GNU (bao gồm cả Mac) theo mặc định. – kojiro

+6

cho các bản ghi 'tail'ing bạn sử dụng tốt hơn' tail -f $ filename' – Francisco

6

MacOS người sử dụng: đây là một phần thi hành GNU watch lệnh (như các phiên bản 0.3.0) cho tương tác invocations định kỳ cho chủ yếu thị giác kiểm tra:

Đó là cú pháp tương thích với phiên bản GNU và thất bại với một thông báo lỗi cụ thể nếu một tính năng chưa được thực hiện được sử dụng.

hạn chế đáng chú ý:

  • Sản lượng không giới hạn ở một màn hình.
  • Hiển thị sự khác biệt đầu ra không được hỗ trợ.
  • Sử dụng thời gian chính xác không được hỗ trợ.
  • Đầu ra màu là luôn luôn được chuyển qua (--color được ngụ ý).

Cũng thực hiện một vài tính năng phi tiêu chuẩn, như chờ đợi thành công (-E) để bổ sung chờ đợi lỗi (-e) và hiển thị thời gian trong ngày của invocation qua cũng như tổng thời gian trôi qua cho đến nay.

Chạy watch -h để biết chi tiết.

Ví dụ:

watch -n 1 ls # list current dir every second 
watch -e 'ls *.lockfile' # list lock files and exit once none exist anymore. 

Source code (dán vào một tập tin script tên watch, làm cho nó thực thi, và đặt trong một thư mục trong $PATH của bạn, lưu ý cú pháp mà làm nổi bật ở đây là bị hỏng, nhưng mã công trình):

#!/usr/bin/env bash 

THIS_NAME=$(basename "$BASH_SOURCE") 

VERSION='0.1' 

# Helper function for exiting with error message due to runtime error. 
# die [errMsg [exitCode]] 
# Default error message states context and indicates that execution is aborted. Default exit code is 1. 
# Prefix for context is always prepended. 
# Note: An error message is *always* printed; if you just want to exit with a specific code silently, use `exit n` directly. 
die() { 
    echo "$THIS_NAME: ERROR: ${1:-"ABORTING due to unexpected error."}" 1>&2 
    exit ${2:-1} # Note: If the argument is non-numeric, the shell prints a warning and uses exit code 255. 
} 

# Helper function for exiting with error message due to invalid parameters. 
# dieSyntax [errMsg] 
# Default error message is provided, as is prefix and suffix; exit code is always 2. 
dieSyntax() { 
    echo "$THIS_NAME: PARAMETER ERROR: ${1:-"Invalid parameter(s) specified."} Use -h for help." 1>&2 
    exit 2 
} 

# Get the elapsed time since the specified epoch time in format HH:MM:SS. 
# Granularity: whole seconds. 
# Example: 
# tsStart=$(date +'%s') 
# ... 
# getElapsedTime $tsStart 
getElapsedTime() { 
    date -j -u -f '%s' $(($(date +'%s') - $1)) +'%H:%M:%S' 
} 

# Command-line help. 
if [[ "$1" == '--help' || "$1" == '-h' ]]; then 
    cat <<EOF 

SYNOPSIS 
    $THIS_NAME [-n seconds] [opts] cmd [arg ...] 

DESCRIPTION 
    Executes a command periodically and displays its output for visual inspection. 

    NOTE: This is a PARTIAL implementation of the GNU \`watch\` command, for OS X. 
    Notably, the output is not limited to one screenful, and displaying 
    output differences and using precise timing are not supported. 
    Also, colored output is always passed through (--color is implied). 
    Unimplemented features are marked as [NOT IMPLEMENTED] below. 
    Conversely, features specific to this implementation are marked as [NONSTD]. 
    Reference version is GNU watch 0.3.0. 

    CMD may be a simple command with separately specified 
    arguments, if any, or a single string containing one or more 
    ;-separated commands (including arguments) - in the former case the command 
    is directly executed by bash, in the latter the string is passed to \`bash -c\`. 
    Note that GNU watch uses sh, not bash. 
    To use \`exec\` instead, specify -x (see below). 

    By default, CMD is re-invoked indefinitely; terminate with ^-C or 
    exit based on conditions: 
    -e, --errexit 
    exits once CMD indicates an error, i.e., returns a non-zero exit code. 
    -E, --okexit [NONSTD] 
    is the inverse of -e: runs until CMD returns exit code 0. 

    By default, all output is passed through; the following options modify this 
    behavior; note that suppressing output only relates to CMD's output, not the 
    messages output by this utility itself: 
    -q, --quiet [NONSTD] 
    suppresses stdout output from the command invoked; 
    -Q, --quiet-both [NONSTD] 
    suppresses both stdout and stderr output. 

    -l, --list [NONSTD] 
    list-style display; i.e., suppresses clearing of the screen 
    before every invocation of CMD. 

    -n secs, --interval secs 
    interval in seconds between the end of the previous invocation of CMD 
    and the next invocation - 2 seconds by default, fractional values permitted; 
    thus, the interval between successive invocations is the specified interval 
    *plus* the last CMD's invocation's execution duration. 

    -x, --exec 
    uses \`exec\` rather than bash to execute CMD; this requires 
    arguments to be passed to CMD to be specified as separate arguments 
    to this utility and prevents any shell expansions of these arguments 
    at invocation time. 

    -t, --no-title 
    suppresses the default title (header) that displays the interval, 
    and (NONSTD) a time stamp, the time elapsed so far, and the command executed. 

    -b, --beep 
    beeps on error (bell signal), i.e., when CMD reports a non-zero exit code. 

    -c, --color 
    IMPLIED AND ALWAYS ON: colored command output is invariably passed through. 

    -p, --precise [NOT IMPLEMENTED] 

    -d, --difference [NOT IMPLEMENTED] 

EXAMPLES 
    # List files in home folder every second. 
    $THIS_NAME -n 1 ls ~ 
    # Wait until all *.lockfile files disappear from the current dir, checking every 2 secs. 
    $THIS_NAME -e 'ls *.lockfile' 

EOF 
    exit 0 
fi 

    # Make sure that we're running on OSX. 
[[ $(uname) == 'Darwin' ]] || die "This script is designed to run on OS X only." 

# Preprocess parameters: expand compressed options to individual options; e.g., '-ab' to '-a -b' 
params=() decompressed=0 argsReached=0 
for p in "[email protected]"; do 
    if [[ $argsReached -eq 0 && $p =~ ^-[a-zA-Z0-9]+$ ]]; then # compressed options? 
    decompressed=1 
    params+=(${p:0:2}) 
    for ((i = 2; i < ${#p}; i++)); do 
     params+=("-${p:$i:1}") 
    done 
    else 
    ((argsReached && ! decompressed)) && break 
    [[ $p == '--' || ${p:0:1} != '-' ]] && argsReached=1 
    params+=("$p") 
    fi 
done 
((decompressed)) && set -- "${params[@]}"; unset params decompressed argsReached p # Replace "[email protected]" with the expanded parameter set. 

# Option-parameters loop. 
interval=2 # default interval 
runUntilFailure=0 
runUntilSuccess=0 
quietStdOut=0 
quietStdOutAndStdErr=0 
dontClear=0 
noHeader=0 
beepOnErr=0 
useExec=0 
while (($#)); do 
    case "$1" in 
    --) # Explicit end-of-options marker. 
     shift # Move to next param and proceed with data-parameter analysis below. 
     break 
     ;; 
    -p|--precise|-d|--differences|--differences=*) 
     dieSyntax "Sadly, option $1 is NOT IMPLEMENTED." 
     ;; 
    -v|--version) 
     echo "$VERSION"; exit 0 
     ;; 
    -x|--exec) 
     useExec=1 
     ;; 
    -c|--color) 
     # a no-op: unlike the GNU version, we always - and invariably - pass color codes through. 
     ;; 
    -b|--beep) 
     beepOnErr=1 
     ;; 
    -l|--list) 
     dontClear=1 
     ;; 
    -e|--errexit) 
     runUntilFailure=1 
     ;; 
    -E|--okexit) 
     runUntilSuccess=1 
     ;; 
    -n|--interval) 
     shift; interval=$1; 
     errMsg="Please specify a positive number of seconds as the interval." 
     interval=$(bc <<<"$1") || dieSyntax "$errMsg" 
     ((1 == $(bc <<<"$interval > 0"))) || dieSyntax "$errMsg" 
     [[ $interval == *.* ]] || interval+='.0' 
     ;; 
    -t|--no-title) 
     noHeader=1 
     ;; 
    -q|--quiet) 
     quietStdOut=1 
     ;; 
    -Q|--quiet-both) 
     quietStdOutAndStdErr=1 
     ;; 
    -?|--?*) # An unrecognized switch. 
     dieSyntax "Unrecognized option: '$1'. To force interpretation as non-option, precede with '--'." 
     ;; 
    *) # 1st data parameter reached; proceed with *argument* analysis below. 
     break 
     ;; 
    esac 
    shift 
done 

# Make sure we have at least a command name 
[[ -n "$1" ]] || dieSyntax "Too few parameters specified." 

# Suppress output streams, if requested. 
# Duplicate stdout and stderr first. 
# This allows us to produce output to stdout (>&3) and stderr (>&4) even when suppressed. 
exec 3<&1 4<&2 
if ((quietStdOutAndStdErr)); then 
    exec &> /dev/null 
elif ((quietStdOut)); then 
    exec 1> /dev/null 
fi 

# Set an exit trap to ensure that the duplicated file descriptors are closed. 
trap 'exec 3>&- 4>&-' EXIT 

# Start loop with periodic invocation. 
# Note: We use `eval` so that compound commands - e.g. 'ls; bash --version' - can be passed. 
tsStart=$(date +'%s') 
while :; do 
    ((dontClear)) || clear 
    ((noHeader)) || echo "Every ${interval}s. [$(date +'%H:%M:%S') - elapsed: $(getElapsedTime $tsStart)]: [email protected]"$'\n' >&3 
    if ((useExec)); then 
    (exec "[email protected]") # run in *subshell*, otherwise *this* script will be replaced by the process invoked 
    else 
    if [[ $* == *' '* ]]; then 
     # A single argument with interior spaces was provided -> we must use `bash -c` to evaluate it properly. 
     bash -c "$*" 
    else 
     # A command name only or a command name + arguments were specified as separate arguments -> let bash run it directly. 
     "[email protected]" 
    fi 
    fi 
    ec=$? 
    ((ec != 0 && beepOnErr)) && printf '\a' 
    ((ec == 0 && runUntilSuccess)) && { echo $'\n'"[$(date +'%H:%M:%S') - elapsed: $(getElapsedTime $tsStart)] Exiting as requested: exit code 0 reported." >&3; exit 0; } 
    ((ec != 0 && runUntilFailure)) && { echo $'\n'"[$(date +'%H:%M:%S') - elapsed: $(getElapsedTime $tsStart)] Exiting as requested: non-zero exit code ($ec) reported." >&3; exit 0; } 
    sleep $interval 
done 
Các vấn đề liên quan