I usually have 2 Xorg sessions running on my system, each on a different virtual terminal and with a respective system user: one for work and another one for leisure.

I do not switch among them often but when I do I must remember to turn off my time tracker software in the first session and then start it on my target session. This is because rescuetime [1] cannot listen to 2 xorg sessions at the same time.

So having some way of triggering a script everytime an Xorg session is aware that it has come into play, I could kill the running rescuetime process and start a new one.

[1] https://www.rescuetime.com

  • Another approach would be that instead of trying to hook into a virtual terminal switch, you write a custom script that turns the time tracking software on/off and switches to the other VT, and then you bind this script to some hotkey e.g. in your window manager. – dirkt May 7 '19 at 6:00
  • @dirkt I thought about this but I'm afraid that I would easily forget about switching hence I'd have some misleading data in my timetracker. – FaureHu May 7 '19 at 16:37

You might be able to script something around

inotifywait -e modify /sys/devices/virtual/tty/tty0/active
case "$(cat /sys/devices/virtual/tty/tty0/active)" in
    tty7) <dothis> ;;
    tty8) <dothat> ;;
exec $0 $*

The "file" /sys/devices/virtual/tty/tty0/active tells which virtual console is the active one, in terms of tty7 or tty1 etc., and it's changed when you shift from one to another. The inotifywait command simply waits until there's a modify event for that pathname, so the script may then inspect which one it is for doing its associated thing. Thereafter (the exec line) the script reinvokes itself for capturing the next virtual console transition.

On a Debian based Linux, you find inotifywait as part of the inotify-tools package. The /sys/.. pathname requires a duly mounted sysfs, which generally belongs to the Linux kernel since yonks ago.

| improve this answer | |
  • This is a very good starting point, thanks. – FaureHu May 7 '19 at 1:15

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.