【发布时间】: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”完美运行,现在我想在后台启动时自动运行它。 我尝试了两种方法,但都失败了(脚本没有运行):
- 使用此处的指南使用 crontab:Run Python script at startup in Ubuntu
@reboot python /bin/web1.py &
- 使用此处的指南编辑 rc.local:https://askubuntu.com/questions/817011/run-python-script-on-os-boot
python /bin/web1.py &
有没有办法解决这个问题?
提前谢谢你。
【问题讨论】:
-
requests+systemd可能有效。 -
当您说“失败”时,您会收到什么错误消息?当你做
python /bin/web1.py时它会运行吗