I've just made a perfectly working Bash script that runs xprop(by a regular user and root):
#!/bin/bash
# time tracking BASH script
# current time and date
current_date=$(date --rfc-3339='seconds')
# active window id
window_id=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut --fields 2)
# active window class
wm_class=$(xprop '\t$0\n' -id $window_id WM_CLASS | cut --fields 2)
# active window name
wm_name=$(xprop '\t$0\n' -id $window_id _NET_WM_NAME | cut --fields 2)
echo '"'$current_date'", '$wm_class', '$wm_name
with the following output (run as a regular user and root):
nelson@triplecero:~$ bash-scripts/time-tracking.sh
"2019-10-16 23:28:41-04:00", "konsole", "nelson@triplecero: ~ — Konsole"
the script is called from a regular user crontab but then it doesn't work as expected, writing an error messages in the log: "xprop: unable to open display ''", which is the typical error message displayed when xprop (and any GUI program) is not run by the current session user, which is not case because I can run xprop (and any other guy program) as the root user.
The crontab (for the regular user) is like this:
# m h dom mon dow command
* * * * * /home/nelson/bash-scripts/time-tracking.sh >> /home/nelson/log/time-tracking.log 2>&1
* * * * * window_id=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut --fields 2) 2>&1; echo $window_id >> /home/nelson/log/test.log 2>&1
the first line is executed with the following error messages in time-tracking.log:
xprop: unable to open display ''
xprop: unable to open display ''
xprop: unable to open display ''
"2019-10-16 23:21:01-04:00", ,
while the second one just create blank lines in test.log
What am I doing wrong in cron to get those error messages instead of the right output?