The following is a script to calculate the idle time of a machine. The script is supposed to check xserver idle time at a time interval of 1 minutes and log the result to a location via cronjob. But that is not working as i expected. It is not reporting the idle time even if the machine idles for more than the idle time limit assigned.

The time limit in the example is 1 minute. It is just a dummy time set for asking this question and checking without wasting time.

Can anybody help ?

timeinmin="$(($timeinmsec / 60000))"
if [[ "$timeinmin" -gt 1 ]]
        echo "$timeinmin Minutes idle time reported" >> /home/user/log.txt;
        echo $(date '+%X') >> /home/user/log.txt;
        echo "$line" >> /home/user/log.txt;
        echo "No idle time reported" >> /home/user/log.txt;
        echo $(date '+%X') >> /home/user/log.txt;
        echo "$line" >> /home/user/log.txt;

Output i get

No idle time reported
05:24:01 PM

No idle time reported
05:25:01 PM

No idle time reported
05:26:01 PM

No idle time reported
05:27:01 PM
  • Have you tried to output the raw data ? echo $timeminsec ? Or that the process, running from crontab, don't have an active xserver session? – Soren A Dec 17 '17 at 14:01
  • @SorenA Thanks for the comment. I fixed the issue.. i had to export the DISPLAY as it is being run by cronjob. Your comment lead me to check that. thanks again. – Rooney Dec 17 '17 at 15:26

Managed to fix the issue.I had to add export DISPLAY=:0 at the top of the script as this task being run by cronjob and its calling xserver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.