Multi thread in shell script code
your initial code
your loop
{
{
do something
}&
}
end loop
wait
SQLite db lock
sqlite3 -cmd ".timeout 1000" data_base "SQL string"
Example
#!/bin/sh
cd /www/pages/system/
ConfigPath="/www/pages/_config/"
LOGFILE="${ConfigPath}test.log"
echo ${ConfigPath}
echo ${LOGFILE}
## Get camera list from database
array=($(sqlite3 -cmd ".timeout 1000" ../bootstrap/db_camera "SELECT active, status, record, id, address, camera_port, camera_login_id, camera_login_pw, camera_protocol, camera_link FROM db_camera WHERE active=1;"))
for i in "${array[@]}"; do
{
echo ""
echo "*** START"
## Get camera information
c_active=$(echo $i |awk -F '|' '{printf "%s" , $1}')
echo "Data: $i"
if [ $c_active == 1 ];then
c_status=$(echo $i |awk -F '|' '{printf "%s" , $2}')
c_record=$(echo $i |awk -F '|' '{printf "%s" , $3}')
c_id=$(echo $i |awk -F '|' '{printf "%s" , $4}')
c_address=$(echo $i |awk -F '|' '{printf "%s" , $5}')
c_ip=$(traceroute ${c_address} |head -1 |cut -d '(' -f 2 |cut -d ')' -f 1)
c_port=$(echo $i |awk -F '|' '{printf "%s" , $6}')
c_login_id=$(echo $i |awk -F '|' '{printf "%s" , $7}')
c_login_pw=$(echo $i |awk -F '|' '{printf "%s" , $8}')
c_protocol=$(echo $i |awk -F '|' '{printf "%s" , $9}')
c_link=$(echo $i |awk -F '|' '{printf "%s" , $10}')
## Get the status of camera and update to database
camera_live=$(timeout --preserve-status 10s echo >/dev/tcp/${c_address}/${c_port} && echo '1' || echo '0')
sqlite3 -cmd ".timeout 1000" ../bootstrap/db_camera "UPDATE db_camera SET status=${camera_live} WHERE id=${c_id};"
echo "Update db: status = $camera_live"
## Start record process if exists and not in record process
if [ $camera_live == 1 ];then
if [ $c_record == 1 ];then
if [ $c_protocol == "rtsp" ];then
echo "Start record process"
sqlite3 -cmd ".timeout 1000" ../bootstrap/db_camera "UPDATE db_camera SET record=2 WHERE id=${c_id};"
echo "Update db: record = 2"
echo "Get video link"
if [ ${#c_login_id} -ne 0 ];then
camera_login_link="${c_protocol}://${c_login_id}:${c_login_pw}@${c_ip}:${c_port}/${c_link}"
else
camera_login_link="${c_protocol}://${c_ip}:${c_port}/${c_link}"
fi
echo $camera_login_link
/www/pages/raid/ffmpeg -rtsp_transport tcp -i "${camera_login_link}" -c copy -f segment -segment_time 180 -reset_timestamps 1 -strftime 1 /www/pages/raid/Record/camera_${c_id}_%Y%m%d_%H%M%S.mp4 -y &
fi
fi
if [ $c_record == 3 ];then
echo "Stop record process"
pkill -2 -f "camera_${c_id}"
sqlite3 -cmd ".timeout 1000" ../bootstrap/db_camera "UPDATE db_camera SET record=0 WHERE id=${c_id};"
echo "Update db: record = 0"
fi
fi
fi
echo "*** END"
echo ""
}&
done
wait
沒有留言:
張貼留言