当脚本在while循环中运行时,计算机不会关闭

时间:2013-11-29 06:42:10

标签: ubuntu bash startup shutdown shell

我把这个脚本放在/etc/init.d中,当一些用户更改密码时,这个脚本在另一个文件data.txt上更新它,它工作正常,但当我关闭我的电脑时它没有做因此在关闭时显示ubutnu屏幕,当我手动关闭它时,它会自动重启,

通过删除此脚本并注释外部while循环,可以轻松关闭计算机。

这是我的代码

while inotifywait -e attrib /etc/shadow; do
         #edit user
            while IFS=: read -r f1 f2
                do
                user=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 1);
                pwd=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 2);
                    if [ "$f2" != "$pwd" ]; then
                    #echo "changed";
                    #search for password, and repalce it with new one
                    sed -i 's@'$f2'@'$pwd'@' $file;
                    #upload file data.txt to server         
                    fi
            done < $file
        #end edit user
    done

请告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

我相信上述内容对你想要实现的目标不起作用。 该脚本正在等待/ etc / shadow上的“attrib”事件,但是在用户重新启动/停止计算机之前很久就修改了您的文件,因此它将无限期地挂起。 如果要将其用作init脚本,则必须异步执行检查,例如将/ etc / shadow md5sum存储在文件中,然后在关闭时进行比较。这可能是一个可能的解决方案:

#Define some useful variables
SHADOWFILE="/etc/shadow"
SHADOWFILE_OLD_SUM="/root/.sum5_shadow_prev"
DATAFILE="/root/.data.txt" # Or /var/pwdchange/data.txt, ...

# If the file exists, extract previous checksum
if [[ -f "$SHADOWFILE_OLD_SUM" ]]; then
    # Extract previous checksum
    OLDSUM="`cat $SHADOWFILE_OLD_SUM`"
    #Calculate current checksum
    CURSUM="`md5sum $SHADOWFILE | sed 's: .*::'`"
    if [[ "$OLDSUM" == "$CURSUM" ]]; then
        # No modification, exit gracefully
        exit 0
    else
        # Do file parsing and update data accordingly
        while IFS=: read -r f1 f2; do
            user=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 1);
            pwd=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 2);
            if [ "$f2" != "$pwd" ]; then
                #echo "changed";
                #search for password, and repalce it with new one
                sed -i 's@'$f2'@'$pwd'@' $DATAFILE;
                #upload file data.txt to server         
            fi
        done < $DATAFILE
    fi
else
    # First run, calculate current hash, store it, and do an update pass (extract from $SHADOWFILE, as $DATAFILE might not exist)
    md5sum $SHADOWFILE | sed 's: .*::' > $SHADOWFILE_OLD_SUM
    # Remove $DATAFILE regardless whether it exists (avoids duplicates if $DATAFILE exists, as later we do append-only)
    rm -f $DATAFILE
    # Generate first datafile
    while IFS=: read -r f1 f2; do
        user=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 1);
        pwd=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 2);
        echo "$user:$pwd" >> $DATAFILE
    done < $SHADOWFILE
fi