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
systemdto 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.
ls -l /root/output? Also note that the linesf.close()andreturn Truedon'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?