The problem is solved! There are 2 ways (my system is Sabayon-Linux):
1. Using a script in /lib64/systemd/system-sleep/ folder. I created a kill_n_start.sh script with the contents
#!/bin/shcase $1/$2 inpre/*)# Execute before going to sleeppkill TeamViewersystemctl stop teamviewerd;;post/*)# Execute after wake upsystemctl start teamviewerd;;esac
It started working immediately without rebooting - TeamViewer closes before PC puts itself to sleep and starts when PC leaves sleep mode.
2. The same can be achieved with a very similar script in /usr/lib/pm-utils/sleep.d/. Described in detail here: https://askubuntu.com/questions/226278/run-script-on-wakeup