'Screencapture over cron shown background instead window content
On macOs Catalina have problems with making screenshots over cron.
When manually run do_screenshot.sh script then all fine.
But when it run auto over cron - probs, only menu correct, instead window content shown macOs background(see pic)
do_screenshot.sh:
#!/bin/bash
DATEFULL=`date '+%Y%m%d%H%M%S'`
FILENAME="/Users/yak/Documents/screenshots/"$DATEFULL.png
/usr/sbin/screencapture -x $FILENAME
Solution 1:[1]
Ran into this problem after the update too. Spent hours learning the details, here is the why and how. Credit goes to big-circle. The original question and answer here.
The problem is the cron doesn't have screen access.
Here's the solution
- close SIP (System Integrity Protection).
Make sure your SIP is disabled. To check if SIP is disabled. To to terminal and type csrutil status. It should say SIP status: enabled/disabled.
To disable it:
Powerdown Mac, start up again and hold down cmd+r until OS X Utilities window shows up. Open terminal type csrutil disable. Restart again, boot into normal mac os.
- grant write permission to TCC
sudo chmod 664 /Library/Application\ Support/com.apple.TCC
grant screencapure privilege to cron and screencapture
a) CRON:
`sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/cron", 1, 1, 1, "", "", "", "UNUSED", "", 0,"")'`b) screencapture:
Pre Big Sur: `sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/screencapture", 1, 1, 1, "", "", "", "UNUSED", "", 0,"")'`--------------------------------------------------------------------------Big Sur and later: `sudo sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" 'insert into access values ("kTCCServiceScreenCapture", "/usr/sbin/screencapture", 1,2,4,1, "", "", "", "UNUSED", "", 0,"")'`It's probably a good idea to turn SIP back on at this stage. To enable it, follow step 1, and instead of
csrutil disablejust typecsrutil enable.
Edit @ 2021-12-09T11:58:00+1000: Added in commands for Big Sur and later per comment from Silvan Mühlemann
Solution 2:[2]
You should allow your script to use the "Screen Recording" in the "Security & Privacy" system preferences.
Solution 3:[3]
- You may start your app in a background from a terminal with a long loop (you need start it manually, that is not good):
#!/bin/bash
for value in {1..980}
do
/Users/<username>/record_activity_with_screen_capture.bash
sleep 60
done
echo All done
OR
use launchd (instead of cron) for running this script. There are two examples:
Solution 4:[4]
Instead of playing with SIP (System Integrity Protection), which is dangerous, but suggested anyways but the top answer, simply give Screen Recording permissions to /usr/sbin/cron (or /sbin/cron in some MacOS versions I think).
In order to do that:
- Open System Settings
- Open Security & Privacy
- Go to the Privacy tab
- Go to Screen Recording
- Click the lock in the bottom left corner and authenticate in order to enable editing the list
- Now, if cron is listed there and unchecked, check it and the job is done. Likely is not listed there at all and you need to add it manually
- Click on + below the list of programs
- In the file picker hit a magic command Cmd+Shift+G, which will show a Go To Folder popup
- Type:
/usr/sbinand hit Enter (if you can't findcronin the next steps, try/sbinhere instead) - You should see a list of files, all with an icon of a black terminal
- Find
cronon the list and hit Open - Check the checkbox next to
cron - Wait for your cron to run the command again.
Voila, it should be capturing screenshots now.
Solution 5:[5]
You can try to change shebang to #!/bin/sh, or change screencapture to exec screencapture
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Cary Crusiau |
| Solution 3 | Constantine Kurbatov |
| Solution 4 | Lukasz Czerwinski |
| Solution 5 | Deni |

