Reputation: 114
I am running a Django application with a few cron tasks. Whenever I run python manage.py crontab add
to register the cron tasks, I get a "mail" in the Terminal showing the following traceback:
Traceback (most recent call last):
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 609, in <module>
main()
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 592, in main
known_paths = venv(known_paths)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 510, in venv
with open(virtual_conf, encoding='utf-8') as f:
PermissionError: [Errno 1] Operation not permitted: '/Users/<local_path>/venv/pyvenv.cfg'
I am running on macOS Catalina, python 3.7.3 in a virtual environment venv.
I have tried giving full disk permission to bash, crontab, terminal, xcode and pycharm.
I have also tried installing python versions 3.7.8 and 3.8.3
I disabled System Integrity Protection using csrutil disable
in Recovery Mode and the cron ran successfully. However, I do not see it as full solution.
I dockerized the app and ran it as a docker container.
Upvotes: 3
Views: 8256
Reputation: 619
The reason for the PermissionError is that the Python virtual environment is placed in the user directory. If you put the virtual environment in a system directory, such as /opt, there will be no problem. See example below:
$ sudo mkdir /opt/myvenv
$ cd /opt/myvenv
$ sudo python3 -m venv .venv
$ mkdir ~/workspaces/myapp
$ cd ~/workspaces/myapp
$ cat <<EOF> demo.py
> with open('/Users/username/workspaces/myapp/demo.txt', 'a+') as f:
> f.write('Hello, cron.\n')
> EOF
$ crontab -e
$ crontab -l
* * * * * /opt/myvenv/.venv/bin/python /Users/username/workspaces/myapp/demo.py >> /Users/username/workspaces/myapp/demo.log 2>&1
$ ls -al
total 24
drwxr-xr-x 5 username staff 160B 6 20 09:24 .
drwxr-xr-x 136 username staff 4.3K 6 20 09:18 ..
-rw-r--r-- 1 username staff 0B 6 20 09:23 demo.log
-rw-r--r-- 1 username staff 117B 6 20 09:23 demo.py
-rw-r--r-- 1 username staff 11B 6 20 09:24 demo.txt
Upvotes: 2
Reputation: 751
I was facing the same problem and after a few searching i found a error with cron permissions.
This is how i solved:
Go to System Preferences -> Security & Privacy - Privacy -> Full Disk Access
Click on the + icon to add an item
If the lock appears locked, click on him to unlock with your user password
Press Command + Shift + G to open a dialog
Type /usr/sbin/cron and press Enter
Search and select the cron file and click Open
Okay, now it should work.
Upvotes: 7