Reputation: 838
I try to write from a Python file cronjobs into crontab. When calling the Python file which contains the code for writing into crontab from another Python file via sudo I expect that the cronjob gets added to the crontab file but instead I get this error:
PermissionError: [Errno 1] Operation not permitted: '/usr/bin/crontab'
File 1
with open('/usr/bin/crontab', 'w') as f2:
f2.write("Hello World")
File 2
import os
command = "python3 /Users/myUser/Projects2022/CronjobManager/File1.py"
os.popen("sudo -S %s"%(command), 'w').write('password')
Upvotes: 1
Views: 495
Reputation: 207485
The binary (executable program) for adding cronjobs is stored in /usr/bin/crontab
. That binary actually writes your crontab
entries in a file whose location is dependent on your OS.
On macOS, the file is in /var/at/tabs/USERNAME
, on some systems it is in /var/spool/cron/XXX
.
You can, and should, no more overwrite the executable at usr/bin/crontab
than you can, or should, overwrite /usr/bin/wc
What I am saying is that you need to differentiate between:
/usr/bin/crontab
and/var/spool/cron/XXX
What you might want to do is:
crontab -l > TEMPORARYFILE
crontab TEMPORARYFILE
There's a good description of the tempfile
module here.
Upvotes: 1