deepch / RTSPtoWeb

RTSP Stream to WebBrowser
MIT License
1.18k stars 284 forks source link

run app on boot #326

Open ruirigel opened 1 year ago

ruirigel commented 1 year ago

how can i run your app on crontab when the machine boots, since I can't do it this way?

@reboot cd /home/y/RTSPtoWeb/ && go run *.g

Thank you

robertcoroianu commented 1 year ago

If you want to run it at machine startup, for Linux I recommand you to create a new service and use a built version. https://www.shellhacks.com/systemd-service-file-example/

geofreybingachie commented 1 year ago

how can i run your app on crontab when the machine boots, since I can't do it this way?

@reboot cd /home/y/RTSPtoWeb/ && go run *.g

Thank you

Did you manage to create a unit that start at boot ? I am looking to do the same thing but i am not very good with linux.

ruirigel commented 1 year ago

how can i run your app on crontab when the machine boots, since I can't do it this way? @reboot cd /home/y/RTSPtoWeb/ && go run *.g Thank you

Did you manage to create a unit that start at boot ? I am looking to do the same thing but i am not very good with linux.

what i did was, without use a built or a service: open terminal and write $ nano file.sh past the lines below

#/bin/bash
cd /home/$USER/RTSPtoWeb/ && go run *.go

ctrl +x to save and exit

Tell crontab to boot the file on startup open the terminal and call $ crontab -e in the end past the line below

@reboot ./home/$USER/file.sh

ctrl +x to save and exit

geofreybingachie commented 1 year ago

Thank you , with your help i managed to setup the stream that displays on a grafana interface that i use to monitor my solar system.

image

ITwrx commented 1 year ago

I have my RTSPtoWeb dir in my Downloads folder, and since RTSPtoWeb expects config.json to be in the same dir as the binary, i just left it all there.

create the service file

touch /etc/systemd/system/RTSPtoWeb.service

add service file contents. using midnight commander in this example.

mcedit /etc/systemd/system/RTSPtoWeb.service

/etc/systemd/system/RTSPtoWeb.service

[Unit]
Description=RTSPtoWeb. Converts rtsp streams to mp4 segments and serves via web app.

[Service]
ExecStart=/home/itwrx/Downloads/RTSPtoWeb/RTSPtoWeb
WorkingDirectory=/home/itwrx/Downloads/RTSPtoWeb

[Install]
WantedBy=multi-user.target

systemctl daemon-reload

systemctl start RTSPtoWeb.service

if it runs successfully:

systemctl enable RTSPtoWeb.service

and you're done.

note for Fedora/SELinux users.

SELinux may prevent the binary from running due to location, perms, flags, etc. You can either make things the way SELinux expects, or just check the systemd log and run the commands SELinux gives you, to allow the action in the future.

journalctl -b -0 and then hitting the end btn will get you to the bottom of the systemd log each time you attempt to run the binary, so you can see what it's problem was. It may take a few attempts and "allows" to get RTSPtoWeb to run.

Rayvenhaus commented 1 year ago

/That doesn't work on Debian 12 (bookworm)

My service file is:

`[Unit] Description=RTSPtoWeb. Converts rtsp streams to mp4 segments and serves via web app.

[Service] WorkingDirectory=/home/rayvenhaus/RTSPtoWeb ExecStart=/home/rayvenhaus/RTSPtoWeb

[Install] WantedBy=multi-user.target`

And when I start then check the service, I get the following.

rayvenhaus@alexis:./RTSPtoWeb$ sudo systemctl start RTSPtoWeb.service rayvenhaus@alexis:./RTSPtoWeb$ systemctl status RTSPtoWeb.service RTSPtoWeb.service - RTSPtoWeb. Converts rtsp streams to mp4 segments and serves via web app. Loaded: loaded (/etc/systemd/system/RTSPtoWeb.service; disabled; preset: enabled) Active: failed (Result: exit-code) since Mon 2023-06-26 18:51:40 AEST; 4s ago Duration: 2ms Process: 10773 ExecStart=/home/rayvenhaus/RTSPtoWeb (code=exited, status=203/EXEC) Main PID: 10773 (code=exited, status=203/EXEC) CPU: 786us rayvenhaus@alexis:~/RTSPtoWeb$

I can't figure out why, nothing I find in the Googleverse leads to a fix. Anyone with any ideas?

ITwrx commented 1 year ago

@Rayvenhaus according to my web search, the exit code 203/exec means systemd wasn't able to execute the binary. journald -b -0 would likely give more info, but i don't know that we need it.

The WorkingDirectory and ExecStart in your service file are the same as each other, and both appear to reference the RTSPtoWeb directory.

In my example, the ExecStart has an extra "/RTSPtoWeb" which is referencing the actual RTSPtoWeb binary. This is what systemd needs to be able to execute. The WorkingDirectory needs to reference it's parent folder/directory. Also, the RTSPtoWeb binary must be executable, at least by the root user.

Rayvenhaus commented 1 year ago

It took a little bit, but I was finally able to get it working on Debian 12

RTSPtoWeb.service

[Unit]
Description=RTSPtoWeb. Converts rtsp streams to mp4 segment mpeg streams and serves via web app.
After=network.target

[Service]
ExecStart=/bin/bash -c 'cd /home/rayvenhaus/RTSPtoWeb && GO111MODULE=on /usr/local/go/bin/go run *.go'
WorkingDirectory=/home/rayvenhaus/RTSPtoWeb
User=rayvenhaus
Group=rayvenhaus
PIDFile=/var/run/RTSPtoWeb.pid
Restart=on-failure

ExecStop=/bin/kill -SIGTERM $MAINPID
TimeoutStopSec=5

[Install]
WantedBy=multi-user.target

Everything is running as a service now. Thanks for your help.

ITwrx commented 1 year ago

I'm glad it's running, but I don't think there's anything specific to Debian 12 going on here. Maybe you never built a binary?

Just for the record, my service file example's ExecStart= assumes there's a built binary in the WorkingDirectory. It's not expected to run the source .go files.

You're welcome too. Sometimes it just helps to bounce stuff off of people until you get it going your way. :)

Rayvenhaus commented 1 year ago

Good point @ITwrx. And that would be because I have no clue on how to build a go binary. I've never worked with the go language before today.

ITwrx commented 1 year ago

I don't have much experience with go either. Looking through my terminal history it looks like i just ran GO111MODULE=on go build in the dir of the source files, but take that with a grain of salt. :)