A Sahra
A Sahra

Reputation: 118

Crontab starts again before the process of conversion in ffmpeg ends (depending on time suppose /2 minutes) How to Control that?

I am running a bash .sh file every two minutes with crontab. the problem is that when crontab runs bash file the process of ffmpeg video conversion starts,the conversion time varies depending on length of videos, i have set the crontab to run every two minutes. crontab runs again after two minutes before end of ffmpeg conversion.

How to Figure out:

control of crontab and conversion process so the crontab doesn't starts again until process of conversion is not completed.

#!/bin/bash
# set PATH to check existance of video file in this directory
checkfiles=/home/webuser/public_html/shareportal/convert_Up_videos/*
checkforfiles=/home/webuser/public_html/shareportal/convert_Up_videos
movetodire=/home/webuser/public_html/shareportal/uploaded_videos/
conversionprocessdir=/home/webuser/public_html/shareportal/conversion_process/
movetoArchive=/home/webuser/public_html/shareportal/Video_Archive/
blockpath=/home/webuser/public_html/shareportal/block.txt
processid=/home/webuser/public_html/shareportal/processid.txt
#format of output video file
webm='webm'
if [ "$(ls -A $checkforfiles)" ]
then 
#check directory for files to convert
for f in $checkfiles
    do  
        fullfilename="$f"
        filename=$(basename "$f")
        filewithoutextforimage="${filename%.*}"
        nametofile=$filewithoutextforimage | cut -c1-10;
        echo $filewithoutextforimage | cut -c1-10 1> $blockpath 2>&1
    filewithoutext="${f%.*}"
    fileextention="${f##*.}"
    image_path='/home/webuser/public_html/shareportal/video_images/'$filewithoutextforimage'.png'
    outputfilename=$conversionprocessdir"$filewithoutextforimage.webm"
    #ffmpeg conversion process starts here
    if (ffmpeg -i "$f" "$outputfilename" 1>> $blockpath 2>&1) 
    then 
        #Extract Image of video file on provided time stamp
        if (ffmpeg -ss 00:00:06 -i "$f" -vframes:v 1 "$image_path") 
        then
            echo "Image Extracted"
        else
            echo "Could not Extract Image"
        fi
        echo "Video Converted";
    else
        echo "Could Not Convert Video"
    fi
    #conversion Ends!!
    mv "$outputfilename" $movetodire
    mv "$fullfilename" $movetoArchive
done
else
echo "File Not Found Directory is empty!!!-----"
fi

Upvotes: 0

Views: 394

Answers (2)

A Sahra
A Sahra

Reputation: 118

I have got my problem solved worked perfect,it might help somebody else so i am gonna put here.Thanks for great contribution of Big man @sjsam. This is what i put on the start of my script ,it ensures that there is one instance of script running at the time using flock.

# stop on errors
set -e
scriptname=$(basename $0)
pidfile="/var/run/${scriptname}"

# lock it
exec 200>$pidfile
flock -n 200 || exit 1
pid=$$
echo $pid 1>&200

## Your code:

Source :This guy

Upvotes: 1

sjsam
sjsam

Reputation: 21955

I am having a bite anyway

#!/bin/bash
[ -f /path/to/script_name.pid ] && ps $(</path/to/script_name.pid) &>/dev/null && exit
#exit the script if script is already running
    echo $$ >/path/to/script_name.pid # if the script is not running write pid to file
# Do the ffmpeg conversion
rm /path/to/script_name.pid

Upvotes: 1

Related Questions