crontabが登録されていないか全てのユーザに対し調査する
先日複数サーバそれぞれのユーザでcrontabが設定されていないかどうか確認する必要が出てきたので作ってみました。
サンプルスクリプト
#!/bin/bash
output_dir="/home/sooni/logs"
host_name=$(hostname)
output_file="$output_dir/"$host_name"_cron_check_results.txt"
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "---------------------------------------------------------"  > "$output_file"
echo $host_name "$current_time"  >> "$output_file"
echo "---------------------------------------------------------"  >> "$output_file"
# ユーザーリストを取得し、ループ処理
for user in $(cut -d: -f1 /etc/passwd)
do
    # crontabの内容を表示し、存在する場合はファイルに追記
    #crontab -u "$user" -l >/dev/null 2>&1
    crontab -u "$user" -l > $output_dir/"$user"_crontab.txt
    if [ $? -eq 0 ]
    then
        echo "User $user has a crontab set." >> "$output_file"
    else
        echo "User $user does not have a crontab set." >> "$output_file"
        rm $output_dir/"$user"_crontab.txt
    fi
done 
  
  
  
  