以前に検索した結果、これはできないという回答が出たようですが、私がLinuxの専門家ではないので、それでも聞いてみたかったです。毎秒デスクトップ通知を開始するnodejsで作成された小さなアプリがあります。
import notifier from 'node-notifier'
import {CronJob} from 'cron';
/* Create a cron job that send a desktop notification every second */
const job = new CronJob('* * * * * *', () => {
notifier.notify({
title: 'My notification',
message: 'Hello, there!',
});
}, null, true, 'America/Los_Angeles');
job.start()
私が走るとき、これはうまくいきますnpm run start
。 systemdサービスを使用して実行したいと思います。
[Unit]
Description=should run node app which launch a desktop notification
After=network.target
[Service]
Environment="DISPLAY=:0" "XAUTHORITY=/home/myuser/.Xauthority"
Type=simple
User=myuser
ExecStart=/home/myuser/.nvm/versions/node/v16.13.1/bin/node /home/myuser/notify_send/notify_node/build/index.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
サービスを開始してから数秒後、ステータスコマンドは次のように表示されます。
● runjs.service - should run node app which launch a desktop notification
Loaded: loaded (/etc/systemd/system/runjs.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2022-12-04 17:47:40 CET; 22s ago
Main PID: 5606 (node)
Tasks: 20 (limit: 18651)
Memory: 18.1M
CGroup: /system.slice/runjs.service
├─5606 /home/myuser/.nvm/versions/node/v16.13.1/bin/node /home/myuser/notify_send/notify_node/build/index.js
├─5633 /bin/sh -c notify-send "My notification" "Hello, there!" --expire-time "10000"
├─5634 notify-send My notification Hello, there! --expire-time 10000
├─5639 dbus-launch --autolaunch=017e96ffe51b466384d899f21cbecdc5 --binary-syntax --close-stderr
├─5640 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
├─5642 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
└─5643 /usr/bin/plasma_waitforname org.freedesktop.Notifications
dic 04 17:47:40 slimbook systemd[1]: Started should run node app which launch a desktop notification.
dic 04 17:48:00 slimbook dbus-daemon[5640]: [session uid=1000 pid=5638] AppArmor D-Bus mediation is enabled
dic 04 17:48:00 slimbook dbus-daemon[5640]: [session uid=1000 pid=5638] Activating service name='org.freedesktop.Notifications' requested by ':1.0>
ただし、サービスの実行中はデスクトップ通知は開始されません。
よろしくお願いします。
編集する @edgar-magallonが提案した変更を適用したら、追加情報を追加してください。
$ sudo systemctl status runjs.service
● runjs.service - should run node app which launch a desktop notification
Loaded: loaded (/etc/systemd/system/runjs.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2022-12-24 00:26:59 CET; 4s ago
Process: 3281 ExecStart=/home/user/notify_send/notify_node/build/runApp (code=exited, status=127)
Main PID: 3281 (code=exited, status=127)
dic 24 00:26:59 slimbook systemd[1]: runjs.service: Scheduled restart job, restart counter is at 5.
dic 24 00:26:59 slimbook systemd[1]: Stopped should run node app which launch a desktop notification.
dic 24 00:26:59 slimbook systemd[1]: runjs.service: Start request repeated too quickly.
dic 24 00:26:59 slimbook systemd[1]: runjs.service: Failed with result 'exit-code'.
dic 24 00:26:59 slimbook systemd[1]: Failed to start should run node app which launch a desktop notification.
そしてログ:
$ sudo journalctl -xeu runjs.service
-- Support: http://www.ubuntu.com/support
-- Support: http://www.ubuntu.com/support
--
-- The unit runjs.service has entered the 'failed' state with result 'exit-code'.
dic 24 00:26:59 slimbook systemd[1]: runjs.service: Scheduled restart job, restart counter is at 5.
-- Subject: Automatic restarting of a unit has been scheduled
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Automatic restarting of the unit runjs.service has been scheduled, as the result for
-- the configured Restart= setting for the unit.
dic 24 00:26:59 slimbook systemd[1]: Stopped should run node app which launch a desktop notification.
-- Subject: A stop job for unit runjs.service has finished
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- A stop job for unit runjs.service has finished.
--
-- The job identifier is 2072 and the job result is done.
dic 24 00:26:59 slimbook systemd[1]: runjs.service: Start request repeated too quickly.
dic 24 00:26:59 slimbook systemd[1]: runjs.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- The unit runjs.service has entered the 'failed' state with result 'exit-code'.
dic 24 00:26:59 slimbook systemd[1]: Failed to start should run node app which launch a desktop notification.
-- Subject: A start job for unit runjs.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- A start job for unit runjs.service has finished with a failure.
--
-- The job identifier is 2072 and the job result is failed.
私は何が間違っていましたか?
ベストアンサー1
DBUS_SESSION_BUS_ADDRESS
systemdサービスで環境変数を指定し、RemainAfterExit=yes
セクションでそれを使用する必要があります[Service]
。
私の場合、echo $DBUS_SESSION_BUS_ADDRESS
その価値を得ました。
echo $DBUS_SESSION_BUS_ADDRESS
#output:
unix:path=/run/user/1000/bus
したがって、systemdサービスは次のようになります。
[Unit]
Description=should run node app which launch a desktop notification
After=network.target
[Service]
Environment="DISPLAY=:0" "XAUTHORITY=/home/myuser/.Xauthority" "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus"
Type=simple
User=myuser
RemainAfterExit=yes
ExecStart=/home/myuser/.nvm/versions/node/v16.13.1/bin/node /home/myuser/notify_send/notify_node/build/index.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
実行中なので、/home/myuser/.nvm/versions/node/v16.13.1/bin/node /home/myuser/notify_send/notify_node/build/index.js
systemdサービスでいくつかのエラーが発生する可能性があると思います(依存関係のためにわかりませんが、ノードについてはわかりません)。したがって、/home/myuser/notify_send/notify_node/build
以下のスクリプトを生成することをお勧めします。
アプリケーションの実行
#!/bin/bash
cd "$(dirname $(realpath $0))"
node ./index.js &>/home/user/logs
システムサービス
[Unit]
Description=should run node app which launch a desktop notification
After=network.target
[Service]
Environment="DISPLAY=:0" "XAUTHORITY=/home/myuser/.Xauthority" "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus"
Type=simple
User=myuser
RemainAfterExit=yes
ExecStart=/home/myuser/notify_send/notify_node/build/runApp
Restart=on-failure
[Install]
WantedBy=multi-user.target
Systemd Timers
ノードアプリでクローンアクションを使用する代わりに、より多くの機能を持つノードアプリを使用できます。