gpt4 book ai didi

python - 启动时自动运行 Python 脚本 - Ubuntu 16.04

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:34 24 4
gpt4 key购买 nike

我在Ubuntu 16.04.1上,我有一个python脚本可以从网站下载图像文件,代码如下:

import sys
import os
import time
import json
import shlex, subprocess
import signal
import random

R = 0
r = 0

targets = ['192.0.78.13', '134.109.133.7', '216.58.212.227', '54.246.159.107', '185.60.216.35', '98.136.103.24']

if __name__ == "__main__":
while True:
cmd = 'wget -A pdf,jpg,png -m -p -E -k -K -np --delete-after '
R = random.randint(0,5)
cmd += targets[R]

args = shlex.split(cmd)
p = subprocess.Popen(args, shell=False)

time.sleep(2.0)
# killing all processes in the group
os.kill(p.pid, signal.SIGTERM)
if p.poll() is None: # Force kill if process
os.kill(p.pid, signal.SIGKILL)
r = random.randint(3,20)
time.sleep(r-1)

它与命令“python webaccess.py”完美运行,现在我想在后台启动时自动运行它。我尝试了两种方法,但都失败了(脚本无法运行):

  1. 按照此处的指南使用 crontab:Run Python script at startup in Ubuntu

@reboot python /bin/web1.py &

  1. 使用此处的指南编辑 rc.local:https://askubuntu.com/questions/817011/run-python-script-on-os-boot

python /bin/web1.py &

有什么办法可以解决吗?

提前谢谢你。

最佳答案

您的 rc.local 方法应该有效,请使用您的完整 python 路径进行检查。如果这是默认的/usr/bin/python/使用/bin/python your_file_py

你还说你验证了 python webaccess.py 从脚本文件夹之外验证它。还要注意 rc.local 中的 scrips 由 root 执行所以从根目录检查 path_to_python python_file #

关于python - 启动时自动运行 Python 脚本 - Ubuntu 16.04,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50581760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com