Search code examples
python-3.xcron

Python cron - ValueError: improper number of cron entries specified; got 1 need 5 to 7


I have the error:

ValueError: improper number of cron entries specified; got 1 need 5 to 7

I'm using Python 3, and this is my code:

def add_cron():
    cron = CronTab(getpass.getuser()) # For current user
    basic_command = "*/15 * * * * python3 /opt/yesiam.py"
    basic_iter = cron.find_command("yesiam")
    exists=False
    for item in basic_iter:
        if str(item) == basic_command:
            print("crontab job already exist", item)
            exists=True
            break

    if not exists:
        job = cron.new(command='python3 /opt/yesiam.py')
        job.minute.every(15)
        job.enable()
        cron.write()

Does anyone know where I'm going wrong?


Solution

  • The problem is that you are using a completely different package. Its an easy mistake to make, seeing as how they have such similar names.

    That error message is coming from the crontab package.

    Installed using:

    pip install crontab
    

    You want instead the python-crontab package.

    Installed using:

    pip install python-crontab
    

    They are distinct projects, maintained by different people. Check out the open-source repositories here:

    Consider using virtual environments to prevent any conflicts you may have with other projects using conflicting packages and versions.