【问题标题】:Run Python Script Automatically at Startup - Ubuntu 16.04在启动时自动运行 Python 脚本 - Ubuntu 16.04
【发布时间】:2018-11-07 22:56:24
【问题描述】:

我在 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 &

有没有办法解决这个问题?

提前谢谢你。

【问题讨论】:

  • requests+systemd 可能有效。
  • 当您说“失败”时,您会收到什么错误消息?当你做python /bin/web1.py时它会运行吗

标签: python linux ubuntu cron


【解决方案1】:

您的 rc.local 方法应该可以工作,请使用您的完整 python 路径进行检查。如果那是默认的 /usr/bin/python /use/bin/python your_file_py

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多