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:
- add a cron job to the cron tab
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
-
1You need to run it as root. Non privileged user can only work on their own crontab.alvits– alvits2015年07月01日 22:28:40 +00:00Commented Jul 1, 2015 at 22:28
3 Answers 3
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
Comments
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)
Comments
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