Bash根crontab脚本挂起,不再运行

时间:2020-06-30 15:55:39

标签: linux bash docker cron

更新:我将*/30 * * * * run-one /opt/scripts/staleFile.sh中的运行一替换为
run-this-one,并更新了日志!似乎是奔跑者阻止了它。我不确定运行一号中的锁如何处理,但是脚本中的某些内容阻止了它释放锁。我搜索了ps aux | grep [script-name]ps aux | grep [PID of original stuck cron command from syslog],但没有看到脚本实际上被卡住了,因此我认为运行一个问题。我在其他几个cron脚本中使用了运行一字,但还没有出现问题。如果有人对绊倒有什么建议,我很听。 / UPDATE

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

我有依赖于mergefs挂载的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 AM,这意味着从头开始花费了永久的时间(1小时51m)。我还有其他的crontab根脚本可以按计划继续运行。

1 个答案:

答案 0 :(得分:0)

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

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

对此

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

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