-1

I have the following simple python script test.py (toy example)

#!/usr/bin/python
import time
from time import gmtime, strftime
from daemon import runner

import config
import subprocess


class App():
def __init__(self):
    self.stdin_path = '/dev/null'
    self.stdout_path = '/dev/tty'
    self.stderr_path = '/dev/tty'
    self.pidfile_path =  '/tmp/foo.pid'
    self.pidfile_timeout = 5
def run(self):
    while True:
        file = open('/var/www/myfile.dat'+strftime("%Y-%m-%d %H:%M:%S", gmtime()), 'w+')
        file.write("Hello World")
        file.close()            

        time.sleep(20)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

I want to run that script on the boot of Linux Centos 7. For that reason I have added the line in rc.local file

  touch python /root/scripts/test.py start

but unfortunately the script doesn't run.

P.S. When I run that script from command line python test.py start it works perfectly.

Paramore
  • 1,313
  • 2
  • 19
  • 37
  • 1
    Check out https://stackoverflow.com/questions/1603109/how-to-make-a-python-script-run-like-a-service-or-daemon-in-linux – AlG Feb 21 '18 at 19:22
  • I know how to create deamon process in python (script above) the problem is that I can't run it on boot – Paramore Feb 21 '18 at 19:30

1 Answers1

0

Create a cron job. Check this link on how to set up cron jobs on CentOS. Then you can call the python script with @reboot sleep 60 && /usr/bin/python /path/to/script.py >> /path/to/log/file.log

omushpapa
  • 1,663
  • 20
  • 25