From Capum321, 1 Year ago, written in Plain Text.
  1. #
  2. # File name: getIdleTimeN - C PROGRAM compiled to executable - it gets idle time from X.
  3. #
  4. #include <X11/extensions/scrnsaver.h>
  5. #include <stdio.h>
  6. int main(void) {
  7. //  Display *dpy = XOpenDisplay(NULL);
  8.   Display *dpy = XOpenDisplay(":0");
  9.   if (!dpy) {
  10.     fprintf(stderr, "unable to connect to display");
  11.     return(1);
  12.   }
  13.   XScreenSaverInfo *info = XScreenSaverAllocInfo();
  14.   XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
  15.   printf("%lu", info->idle);
  16.   return(0);
  17. }
  18.  
  19. #
  20. # compilation output
  21. #
  22. $ gcc -o getIdleTime getIdleTime.c -lXss -lX11
  23. OK
  24.  
  25. #
  26. # File name: VerinaN - BASH script built upon getIdleTime, which N version is current.
  27. #
  28. #!/bin/bash
  29. idleAfter=7200000     # consider inactive after (7200 s * 1000) ms = 120 min
  30. contador=$(/path/to/getIdleTime4)
  31. if [ "$?" != 0 ]; then
  32.     echo -e "\nERROR: getIdleTime failed"; exit 1;
  33. fi
  34. if [ "$contador" -gt "$idleAfter" ]; then
  35. #  echo $contador  # just for debug purposes.
  36.     rtcwake -s 7200 -m mem   # enter cmd
  37. fi
  38. sleep 1      # polling interval
  39. exit
  40.  
  41. #
  42. # File name: Verina.service - SYSTEMD SERVICE which is triggered to run the BASH wich N version is current.
  43. #
  44. [Unit]
  45. Description=Verif of Inact for Off Time Schedule every 2 h
  46. [Service]
  47. User=`user id n`
  48. ExecStart=/bin/bash /path/to/VerinaN
  49.  
  50. #
  51. # `journalctl -u Verina.service` output
  52. #
  53. $ journalctl -u Verina.service
  54. Set 25 08:44:18 GotaSerena systemd[1]: Started Verif de Inat para Esc de Trab cada 2 h.
  55. Set 25 08:44:18 GotaSerena bash[21394]: rtcwake: /dev/rtc0: unable to find device: Permission denied
  56. Set 25 08:44:18 GotaSerena bash[21394]: rtcwake: assuming RTC uses UTC ...
captcha