ffmpegcronrootdisplay

ffmpeg doesn't work when the script is launched by cron - No protocol specified, Cannot open display :0.0


ffmpeg in my script doesn't work when the script is launched by cron of the root

Error: No protocol specified, Cannot open display :0.0

OS: Ubuntu 20.04

#!/bin/bash
log=/var/log/log2/log2.txt

echo ______________ $(date) >> "$log"
echo "$""DISPLAY" "=      "  "$DISPLAY" >> "$log" ;
echo whoami '         ' $(whoami) >> "$log" 
echo pwd '            ' $(pwd) >> "$log" 
echo "$""USER" "=" '        ' "$USER" >> "$log"
echo PATH '           ' "$PATH" >> "$log"
echo which ffmpeg '   ' $(which ffmpeg)>> "$log" 
echo whereis ffmpeg ' ' $(whereis ffmpeg) >> "$log" 
echo "\nls -l /bin/* | grep ffmpeg" '        ' >> "$log"
ls -l /usr/bin/* | grep ffmpeg >> "$log"
echo "ls -l /var/log | grep log2" ' '>> "$log"
ls -l /var/log | grep log2 >> "$log"

ffmpeg -y -f x11grab -s 1366x768 -i :0.0 -r 25 /var/log/log2/test.mp4 -loglevel error 2>>"$log" &

echo "pid ffmpeg ""$""!"" = " "$!" >> "$log" 
sleep 5
kill "$!"
echo exit >> "$log"
exit

When Cron (Cron of the root) launches the script, ffmpeg shows the error "No protocol specified, Cannot open display :0.0"

/var/log/log2/log2.txt:

______________ ven. 05 mai 2023 04:10:01 CEST
$DISPLAY =       
whoami           root
pwd              /root
$USER =          
PATH             /usr/bin:/bin
which ffmpeg     /usr/bin/ffmpeg
whereis ffmpeg   ffmpeg: /usr/bin/ffmpeg /usr/share/ffmpeg /usr/share/man/man1/ffmpeg.1.gz
ls -l /usr/bin/* | grep ffmpeg  
-rwxr-xr-x 1 root   root      284976 mai   18  2022 /usr/bin/ffmpeg
ls -l /var/log | grep log2  
drwxrwxrwx  2 root              root               4096 mai    5 04:09 log2
pid ffmpeg $! =  74590
No protocol specified
[x11grab @ 0x56244aa06740] Cannot open display :0.0, error 1.
:0.0: Input/output error
exit

When I launch the script manually as a not-root user (with sudo), everything work correctly:

______________ ven. 05 mai 2023 04:10:47 CEST
$DISPLAY =       :0
whoami           root
pwd              /home/an
$USER =          root
PATH             /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
which ffmpeg     /usr/bin/ffmpeg
whereis ffmpeg   ffmpeg: /usr/bin/ffmpeg /usr/share/ffmpeg /usr/share/man/man1/ffmpeg.1.gz
ls -l /usr/bin/* | grep ffmpeg  
-rwxr-xr-x 1 root   root      284976 mai   18  2022 /usr/bin/ffmpeg
ls -l /var/log | grep log2  
drwxrwxrwx  2 root              root               4096 mai    5 04:09 log2
pid ffmpeg $! =  74618
exit

When I launch the script manually as the root, everything works correctly:

______________ ven. 05 mai 2023 04:11:27 CEST
$DISPLAY =       :0
whoami           root
pwd              /root
$USER =          root
PATH             /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
which ffmpeg     /usr/bin/ffmpeg
whereis ffmpeg   ffmpeg: /usr/bin/ffmpeg /usr/share/ffmpeg /usr/share/man/man1/ffmpeg.1.gz
ls -l /usr/bin/* | grep ffmpeg  
-rwxr-xr-x 1 root   root      284976 mai   18  2022 /usr/bin/ffmpeg
ls -l /var/log | grep log2  
drwxrwxrwx  2 root              root               4096 mai    5 04:11 log2
pid ffmpeg $! =  74683
exit

When crontab of an ordinary user launches the script, it works correctly:

$DISPLAY =       
whoami           an
pwd              /home/an
$USER =          
PATH             /usr/bin:/bin
which ffmpeg     /usr/bin/ffmpeg
whereis ffmpeg   ffmpeg: /usr/bin/ffmpeg /usr/share/ffmpeg /usr/share/man/man1/ffmpeg.1.gz
ls -l /usr/bin/* | grep ffmpeg  
-rwxr-xr-x 1 root   root      284976 mai   18  2022 /usr/bin/ffmpeg
ls -l /var/log | grep log2  
drwxrwxrwx  2 root              root               4096 mai    5 05:05 log2
pid ffmpeg $! =  77601
exit

I have tried to put into the script:

export DISPLAY=":0"

export DISPLAY=":0.0"

export DISPLAY=":1"


Solution

  • The problem was solved, the script should just wait a little after restarting the computer