'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

enter image description here



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

  1. 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.

  1. grant write permission to TCC

sudo chmod 664 /Library/Application\ Support/com.apple.TCC

  1. 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,"")'`
    
  2. It's probably a good idea to turn SIP back on at this stage. To enable it, follow step 1, and instead of csrutil disable just type csrutil 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]

  1. 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:

  1. how to start bash script in launchd
  2. How to add timing
  3. calendar example

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:

  1. Open System Settings
  2. Open Security & Privacy
  3. Go to the Privacy tab
  4. Go to Screen Recording
  5. Click the lock in the bottom left corner and authenticate in order to enable editing the list
  6. 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
  7. Click on + below the list of programs
  8. In the file picker hit a magic command Cmd+Shift+G, which will show a Go To Folder popup
  9. Type: /usr/sbin and hit Enter (if you can't find cron in the next steps, try /sbin here instead)
  10. You should see a list of files, all with an icon of a black terminal
  11. Find cron on the list and hit Open
  12. Check the checkbox next to cron
  13. 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