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')
CodePudding user response:
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:
- the program
/usr/bin/crontab
and - the table it writes to in
/var/spool/cron/XXX
What you might want to do is:
- extract the user's current crontab to a temporary file, i.e.
crontab -l > TEMPORARYFILE
- edit or append to that file
- re-load that file back to the system, i.e.
crontab TEMPORARYFILE