Last active
October 5, 2020 11:10
-
-
Save lesstif/5d275c5d6a7956697198c17ca36b5a89 to your computer and use it in GitHub Desktop.
run some task every x second through cron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
SLEEP_SECOND=7 | |
iter=0; | |
PARAM="s:h" | |
MUTEX=${HOME}/.run-x-seconds | |
LOGFILE=${HOME}/run-logfile | |
function usage { | |
echo "USAGE: $0 [OPTION] "; | |
echo "-s: specify sleep seconds (default ${SLEEP_SECOND})" | |
exit 0; | |
} | |
while getopts $PARAM opt; do | |
case $opt in | |
s) | |
echo "-s option was supplied. OPTARG: $OPTARG" >&2 | |
SLEEP_SECOND=$OPTARG; | |
;; | |
h) | |
usage; | |
;; | |
esac | |
done | |
## avoid over wrapping | |
function check_mutex { | |
if [ -f ~/${MUTEX} ];then | |
echo "mutex file exist!" >&2 | |
exit 1; | |
fi | |
} | |
## implement your task | |
function some_task { | |
check_mutex | |
echo $(date "+%Y-%m-%d %H:%M:%S") >> ${LOGFILE}; | |
} | |
echo $BASHPID > ${MUTEX} | |
for i in $(seq -s " " 1 ${SLEEP_SECOND} 60);do | |
some_task & | |
sleep ${SLEEP_SECOND}; | |
done | |
rm -f ${MUTEX} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment