如何设置使用 wget 下载的计时器

问题描述 投票:0回答:1

你好,我在下面列出了我的问题

功能:通过此我将网址从 urls.txt 下载到lines.txt

“wget -i urls.txt -O ->>lines.txt”

---> 问题:如何设置一个以分钟为单位的计时器,并在下载每个网址后删除重复项? (或下载全部后。?)还需要继续24/7下载,直到我停止脚本

有人可以为我创建一个 .sh 脚本来设置 cronjob 来执行此操作吗?或者还有其他解决方案吗?我需要它每 5 分钟下载一次并独一无二

提前谢谢你

我尝试研究 cronjob 但没有成功,我还读了一些关于超时的内容

python bash wget sudo data-retrieval
1个回答
0
投票

您可以通过将

wget
与其他 shell 命令结合使用并使用 cronjob 安排脚本每 5 分钟运行一次来实现此目的。以下是您可以创建的示例 shell 脚本 (
download_and_unique.sh
):

#!/bin/bash

# Set the timer in minutes
TIMER=5

# Function to download URLs from urls.txt and remove duplicates
download_and_unique() {
    wget -i urls.txt -O - | sort -u >> lines.txt
}

# Loop to download and remove duplicates until script is stopped
while :
do
    download_and_unique
    sleep $(($TIMER * 60)) # Convert minutes to seconds for sleep
done

保存此脚本文件,使其可以使用

chmod +x download_and_unique.sh
执行,然后您可以使用
./download_and_unique.sh
运行它。

要使用 cron 将其安排为每 5 分钟运行一次,您可以使用

crontab -e
编辑 crontab 文件并添加以下行:

*/5 * * * * /path/to/download_and_unique.sh

/path/to/download_and_unique.sh
替换为保存脚本的实际路径。

此脚本将每 5 分钟持续从

urls.txt
下载 URL,删除重复项并将唯一行附加到
lines.txt
,直到手动停止脚本

© www.soinside.com 2019 - 2024. All rights reserved.