# FILE_NAME="run_Lola_run" # WATCH_DIR="/home/mcleod/test" # LOCK_FILE="${WATCH_DIR}/monitor_file.lock" # PID_FILE="${WATCH_DIR}/monitor_file.pid" # JOB_LOCK_FILE="${WATCH_DIR}/job_monitor_file.lock" # LOG="${WATCH_DIR}/monitor_file_work.log" # ERR_LOG="${WATCH_DIR}/monitor_file_error.log" # RANGE=100
case $1 in "start") start ;; "stop") stop ;; *) usage ;; esac exit
# usage() { echo "$0 (start|stop)" }
# _log() { process=$1 shift echo "${process}[$$]: $*" }
# stop() { # pid , pid if [ -e ${PID_FILE} ] then _pid=$(cat ${PID_FILE}) kill $_pid rt=$? if [ "$rt" == "0" ] then echo "Daemon stop" else echo "Error stop daemon" fi else echo "Daemon is't running" fi }
# start() { # pid if [ -e $PID_FILE ] then _pid=$(cat ${PID_FILE}) if [ -e /proc/${_pid} ] then echo "Daemon already running with pid = $_pid" exit 0 fi fi # touch ${LOG} touch ${ERR_LOG} # , cd / # , exec > $LOG exec 2> $ERR_LOG exec < /dev/null # , . ( # trap "{ rm -f ${PID_FILE}; exit 255; }" TERM INT EXIT # while [ 1 ] do # if ls -1 ${WATCH_DIR} | grep "^${FILE_NAME}$" 2>&1 >/dev/null then _log "parent" "File found" rm -f ${WATCH_DIR}/${FILE_NAME} _log "parent" "File deleted" # number=$RANDOM let "number %= $RANGE" _log "parent" "Genereated number $number" JOBS[${#JOBS[@]}]=$number fi # 0, , if [ "${#JOBS[@]}" -gt "0" ] then if [ ! -e ${JOB_LOCK_FILE} ] then run_job _log "parent" "Running job with pid $!" unset JOBS[0] JOBS=("${JOBS[@]}") _log "parent" "Jobs in queue [${#JOBS[@]}]" fi fi # sleep 1 done exit 0 )& # pid , echo $! > ${PID_FILE} }
# . JOBS, run_job() { # . /, .. ( # trap "{ rm -f ${JOB_LOCK_FILE}; exit 255; }" TERM INT EXIT # if [ ! -e ${JOB_LOCK_FILE} ] then # pid , echo "$$" > ${JOB_LOCK_FILE} _log "child" "Job with pid $$" # seconds=${JOBS[0]} # , , unset JOBS _log "child" "Sleep seconds $seconds" sleep ${seconds} else _log "child" "Lock file is exists" fi # exit 0 )& }
parent[32338]: File found parent[32338]: File deleted parent[32338]: Genereated number 96 parent[32338]: Running job with pid 32385 parent[32338]: Jobs in queue [0] child[32338]: Job with pid 32338 child[32338]: Sleep seconds 96 parent[32338]: File found parent[32338]: File deleted parent[32338]: Genereated number 46 parent[32338]: File found parent[32338]: File deleted parent[32338]: Genereated number 1 parent[32338]: Running job with pid 32694 parent[32338]: Jobs in queue [1] child[32338]: Job with pid 32338 child[32338]: Sleep seconds 46 parent[32338]: Running job with pid 371 parent[32338]: Jobs in queue [0] child[32338]: Job with pid 32338 child[32338]: Sleep seconds 1
Source: https://habr.com/ru/post/151771/
All Articles