1

I'm on Ubuntu 16.04.1, and I have a python script to download image files from websites, the codes are as follows:

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)

it run perfectly with command "python webaccess.py", now I want to run it automatically on startup in the background. I've tried two methods but all of them are fail (the scripty does not run):

  1. Use crontab using the guide here: Run Python script at startup in Ubuntu

@reboot python /bin/web1.py &

  1. Edit the rc.local using the guide here: https://askubuntu.com/questions/817011/run-python-script-on-os-boot

python /bin/web1.py &

Is there any way to solve this?

Thank you in advance.

nugie
  • 23
  • 2
  • 5

1 Answers1

0

your rc.local method should work, check using your full python path. if that is default /usr/bin/python /use/bin/python your_file_py

also you said you verified python webaccess.py do verify it from outside the folder of script. also note that scrips in rc.local are executed by root so check path_to_python python_file from root #

bhaskar
  • 3
  • 4