Pi1

From dtype.org

systemd services

/etc/systemd/system/autossh.service

[Unit]
Description=AutoSSH tunnel service
After=network.target

[Service]
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 110" -o "ServerAliveCountMax 3" -o ExitOnForwardFailure=yes -N -i /root/.ssh/id_rsa -R "[::]:62222:localhost:22" r@j1.alt.org
Restart=always

[Install]
WantedBy=multi-user.target

option 2

[Unit]
Description=Service to maintain an ssh reverse tunnel
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=0

[Service]
Type=simple
ExecStart=/usr/bin/ssh -qNn \
  -o ServerAliveInterval=30 \
  -o ServerAliveCountMax=3 \
  -o ExitOnForwardFailure=yes \
  -o StrictHostKeyChecking=no \
  -o UserKnownHostsFile=/dev/null \
  -i /etc/sshtunnel/id_rsa \
  -R 9001:localhost:22 \
  sshtunnel@server.net -p 443
Restart=always
RestartSec=60

[Install]
WantedBy=multi-user.target

ffmpeg-driveway.service

[Unit]
Description=ffmpeg driveway feed
After=network.target

[Service]
User=drew
Group=drew
ExecStart=/var/www/html/driveway/ffmpeg.sh
Restart=always

[Install]
WantedBy=multi-user.target

ffmpeg-live.service

[Unit]
Description=ffmpeg driveway feed live
After=network.target

[Service]
User=drew
Group=drew
ExecStart=/home/drew/live_stream.sh

[Install]
WantedBy=multi-user.target

service supporting scripts

/var/www/html/driveway/ffmpeg.sh

#!/bin/bash
cd /var/www/html/driveway/
/usr/bin/ffmpeg -loglevel quiet -i rtsp://192.168.42.109:554/CH001.sdp?tcp -timelimit 86400 -c copy -f segment -segment_time 600 -segment_atclocktime 1 -reset_timestamps 1 -strftime 1 "driveway-%Y%m%d-%H%M%S.mp4"

/home/drew/live_stream.sh

#!/bin/bash
ffmpeg -i rtsp://192.168.42.109:554/CH001.sdp?tcp -f lavfi -i anullsrc -c:v copy -c:a aac -t 5:00 -f flv rtmp://a.rtmp.youtube.com/live2/YOUTUBEKEY

sqs-consumer.sh

#!/usr/bin/python3 -u
import boto3
import os
import time

POLLSLEEP = 1
POLLTIME = 20

sqs = boto3.resource('sqs')
snsclient = boto3.client('sns')

queue = sqs.get_queue_by_name(QueueName='host-pi1')

while True:
    for message in queue.receive_messages(
        MessageAttributeNames=['command'],
        MaxNumberOfMessages=1,
        WaitTimeSeconds=POLLTIME
    ):
        command = ''
        print("Received message")
        print(message.message_attributes)
        if message.message_attributes is not None:
            command = message.message_attributes.get('command').get('StringValue')
        if command == 'ffmpeg-live':
            print("Executing service ffmpeg-live start")
            os.system("service ffmpeg-live start")
        message.delete()
        response = snsclient.publish(
            TargetArn="arn:aws:sns:us-east-1:826233342239:emaildrew",
            Message="SQS message received by pi1!"
        )
    time.sleep(POLLSLEEP)