Lacer
Lacer

Reputation: 5958

python - working with crontab

I'm fairly new to python. Some googling has got me to this module https://pypi.python.org/pypi/python-crontab. I've setup my env and installed python-crontab==1.9.3. But I keep getting errors. What am I doing wrong? Any help would be greatly appreciate. I'm trying to use examples but they don't seem to be working for me.

What I would like to do is the following:

Terminal Error Output:

    Traceback (most recent call last):
      File "test5.py", line 5, in <module>
        users_cron    = CronTab(user='testuser')
      File "/Users/testuser/Desktop/sample1/prj-env/lib/python2.7/site-packages/crontab.py", line 187, in __init__
        self.read(tabfile)
      File "/Users/testuser/Desktop/sample1/prj-env/lib/python2.7/site-packages/crontab.py", line 231, in read
        raise IOError("Read crontab %s: %s" % (self.user, err))
    IOError: Read crontab testuser: crontab: must be privileged to use -u

Upvotes: 1

Views: 3386

Answers (3)

gerosalesc
gerosalesc

Reputation: 3063

You can also use plan which is an easier way to write cron job for crontab from python:

from plan import Plan

cron = Plan()

cron.command('ls /tmp', every='1.day', at='12:00')
cron.command('pwd', every='2.month')
cron.command('date', every='weekend')

if __name__ == '__main__':
    cron.run()

See more in the docs

Upvotes: 0

CrazyCasta
CrazyCasta

Reputation: 28332

You're trying to access a specific user's crontab, you can't do that on the base system (which is what the python module is trying to use) without root access. If you want to get your own crontab do the following:

users_cron = CronTab(user=True)

Upvotes: 2

Ayush
Ayush

Reputation: 42450

users_cron    = CronTab(user='testuser')

It appears like you are trying to create a cronjob for the user 'testuser'.

IOError: Read crontab testuser: crontab: must be privileged to use -u

The error is telling you that you need to be a privileged user to be able to do that. Try running your script with 'sudo':

sudo python my_python_script.py

Reference

Upvotes: 3

Related Questions