2

I've set up a server running Ubuntu 20.04, and if for any reason the power fails or it is rebooted, I want to be able to have a script run automatically that will do the following;

sudo systemctl restart ovenmediaengine.service
export DISPLAY=:0 && obs --startstreaming &

I'm not sure why the ovenmediaengine service needs to be restarted when the server is rebooted, it is already enabled as a service when it was installed.

I have looked into using crontab by doing

sudo crontab -e

and adding to the end of the line:

@reboot /path/to/script.sh

The script file contains two lines as written above, and it is also set as executable. I have also tried to add a few seconds delay at the start of the script as that was preventing me from running scripts on reboot on a different project.

How would I go about checking what can be wrong?

Is there a better way to do what I need to do here?

Edit: Here are the outputs of sudo systemctl status ovenmediaengine.service before and after a restart:

● 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.

After:

● 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.

I have added the following in sudo crontab -e

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

Here is the output of the log file. Does this mean that due to the order things get initialised, it isn't starting because it needs to wait?

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.

muru
  • 207,228

2 Answers2

5

Never use sudo inside scripts.

I would favor using /etc/crontab over crontab as you can add the user used for it to the cron line. Plus you should add a log for debugging. Biggest advantage: all cron jobs in 1 file instead over different user crontabs.

@reboot root /path/to/{script}.sh  >>/var/log/{script}.log 2>&1

That way you catch errors.

You can remove sudo here and make sure you either include setting a PATH or use absolute path names:

/bin/systemctl restart ovenmediaengine.service
export DISPLAY=:0 && /usr/bin/obs --startstreaming &

(guessing dir for obs here)

I'm not sure why the ovenmediaengine service needs to be restarted when the server is rebooted, it is already enabled as a service when it was installed.

journalctl does not give a clue? All activities for services are listed there. Including start, restart, stop, errors, warnings, and notices.

Add the service to the question or ask a new one specific for the problem with the service if you want.

Rinzwind
  • 309,379
0

Putting only the below in crontab,

export DISPLAY=:0 && obs --startstreaming &

I was able to get it to run. I am not sure why running as root user by defining in /etc/crontab gave me a Qt error as show in the original post. I assume if instead I set the user in /etc/crontab to the local user instead of root, it would be the equivalent?