linux - Bash root crontab 脚本挂起并且不再运行
问题描述
更新:我替换了run-one并更新*/30 * * * * run-one /opt/scripts/staleFile.sh
了
run-this-one
日志!看来run-one阻止了它。我不确定如何处理 run-one 中的锁,但我的脚本中的某些内容阻止它释放锁。我搜索了ps aux | grep [script-name]
,ps aux | grep [PID of original stuck cron command from syslog]
但没有看到脚本实际上被卡住了,所以我认为这是 run-one 的问题。我在其他几个 cron 脚本中使用 run-one 并且还没有遇到问题。如果有人对什么是绊倒它有任何建议,我全神贯注。/更新
我有一个 bash root crontab 脚本,它每 30 分钟运行一次以检查 nfs 过时的文件句柄,如果存在过时的句柄,则通过重新挂载 fstab 来修复它。每次我将数据移动到 nfs 共享时都会发生这种情况,第二天早上 7 点左右再次发生这种情况,因为当数据移动到共享上时,它首先加载到缓存驱动器上,然后在清晨移动到 HDD。它似乎根据日志成功完成(粘贴在脚本下方),但根据日志文件时间戳需要永远完成(1hr51m) ,如果遇到过时的句柄并修复它,它将不会再次运行。如果相同的脚本只是以 root 身份运行,即“sudo ./staleFile.sh”,它会快速完成(不到一分钟),并且按预期完成。
我有依赖于合并本地数据和来自我的 nfs 共享数据的 mergefs 挂载的 docker 容器,这就是我在脚本运行时停止这些容器的原因。
以下是我的 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 身份运行脚本
推荐阅读
- django - DRF 序列化器 - 如何返回外键字段?
- ios - 在现有 swift 项目中添加新的 Cocoa Touch 框架与 Objective-C 可可 pod 文件冲突
- java - 具有水平滚动的嵌套 RecyclerView
- typescript - 我们什么时候应该使用 Record
vs { [索引:字符串]:T }?[打字稿 - java - Java Streams 中的 lambda 函数集合
- python-3.x - 逻辑回归系数没有意义
- php - 在jQuery每个函数上制作PHP变量循环
- plot - Netlogo 图不显示结果
- java - 在 HTTP 请求返回值之前阻止函数向前移动
- linux - 如何在 Yocto 启动时自动加载内核模块?(如“insmod /xxx/oo.ko”)