0

I have my python script to schedule the creation of a text file in every 1 minutes of interval. I want to run this file in the background and it should be alive even after restarting the system.

My Python file:

import schedule
import time

#datetime.datetime.now().strftime("%H:%M")


def job():
    print("Scheduling is Working...")
    createfile()

def createfile():
    company = "Example file"
    with open('company.txt', 'w+') as f:
            f.write(str(company))
            print("File Created on:",time.ctime(time.time()))
            f.close()
    return True
# schedule.every(10).minutes.do(job)
# schedule.every().hour.do(job)

#schedule.every().day.at("11.40").do(job)
schedule.every(1).minutes.do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)
  • I'm using systemd to make service
  • os is ubuntu 16 and pytho3

MY Systemd service:

[Unit]
Description=Run scheduler back
After=multi-user.target
[email protected]

[Service]
Type=simple
ExecStart=/usr/bin/python3 /var/www/html/dev/schedulerun.py > /var/log/sanu_daemon.log 2>&1
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

I checked about status, its working fine but not creating the text file I couldn't figure out what is the error.

8
  • 1
    Have you checked permissions? What are the permissions on the script, who is the owner of it, permission on the folder where the file is created, etc.. Commented Mar 31, 2018 at 7:47
  • 1
    It's trying to create file in working folder. What's the working folder of your service? Commented Mar 31, 2018 at 8:06
  • i gave chmod 644 on my folder /var/www/html/dev/ @Vinny . And owner user and www-data Commented Mar 31, 2018 at 8:24
  • 1
    How have you determined that the file is not present? What does ls -l /root/ output? Also note that the lines f.close() and return True don't do anything in your code, they can be safely removed. In addition, if you just want to execute one task regularly, why are you using a systemd service instead of cron? Commented Mar 31, 2018 at 8:25
  • 1
    @frankhk I'll write an answer then. Commented Mar 31, 2018 at 8:39

1 Answer 1

3

You can configure working directory and user in the Service section:

[Service]
WorkingDirectory=/var/www/html/dev/
User=frank
# or www-data, or whatever user you want ...

# Other settings, such as ...
Type=simple
ExecStart=/usr/bin/python3 /var/www/html/dev/schedulerun.py > /var/log/sanu_daemon.log 2>&1
StandardInput=tty-force

For more information, refer to the systemd.exec manpage.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.