简体   繁体   中英

SyntaxError invalid syntax when running Crontab in Python 3.8

I'm a newbie at this. I'm trying to use Crontab to periodically run a python script to control a stepper motor. When I run 'stepper_motor_cron.py' in terminal,I keep getting the SyntaxError below.


    File "stepper_motor_cron.py", line 4
        * * * * * cd /bleary83/Documents example1.py
          ^
    SyntaxError: invalid syntax

I'm practicing use of Crontab with the 'example1.py' script below. I believe the problem is in designating where the file 'example1.py' is located in the Crontab script. I've tried many variations in the path but invariably come up with the syntax error message.

I can run the 'example1.py' in terminal and I get the correct info in the 'append.txt' file.

I've found crontab in the usr/bin directory.

#! /usr/bin
from crontab import CronTab
cron = CronTab()
* * * * * cd /bleary83/Documents example1.py

The following is the 'example1.py' script I'm trying to run to learn how crontab works. Once I learn how crontab works, I'll try to use it to schedule running the script I have to run the stepper motor using a Rasberry Pi contoller.

from datetime import datetime
myFile = open('append.txt', 'a') 
myFile.write('\nAccessed on ' + str(datetime.now()))

You can't just randomly drop crontab syntax into a Python file and expect it to work. You should read the readme for python-crontab to actually see how to use it. If you had, you'd have seen that you have to set the tab argument of CronTab to your crontab expression:

cron = CronTab(tab="""
  * * * * * cd /bleary83/Documents example1.py
""")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM