Bash script, watch folder, execute command

后端 未结 12 1344
小鲜肉
小鲜肉 2020-12-04 08:57

I am trying to create a bash script with 2 parameters:

  • a directory
  • a command.

I want to watch the

12条回答
  •  不知归路
    2020-12-04 09:19

    If you only need to check for files being created/deleted on top level (not checking subfolders) you might want to use the following.

    It uses few ressources hence it can react quickly, I use it to check for a changed file.

    #!/bin/bash
    
    file="$1"
    shift
    
    tmp=$(mktemp)
    trap 'rm "$tmp"' EXIT
    
    while true; do
        while [ ! "$tmp" -ot "$file" ]; do
            sleep 0.5
        done
        eval "$@ &"
        echo $! > "$tmp"
        wait
    done
    

提交回复
热议问题