如何在重新启动时运行脚本来重新启动服务并启动 GUI 应用程序?

dar*_*sol 2 reboot bash root scripts cron

我已经设置了一台运行 Ubuntu 20.04 的服务器,如果由于任何原因断电或重新启动,我希望能够自动运行一个脚本来执行以下操作;

\n
sudo systemctl restart ovenmediaengine.service\nexport DISPLAY=:0 && obs --startstreaming &\n
Run Code Online (Sandbox Code Playgroud)\n

我不确定为什么在服务器重新启动时需要重新启动烤箱媒体引擎服务,它在安装时已经作为服务启用。

\n

我已经通过执行以下操作来研究使用 crontab

\n
sudo crontab -e\n
Run Code Online (Sandbox Code Playgroud)\n

并添加到行尾:

\n
@reboot /path/to/script.sh\n
Run Code Online (Sandbox Code Playgroud)\n

脚本文件包含上面写的两行,并且它也被设置为可执行文件。我还尝试在脚本开始时添加几秒钟的延迟,因为这会阻止我在不同项目上重新启动时运行脚本。

\n

我该如何检查可能出现的问题?

\n

有没有更好的方法来完成我需要在这里做的事情?

\n

sudo systemctl status ovenmediaengine.service编辑:以下是重新启动之前和之后的输出:

\n
\xe2\x97\x8f ovenmediaengine.service - OvenMediaEngine\n     Loaded: loaded (/lib/systemd/system/ovenmediaengine.service; enabled; vendor preset: enabled)\n     Active: active (running) since Wed 2022-07-13 08:03:46 BST; 13min ago\n    Process: 670 ExecStart=/usr/bin/OvenMediaEngine -d (code=exited, status=0/SUCCESS)\n   Main PID: 750 (OvenMediaEngine)\n      Tasks: 24 (limit: 4611)\n     Memory: 23.3M\n     CGroup: /system.slice/ovenmediaengine.service\n             \xe2\x94\x94\xe2\x94\x80750 /usr/bin/OvenMediaEngine -d\n\nJul 13 08:03:40 streambox-PowerEdge-R210-II systemd[1]: Starting OvenMediaEngine...\nJul 13 08:03:45 streambox-PowerEdge-R210-II OvenMediaEngine[750]: [2022-07-13 08:03:45.744] W [OvenMediaEngine:750] HLS | hls_publisher.cpp:38   | HLS Publ>\nJul 13 08:03:45 streambox-PowerEdge-R210-II OvenMediaEngine[750]: [2022-07-13 08:03:45.744] W [OvenMediaEngine:750] DASH | dash_publisher.cpp:36   | DASH P>\nJul 13 08:03:45 streambox-PowerEdge-R210-II OvenMediaEngine[750]: [2022-07-13 08:03:45.745] W [OvenMediaEngine:750] LLDASH | cmaf_publisher.cpp:39   | LLDA>\nJul 13 08:03:45 streambox-PowerEdge-R210-II OvenMediaEngine[750]: [2022-07-13 08:03:45.813] W [OvenMediaEngine:750] Thumbnail | thumbnail_publisher.cpp:45 >\nJul 13 08:03:45 streambox-PowerEdge-R210-II OvenMediaEngine[750]: [2022-07-13 08:03:45.935] W [OvenMediaEngine:750] Publisher | publisher.cpp:69   | File p>\nJul 13 08:03:45 streambox-PowerEdge-R210-II OvenMediaEngine[750]: [2022-07-13 08:03:45.935] W [OvenMediaEngine:750] Publisher | publisher.cpp:69   | MPEGTS>\nJul 13 08:03:45 streambox-PowerEdge-R210-II OvenMediaEngine[750]: [2022-07-13 08:03:45.935] W [OvenMediaEngine:750] Publisher | publisher.cpp:69   | RTMPPu>\nJul 13 08:03:46 streambox-PowerEdge-R210-II systemd[1]: Started OvenMediaEngine.\n
Run Code Online (Sandbox Code Playgroud)\n

后:

\n
\xe2\x97\x8f ovenmediaengine.service - OvenMediaEngine\n     Loaded: loaded (/lib/systemd/system/ovenmediaengine.service; enabled; vendor preset: enabled)\n     Active: active (running) since Wed 2022-07-13 08:19:45 BST; 7s ago\n    Process: 2293 ExecStart=/usr/bin/OvenMediaEngine -d (code=exited, status=0/SUCCESS)\n   Main PID: 2294 (OvenMediaEngine)\n      Tasks: 30 (limit: 4611)\n     Memory: 7.5M\n     CGroup: /system.slice/ovenmediaengine.service\n             \xe2\x94\x94\xe2\x94\x802294 /usr/bin/OvenMediaEngine -d\n\nJul 13 08:19:45 streambox-PowerEdge-R210-II systemd[1]: Starting OvenMediaEngine...\nJul 13 08:19:45 streambox-PowerEdge-R210-II OvenMediaEngine[2294]: [2022-07-13 08:19:45.303] W [OvenMediaEngine:2294] HLS | hls_publisher.cpp:38   | HLS Pu>\nJul 13 08:19:45 streambox-PowerEdge-R210-II OvenMediaEngine[2294]: [2022-07-13 08:19:45.303] W [OvenMediaEngine:2294] DASH | dash_publisher.cpp:36   | DASH>\nJul 13 08:19:45 streambox-PowerEdge-R210-II OvenMediaEngine[2294]: [2022-07-13 08:19:45.304] W [OvenMediaEngine:2294] LLDASH | cmaf_publisher.cpp:39   | LL>\nJul 13 08:19:45 streambox-PowerEdge-R210-II OvenMediaEngine[2294]: [2022-07-13 08:19:45.305] W [OvenMediaEngine:2294] Thumbnail | thumbnail_publisher.cpp:4>\nJul 13 08:19:45 streambox-PowerEdge-R210-II OvenMediaEngine[2294]: [2022-07-13 08:19:45.312] W [OvenMediaEngine:2294] Publisher | publisher.cpp:69   | File>\nJul 13 08:19:45 streambox-PowerEdge-R210-II OvenMediaEngine[2294]: [2022-07-13 08:19:45.312] W [OvenMediaEngine:2294] Publisher | publisher.cpp:69   | MPEG>\nJul 13 08:19:45 streambox-PowerEdge-R210-II OvenMediaEngine[2294]: [2022-07-13 08:19:45.312] W [OvenMediaEngine:2294] Publisher | publisher.cpp:69   | RTMP>\nJul 13 08:19:45 streambox-PowerEdge-R210-II systemd[1]: Started OvenMediaEngine.\n
Run Code Online (Sandbox Code Playgroud)\n

我在中添加了以下内容sudo crontab -e

\n

@reboot /home/streambox4/startupScript.sh >> /var/log/startupScript.log 2>&1

\n

这是日志文件的输出。这是否意味着由于事物初始化的顺序,它不会启动,因为它需要等待?

\n
No protocol specified\nqt.qpa.xcb: could not connect to display :0\nqt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.\nThis application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.\n\nAvailable platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.\n
Run Code Online (Sandbox Code Playgroud)\n

Rin*_*ind 5

切勿使用sudo内部脚本。

我更喜欢使用/etc/crontabover crontab,因为您可以将用于它的用户添加到 cron 行。另外,您应该添加用于调试的日志。最大的优点:所有 cron 作业都在 1 个文件中,而不是不同的用户 crontab。

@reboot root /path/to/{script}.sh  >>/var/log/{script}.log 2>&1
Run Code Online (Sandbox Code Playgroud)

这样你就可以捕获错误。

您可以删除sudo此处并确保包含设置 PATH 或使用绝对路径名:

/bin/systemctl restart ovenmediaengine.service
export DISPLAY=:0 && /usr/bin/obs --startstreaming &
Run Code Online (Sandbox Code Playgroud)

(这里猜测 obs 的目录)

我不确定为什么在服务器重新启动时需要重新启动烤箱媒体引擎服务,它在安装时已经作为服务启用。

journalctl没有给出任何线索?此处列出了所有服务活动。包括启动、重新启动、停止、错误、警告和通知。

将服务添加到问题中,或者根据需要针对该服务的问题提出新的问题。