So I was making a script that monitors my app usage. It does so by running a cronjob every minute, and cronjob in question checks which window is focused on and increments its counter by 1.
Other parts of code are insignificant, this is important part:
focused=$(xdotool getwindowfocus)
pid=$(xdotool getwindowpid $focused 2>/dev/null)
[ "$pid" ] &&
pname="$(cat /proc/$pid/comm)" ||
pname="idling"
I tested the script, and running it from dmenu, terminal or i3blocks, pname is what it is supposed to be every time. But when I run it from crontab, echo $pname ends up resulting in idling every single time.
I checked if I'm running my crontab as my user and not as root.
edit: Ideally I want to keep all my cronjobs in personal crontab, not in /etc/crontab for example.
echo $DISPLAYfrom terminal returns ":0" and when I add DISPLAY=":0" in the script that runs as cronjob nothing changes. – Fedja May 14 '22 at 00:58