如何运行使用 systemd 服务启动桌面通知的节点应用程序

如何运行使用 systemd 服务启动桌面通知的节点应用程序

根据我之前的搜索,我认为答案是我不能这样做,但无论如何我想问一下,因为我不是 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您可以使用具有更多功能的节点应用程序,而不是在节点应用程序中使用 cron-jobs

相关内容