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.
6
  • 1
    You should drop the sudo part, and add it to the crontab of root instead. Also, you haven't actually explained what's not working. Jul 12, 2022 at 14:13
  • You could create a systemd service. Check this answer...
    – kanehekili
    Jul 12, 2022 at 14:21
  • 1
    Since the command suggests that it's already a systemd service, look into why it's not restarting automatically already. That's one of the features of systemd, and you shouldn't need to be scripting anything. Edit your question to show us the complete output of systemctl status ovenmediaengine.service right after a reboot, and again right after you have restarted it.
    – user535733
    Jul 12, 2022 at 14:33
  • @ArturMeinild sorry I wasn't clear enough, I was saying why I wasn't sure why the ovenmediaengine service needs to be restarted when the server is rebooted. Meaning the script that restarts this service fails to execute. Jul 13, 2022 at 7:15
  • @user535733 I added that in the post now. Both seem to indicate that it started correctly without errors. Jul 13, 2022 at 7:28

2 Answers 2

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.

5
  • +1 ... Generally, might do with just using sudo crontab -e without specifying root as the user ... However, specifying the full path to executables like /bin/bash /full/path/to/script.sh as well as any executables used in the script itself is a must.
    – Raffa
    Jul 13, 2022 at 7:56
  • Thanks, I wasn't able to use /etc/crontab - says permission denied. I must be doing something wrong here? I reverted to using sudo crontab -e and will report back. I had a look through journalctl, I don't see any errors relating to ovenmediaengine, it appears that it started fine without errors. Jul 13, 2022 at 9:01
  • Alright I have an update when I add the following in sudo crontab -e @reboot /home/streambox4/startupScript.sh >> /var/log/startupScript.log 2>&1 I will edit and attach to the post. Jul 13, 2022 at 9:09
  • You forgot to put sudo in front of vi /etc/crontab?
    – Rinzwind
    Jul 13, 2022 at 12:35
  • @Rinzwind ok, so I've omitted the command that restarts ovenmediaengine from the script and doing that a different way. So what would be the best way to wait for graphics to be online before executing export DISPLAY=:0 && obs --startstreaming & Jul 14, 2022 at 6:42
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?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .