我已经设置了一台运行 Ubuntu 20.04 的服务器,如果由于任何原因电源故障或重新启动,我希望能够自动运行一个脚本来执行以下操作;
sudo systemctl restart ovenmediaengine.service
export DISPLAY=:0 && obs --startstreaming &
我不确定为什么在服务器重新启动时需要重新启动 ovenmediaengine 服务,它在安装时已经作为服务启用。
我已经研究过使用 crontab 的方法
sudo crontab -e
并在行尾添加:
@reboot /path/to/script.sh
脚本文件包含如上所示的两行,并且也被设置为可执行文件。我还尝试在脚本开始时添加几秒钟的延迟,因为这会阻止我在重新启动时在另一个项目上运行脚本。
我该如何检查可能出了什么问题?
有没有更好的方法来完成我需要做的事情?
sudo systemctl status ovenmediaengine.service
编辑:这是重启前后的输出:
● ovenmediaengine.service - OvenMediaEngine
Loaded: loaded (/lib/systemd/system/ovenmediaengine.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-07-13 08:03:46 BST; 13min ago
Process: 670 ExecStart=/usr/bin/OvenMediaEngine -d (code=exited, status=0/SUCCESS)
Main PID: 750 (OvenMediaEngine)
Tasks: 24 (limit: 4611)
Memory: 23.3M
CGroup: /system.slice/ovenmediaengine.service
└─750 /usr/bin/OvenMediaEngine -d
Jul 13 08:03:40 streambox-PowerEdge-R210-II systemd[1]: Starting OvenMediaEngine...
Jul 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>
Jul 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>
Jul 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>
Jul 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 >
Jul 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>
Jul 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>
Jul 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>
Jul 13 08:03:46 streambox-PowerEdge-R210-II systemd[1]: Started OvenMediaEngine.
后:
● ovenmediaengine.service - OvenMediaEngine
Loaded: loaded (/lib/systemd/system/ovenmediaengine.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-07-13 08:19:45 BST; 7s ago
Process: 2293 ExecStart=/usr/bin/OvenMediaEngine -d (code=exited, status=0/SUCCESS)
Main PID: 2294 (OvenMediaEngine)
Tasks: 30 (limit: 4611)
Memory: 7.5M
CGroup: /system.slice/ovenmediaengine.service
└─2294 /usr/bin/OvenMediaEngine -d
Jul 13 08:19:45 streambox-PowerEdge-R210-II systemd[1]: Starting OvenMediaEngine...
Jul 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>
Jul 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>
Jul 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>
Jul 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>
Jul 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>
Jul 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>
Jul 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>
Jul 13 08:19:45 streambox-PowerEdge-R210-II systemd[1]: Started OvenMediaEngine.
我已添加以下内容sudo crontab -e
@reboot /home/streambox4/startupScript.sh >> /var/log/startupScript.log 2>&1
这是日志文件的输出。这是否意味着由于初始化顺序,它无法启动,因为它需要等待?
No protocol specified
qt.qpa.xcb: could not connect to display :0
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.
答案1
切勿使用sudo
内部脚本。
我更倾向于使用/etc/crontab
over,crontab
因为您可以将使用它的用户添加到 cron 行。另外,您应该添加一个日志以进行调试。最大的优势:所有 cron 作业都在一个文件中,而不是不同的用户 crontab。
@reboot root /path/to/{script}.sh >>/var/log/{script}.log 2>&1
这样你就能发现错误。
您可以sudo
在此处删除并确保包含设置 PATH 或使用绝对路径名:
/bin/systemctl restart ovenmediaengine.service
export DISPLAY=:0 && /usr/bin/obs --startstreaming &
(此处猜测 obs 的目录)
我不确定为什么在服务器重新启动时需要重新启动 ovenmediaengine 服务,它在安装时已经作为服务启用。
journalctl
没有给出任何线索?服务的所有活动都列在那里。包括启动、重启、停止、错误、警告和通知。
如果您愿意,可以将服务添加到问题中,或者针对服务问题提出新的具体问题。
答案2
只需将下面的内容放入 crontab 中,
export DISPLAY=:0 && obs --startstreaming &
我能够让它运行。我不确定为什么通过定义 in 以 root 用户身份运行/etc/crontab
会给我一个 Qt 错误,如原始帖子中所示。我假设如果我将用户设置/etc/crontab
为本地用户而不是 root,它会是等效的吗?