繁体   English   中英

Bash 根 crontab 脚本挂起并且不再运行

[英]Bash root crontab script hangs and doesn't run again

更新:我将*/30 * * * * run-one /opt/scripts/staleFile.sh的 run-one 替换为
run-this-one并更新日志。 似乎 run-one 阻止了它,我不确定 run-one 中的锁是如何处理的。 但是我的脚本中有一些东西阻止它释放锁。 我搜索ps aux | grep [script-name] ps aux | grep [script-name]ps aux | grep [PID of original stuck cron command from syslog] ps aux | grep [PID of original stuck cron command from syslog]但没有看到脚本实际上卡住了,所以我认为这是run-one的问题。 我在其他几个 cron 脚本中使用 run-one 并且还没有遇到问题。 如果有人对什么是绊倒它有任何建议,我全神贯注。 /更新

我有一个 bash 根 crontab 脚本,它每 30 分钟运行一次以检查 nfs 陈旧文件句柄,如果存在陈旧句柄,则通过重新挂载 fstab 来修复它。 每次我将数据移动到 nfs 共享时都会发生这种情况,第二天早上 7 点左右再次发生这种情况,因为当数据移动到共享上时,它首先加载到缓存驱动器上,然后在清晨移动到 HDD。 它似乎根据日志成功完成(粘贴在脚本下方),但根据日志文件时间戳需要永远完成(1hr51m)如果遇到过时的句柄并修复它,它将不会再次运行 如果相同的脚本只是以 root 身份运行,即“sudo./staleFile.sh” ,它会快速完成(不到一分钟),并且按预期完成。

我有 docker 容器,这些容器依赖于合并本地数据和来自我的 nfs 共享的数据的合并挂载,这就是我在脚本运行时停止这些容器的原因。

以下是我的 sudo crontab 的相关摘录

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

*/30 * * * * run-one /opt/scripts/staleFile.sh

以下是有问题的脚本

#!/bin/bash
logFile="/av/misc/logs/stale.log" #REMEMBER TO CHANGE!
exec &>> "$logFile"

# Exit if not being run by root user.
if [[ $(/usr/bin/id -u) -ne 0 ]]; then echo "script must be run as root. exiting..."; exit 1; fi

# Get time for log
now="$(/usr/bin/date +'%Y/%m/%d %H:%M')"

# Check for stale file handle, exit script if no problems
if ls /mnt/movies &>/dev/null; then :; else mov=1; fi
if ls /mnt/TV     &>/dev/null; then :; else tv=1; fi
if [[ -z $mov && -z $tv ]]; then echo "$now: ok"; exit 0; fi

echo "Stale file handle...fixing"
echo "----------START----------"
printf "DATE: %s\n" "$now"

if [[ "$mov" && -z "$tv" ]]; then #check if just movies nfs share
    echo "STALE NFS MOVIE FILE HANDLE. FIXING..."
    docker-compose -f /opt/docker-compose.yml stop radarr
    docker-compose -f /opt/docker-compose.yml stop rutorrent
    echo "unmounting /av/mergerfs/movies"
    umount /av/mergerfs/movies
    systemctl stop plexmediaserver.service
    echo "unmounting /mnt/movies"
    umount /mnt/movies
    echo "remounting fstab"
    mount -a
    systemctl start plexmediaserver.service
    echo "remounting /av/mergerfs/movies"
    mergerfs -o allow_other,minfreespace=75G,async_read=false,use_ino,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true,nonempty /av/movies=RW:/mnt/movies=RO /av/mergerfs/movies
    echo "relaunching docker containers"
    mergMovies=$(find /av/mergerfs/movies/* -maxdepth 0 | wc -l)
    mergTV=$(find /av/mergerfs/tv/* -maxdepth 0 | wc -l)
    if [ "$mergMovies" -gt 1000 ]; then docker-compose -f /opt/docker-compose.yml up -d radarr; fi
    if [[ $mergTV -gt 200 && $mergMovies -gt 1000 ]]; then docker-compose -f /opt/docker-compose.yml up -d rutorrent; fi
    docker-compose -f /opt/docker-compose.yml restart reverse
    echo "finished!"
    exit 0
elif [[ -z "$mov" && "$tv" ]]; then #check if just tv nfs share
    echo "STALE NFS TV FILE HANDLE. FIXING..."
    docker-compose -f /opt/docker-compose.yml stop sonarr
    docker-compose -f /opt/docker-compose.yml stop rutorrent
    echo "unmounting /av/mergerfs/*..."
    umount /av/mergerfs/tv
    systemctl stop plexmediaserver.service
    echo "unmounting /mnt/[services]"
    umount /mnt/TV
    echo "remounting fstab"
    mount -a
    systemctl start plexmediaserver.service
    echo "remounting /av/mergerfs/tv..."
    mergerfs -o allow_other,minfreespace=75G,async_read=false,use_ino,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true,nonempty /av/tv=RW:/mnt/TV=RO /av/mergerfs/tv
    echo "relaunching docker containers"
    mergTV=$(find /av/mergerfs/tv/* -maxdepth 0 | wc -l)
    mergMovies=$(find /av/mergerfs/movies/* -maxdepth 0 | wc -l)
    if [ "$mergTV" -gt 200 ];      then docker-compose -f /opt/docker-compose.yml up -d sonarr; fi
    if [[ $mergTV -gt 200 && $mergMovies -gt 1000 ]]; then docker-compose -f /opt/docker-compose.yml up -d rutorrent; fi
    docker-compose -f /opt/docker-compose.yml restart reverse
    echo "finished!"
    exit 0
elif [[ "$mov" && "$tv" ]]; then #must be both
    echo "STALE NFS MOVIE & TV FILE HANDLE. FIXING..."
    docker-compose -f /opt/docker-compose.yml stop radarr
    docker-compose -f /opt/docker-compose.yml stop sonarr
    docker-compose -f /opt/docker-compose.yml stop rutorrent
    echo "unmounting /av/mergerfs/BOTH..."
    umount /av/mergerfs/movies
    umount /av/mergerfs/tv
    systemctl stop plexmediaserver.service
    echo "unmounting /mnt/BOTH"
    umount /mnt/movies
    umount /mnt/TV
    echo "remounting fstab"
    mount -a
    systemctl start plexmediaserver.service
    echo "remounting /av/mergerfs/movies..."
    mergerfs -o allow_other,minfreespace=75G,async_read=false,use_ino,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true,nonempty /av/movies=RW:/mnt/movies=RO /av/mergerfs/movies
    echo "remounting /av/mergerfs/tv..."
    mergerfs -o allow_other,minfreespace=75G,async_read=false,use_ino,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true,nonempty /av/tv=RW:/mnt/TV=RO /av/mergerfs/tv
    #restart docker containers, but check if mergerfs mount was successful based on number of files
    echo "relaunching docker containers"
    mergMovies=$(find /av/mergerfs/movies/* -maxdepth 0 | wc -l)
    mergTV=$(find /av/mergerfs/tv/* -maxdepth 0 | wc -l)
    if [ "$mergTV" -gt 200 ];      then docker-compose -f /opt/docker-compose.yml up -d sonarr; fi
    if [ "$mergMovies" -gt 1000 ]; then docker-compose -f /opt/docker-compose.yml up -d radarr; fi
    if [[ $mergTV -gt 200 && $mergMovies -gt 1000 ]]; then docker-compose -f /opt/docker-compose.yml up -d rutorrent; fi
    docker-compose -f /opt/docker-compose.yml restart reverse
    echo "finished!"
    exit 0
fi

以下是日志的摘录(奇怪的字符来自 docker 在控制台中以绿色突出显示“完成”,在控制台上查看时一切正常):

2020/06/30 04:00: ok
2020/06/30 04:30: ok
2020/06/30 05:00: ok
2020/06/30 05:30: ok
2020/06/30 06:00: ok
2020/06/30 06:30: ok
2020/06/30 07:00: ok
Stale file handle...fixing
----------START----------
DATE: 2020/06/30 07:30
STALE NFS TV FILE HANDLE. FIXING...
Stopping sonarr ... 
[1A[2K
Stopping sonarr ... [32mdone[0m
[1BStopping rutorrent ... 
[1A[2K
Stopping rutorrent ... [32mdone[0m
[1Bunmounting /av/mergerfs/*...
unmounting /mnt/[services]
remounting fstab
remounting /av/mergerfs/tv...
relaunching docker containers
Starting sonarr ... 
[1A[2K
Starting sonarr ... [32mdone[0m
[1BStarting rutorrent ... 
[1A[2K
Starting rutorrent ... [32mdone[0m
[1BRestarting reverse ... 
[1A[2K
Restarting reverse ... [32mdone[0m
[1Bfinished!

如日志中所示,脚本返回“完成!”后它不再在预定的下半小时运行 此外,日志文件上的时间戳是上午 8:51,这意味着它首先需要很长时间(1 小时 51 分钟)才能完成。 我有其他根 crontab 脚本继续按计划运行。

尝试将 cron 从以下位置更改:

*/30 * * * * run-one /opt/scripts/staleFile.sh

对此

*/30 * * * * su - root run-one /opt/scripts/staleFile.sh

添加 su -root 以 root 身份运行脚本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM