blu*_*ray 6 networking bash shell-script process-management linux-mint
我正在使用 Linux Mint 20。
我正在使用带有终止开关 ( protonvpn-cli ks --on)的 vpn 。
因此,如果 vpn 连接由于某种原因断开,网络将断开连接。
当网络断开连接时,我的youtube-dl下载永久停止并出现错误
ERROR: Unable to download JSON metadata: <urlopen error [Errno -2] Name or service not known> (caused by URLError(gaierror(-2, 'Name or service not known')))
Run Code Online (Sandbox Code Playgroud)
问题是,我想youtube-dl暂停而不是关闭,并在连接恢复时恢复。
我检查了连接断开连接不起作用时的重试,但我认为这与我的问题无关。
我的配置文件看起来像
--abort-on-error
--no-warnings
--console-title
--batch-file='batch-file.txt'
--socket-timeout 10
--retries 10
--continue
--fragment-retries 10
Run Code Online (Sandbox Code Playgroud)
当我使用批处理文件时,我不想从头开始这个过程。我只想暂停该youtube-dl过程,直到我再次连接,然后继续该过程。
我怎样才能做到这一点?
更新 1:
到目前为止,我发现的是,要暂停一个过程,我们可以执行以下操作:
$ kill -STOP 16143
Run Code Online (Sandbox Code Playgroud)
要恢复流程,我们可以执行以下操作:
$ kill -CONT 16143
Run Code Online (Sandbox Code Playgroud)
我不确定,但认为我们可以通过 ping 1 2来知道我的网络是否已启动:
#!/bin/bash
HOSTS="cyberciti.biz theos.in router"
COUNT=4
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
# 100% failed
echo "Host : $myHost is down (ping failed) at $(date)"
fi
done
Run Code Online (Sandbox Code Playgroud)
然而,这似乎不是一个有效的解决方案。
Linux:在网络连接恢复时执行命令建议使用ifplugd或使用/etc/network/if-up.d/。
还有另一个问题和一篇博客文章提到使用/etc/NetworkManager/dispatcher.d.
当我使用 Linux Mint 时,我认为任何围绕NetworkManager 的解决方案对我来说都会更容易。
这是我现在写的东西,在每一行上运行batch-file.txt并在其上运行 youtube-dl 。
如果没有与网站的连接,您尝试从中下载的内容将循环,直到连接恢复(您可能需要在其上添加一些超时,因为它不会停止。)
我使用了curl预期的200状态代码,因为如果这不发生您可能无法下载该作品。
内容与batch-file.txt之前相同。
运行脚本:
download.sh ./batch-file.txt {download_web_site_url}
# Example:
download.sh ./batch-file.txt google.com`
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
# B"H
# Insted of using youtube-dl batch-file option read the file using bash.
URLS=$1 # "batch-file.txt"
# Pass the URL for site you trying to download from.
SITE_URL=$2
# This function check if url is returning 200 status code.
check_site_connection()
{
# curl to 'your_site_url' with -I option for only the respons info only
# pipe the respons tp awk with 'NR < 2' so only the first line with the status code is printed.
# Return up if respons is 200.
if [[ $(curl -Is $SITE_URL | awk 'NR < 2 {print $2}') == "200" ]];
then
echo up;
else
echo down;
fi
}
# read the batch-file.
while IFS= read -r line
do
# for each line in batch-file loop until conection to site is up with 2 seconds delay.
while [[ $(check_site_connection) == "down" ]]
do
echo "waiting for internet conection"
sleep 2
done
# run youtube-dl where line is the url to dowmload.
./youtube-dl "$line"
done < "$URLS"
Run Code Online (Sandbox Code Playgroud)
编辑:
刚刚在README.md中找到了这个并测试了它的工作原理。
对于这种情况,每行都是一个播放列表,而不是单独的视频。
youtube-dl --download-archive archive.txt URL_TO_PLAYLIST
Run Code Online (Sandbox Code Playgroud)
这将在您每次运行时仅下载新视频,因此如果您添加--download-archive archive.txt到上面的脚本,
./youtube-dl --download-archive archive.txt "$line"
那么如果再次启动,它将遍历所有播放列表,但只会从停止的位置开始下载。