Launching OBS from cron not working (but OK from command line)

omega1

New Member
Hi all,

I am trying to get OBS to launch from a cron job, I'm running Debian, my crons run from crontab. What I am trying to achieve is to start a broadcast by getting OBS to start at a particular time.

I am able to do this with a command line script and it works fine, but if I try and run it from a cron, it just wont launch and I can't for the life of me understand why or what the difference is.

This is the script I can run to launch it from command line:
#!bin/bash
su user1 -c "/usr/bin/obs --startstreaming > /dev/null 2>&1 &"


That is saved in a file startobs.sh (I have run chown +x startobs.sh)
If from command line if I run /bin/sh strartobs.sh it launches and starts streaming correctly.

However, if I try and run the following cron inside my crontab
0 0 * * * /home/user1/startobs.sh

It will not launch, the only glimpse of an error I have seen while trying to get it to work is this :
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display


I have checked all the paths, tried running under root, everything I can think of.

I really have no idea why, anyone got any suggestions on how to resolve?

Thanks in advance.
 

tomcroll

New Member
I have finally worked it out.

crontab runs on the console, a tty terminal, not a GUI display.
So you have to instruct the script to run on a DISPLAY.

My setup is Ubuntu 21.0.4
OBS-Studio 27.01.

First I determined my DISPLAY number by typing
echo $DISPLAY
and pressing ENTER.
This gave me 0.

My user crontab:
Edit my user crontab by typing "crontab -e" and pressing ENTER.
This puts you into the vi editor. Look-up vi on commands on Google to work out how to use vi.

Now my crontab has the following line. All I had to do was add "DISPLAY=:0 " before my command to call my script.

55 20 * * * DISPLAY=:0 /home/pats/Documents/LiveStreamFiles/scripts/myscript.sh 2>&1 &

Works like a dream :)

Thanks again and I hope this helps others...
 

wtw_angel

New Member
Hi, I need help.
After reading the solution to run obs via cron. I would like to know how I could run a transmission in obs every 24h
For example: close obs at 23:50 and run obs at 00:00 and restart stream

What does myscript.sh mean? A script to run the obs stream?


Thank you very much for the above solution.
 

AaronD

Active Member
close obs at 23:50
kill -TERM 1234 sends the TERMinate signal to the process id (PID) of 1234.

There are lots of different signals that you can send, and what they do depends on how the program is written. OBS closes gracefully when it gets that one, as if you clicked the Exit button.

Every process has a different id, that is assigned when the process starts. There are ways to determine what the PID is for a running program, but the easiest way is to capture it when the program starts:
Bash:
obs [options] &    # The & at the end is critical!  It tells the script to continue, without waiting for obs to finish.
PID_OBS=$!
...
kill -TERM $PID_OBS
In your case, you might want to save the PID to a file, so you can read it from a different script.

run obs at 00:00 and restart stream
OBS has command line options to do all kinds of things. That's one of them. obs --help to see what they are.

What does myscript.sh mean? A script to run the obs stream?
Yes.
 
Top