frank hk
frank hk

Reputation: 127

Systemd service not executing my Python script

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)

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.

Upvotes: 0

Views: 5548

Answers (1)

phihag
phihag

Reputation: 287735

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.

Upvotes: 3

Related Questions