my cognition

書きたいことを書きます。

Ubuntu 22.04 LTS で、デスクトップユーザーのログイン時に Synergy クライアントを systemd で自動的に Synergy サーバーに接続する

GUI による Synergy クライアントのセットアップ

Symless 社公式のドキュメントで、Synergy クライアントを、 Windows 等の Synergy サーバへ、 GUI 上で接続を行う。

Synergy - Installing Synergy 1.13.1 on Ubuntu 20.04 LTS

尚、2023/05/05 現在、 Synergy は Wayland がサポートされていないので、Xorgでログインすること。

Synergy - Wayland workaround for Ubuntu 21.04 and 22.04 LTS

接続が完了した後、プロセスを見ると、以下のように実行時引数が分かるので、後述のIPやポートなどに記載する。

~ $ ps aux | grep synergyc | grep -v grep
username    7240  0.5  0.0 147932 14900 ?        Sl   22:14   0:01 /usr/bin/synergyc -f --no-tray --debug INFO --name CLIENT 192.168.x.y:24800

synergyc.service のセットアップ

ユーザー権限の systemd は、 ~/.config/systemd/user/*.service に配置する。ディレクトリを作成していない場合は、作成を行う。

mkdir -p ~/.config/systemd/user/

synergyc.service を作成する。

vim ~/.config/systemd/user/synergyc.service

内容は、以下のように記載する。IP、ポート、クライアント名などは、環境に合わせること。

[Unit]
Description=Synergy Client Daemon
After=graphical.target

[Service]
Environment=DEBUGLEVEL=ERROR
Environment=SERVER=<IP>
Environment=SERVERPORT=<ポート デフォルトは24800>
Environment=CLIENT=<クライアント名>
ExecStart=/usr/bin/synergyc --no-daemon --debug ${DEBUGLEVEL} --name ${CLIENT} ${SERVER}:${SERVERPORT}
ExecStop=/bin/kill ${MAINPID}
Restart=no

[Install]
WantedBy=default.target

systemctl で起動を行う。

systemctl --user daemon-reload
systemctl --user start synergyc

systemctl の操作をしてみて、 stop した場合は操作が途切れ、 start した場合は操作が可能になる、といったように、意図通りの挙動となるか確認する。

systemctl --user stop synergyc
systemctl --user start synergyc

systemctl の start, stop が意図通りに動作することを確認すれば、 enable を実行することで、次回から自動で起動してくる

systemctl --user enable synergyc

接続に失敗する場合は、 Environment=DEBUGLEVEL=INFO などに変更し、systemctljounalctl コマンドで、ログを確認すること。

## systemctl で直近のログを確認
systemctl --user status synergyc

## journalctl で全文を確認
journalctl --user -u synergyc.service

制約: Ubuntuのユーザログイン後すぐに有効にならず、1分後に有効になる

Synergy クライアントは、 Xorg のディスプレイを必要とする。そのため、GUI上でログインが完了した状態の後で起動する必要がある。しかし、筆者の調べた限りでは、 systemd の制御によってデスクトップログインを検知する方法が見当たらなかった。

尚、 Synergy クライアントが、接続に失敗した際は、自動で1分毎に再接続を試行する。そのため、ログイン後1回目の接続試行に失敗し、2, 3回目の接続試行時に成功する。その様子は、ログレベルをINFOに設定すると確認できる。

 5月 04 23:40:14 client systemd[935]: Started Synergy Client Daemon.
 5月 04 23:40:15 client synergyc[955]: [2023-05-04T23:40:15] INFO: Failed to enable system idle sleep
 5月 04 23:40:15 client synergyc[955]: [2023-05-04T23:40:15] WARNING: secondary screen unavailable: unable to open screen
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] WARNING: Can't open Xkb display during reading languages
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] NOTE: started client
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] NOTE: connecting to '192.168.x.y': 192.168.x.y:24800
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] WARNING: Can't open Xkb display during reading languages
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] INFO: Local languages: en
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] INFO: Remote languages: ja
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] WARNING: You need to install these languages on this computer and restart Synergy to enable support for multiple languages: ja
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] WARNING: LANGUAGE_DEBUG Poll result 0
 5月 04 23:41:15 client synergyc[955]: [2023-05-04T23:41:15] NOTE: connected to server
 5月 04 23:42:11 client synergyc[955]: [2023-05-04T23:42:11] INFO: entering screen
 5月 04 23:42:11 client synergyc[955]: [2023-05-04T23:42:11] INFO: clipboard was updated

補足: synergyc の起動時オプションについて

  • systemctl で制御するにあたって、 --daemon オプションが有効だとうまくいかないため、 --no-daemon を指定している
  • --debug を WARNING 以下にすると、キーボードのストロークが全て記載されるので、ログ量が多くなるほか、機微な情報がログに記載される可能性があるので、ログレベルを設定する場合は注意する。

以下に、参考に help を実行した場合の出力を記載する。

~$ synergyc --help
Usage: synergyc [--yscroll <delta>] [--sync-language] [--invert-scroll] [--host] [--display <display>] [--no-xinitthreads] [--daemon|--no-daemon] [--name <screen-name>] [--restart|--no-restart] [--debug <level>] <server-address>

Connect to a synergy mouse/keyboard sharing server.

  -d, --debug <level>      filter out log messages with priority below level.
                             level may be: FATAL, ERROR, WARNING, NOTE, INFO,
                             DEBUG, DEBUG1, DEBUG2.
  -n, --name <screen-name> use screen-name instead the hostname to identify
                             this screen in the configuration.
  -1, --no-restart         do not try to restart on failure.
*     --restart            restart the server automatically if it fails.
  -l  --log <file>         write log messages to file.
      --no-tray            disable the system tray icon.
      --enable-drag-drop   enable file drag & drop.
      --enable-crypto      enable the crypto (ssl) plugin.
      --tls-cert           specify the path to the tls certificate file.
      --display <display>  connect to the X server at <display>
      --no-xinitthreads    do not call XInitThreads()
  -f, --no-daemon          run in the foreground.
*     --daemon             run as a daemon.
      --yscroll <delta>    defines the vertical scrolling delta, which is
                             120 by default.
      --sync-language      set this parameter to enable language synchronization.
      --invert-scroll      invert scroll direction on this computer.
      --host               client starts a listener and waits for a server connection.
  -h, --help               display this help and exit.
      --version            display version information and exit.

* marks defaults.

The server address is of the form: [<hostname>][:<port>].  The hostname
must be the address or hostname of the server.  The port overrides the
default port, 24800.

synergyc: a server address or name is required
Try `synergyc --help' for more information.

参考文献

Synergy - ArchWiki
systemd の Unit定義ファイル で 変数 を使用する方法 - Qiita
systemctlでよくつかうオプションまとめ - かべぎわブログ
systemdでプロセス自動再起動 | 晴耕雨読
ネットワークを利用するサービスの起動を遅延する – ビットログ
tightvnc - How to set a systemd unit to start after loading the desktop? - Unix & Linux Stack Exchange
systemd .timerについて調べた事を記事にしておく | そう備忘録
OS起動時にsystemdで行われていること - Qiita