I have used a script to perform a cron job as follow
URL="http://local.cloudservice.com/process2"
LOG="/var/www/cloudservice/logs/cs.log"
DATETIME=$(date +"%Y.%m.%d-%H:%M:%S")
echo "Started at $DATETIME"
if [[ `ps aux | grep $URL | wc -l` > 1 ]];
then
echo "$DATETIME : Exitting -> More than one Processing" >> $LOG
exit 0
fi
for RETRY in $(seq 1 100);
do
echo "$DATETIME : Running" >> $LOG
if [ $(curl -s --connect-timeout 60 --max-time 300 $URL | grep -cim1 "Job queue is empty\|wtf" ) == "1" ];
then
echo "$DATETIME : Processed" >> $LOG
exit 0
else
echo "$DATETIME : Currently Busy - sleeping " >> $LOG
sleep 1
fi
done
exit 0
DATETIME=$(date +"%Y.%m.%d-%H:%M:%S")
echo "Ended at $DATETIME"
I have used this file in every minute cron. Here I am trying to stop a cron if the request is already processing on that url. But I feel like the 2 request is sometimes running regardless of grepping it.
Here is the log
The code to produce this is
<?php
//a working function inside some class say Processor class
public function linkSmartColAndProduct($shop, &$processed, &$additional_info){
$reference_temp = $processed;
try{
sleep(3);
//code here
// lots of db operation. this db operations is not related with increasing the processed count db operation any way
$processed ++;
$additional_info = "some text value";
if(true) //some condition matches
return true;
//else return false
return false;
}catch(Exception $e){
error_log("Log:" . $e->getMessage());
}
}
//Another function inside some other class say Jobs, calling above function
public function process2(){
$data = //gets value from db consists shop_id, processed, additional_info
$processor = new Processor();
$processor->linkSmartColAndProduct($data->shop_id,$data->processed, $data->additional_info); //calling the above function
$data->update(); // updating the db after the value is changed from above function linkSmartColAndProduct
}
According to the expected code, processed value should have increased, for each cron job, but some times it is duplicated as you can see in picture, processed count 170 and 170 Is my sh file written correctly to block the request? Or is it due to the db operation the other processes are executing ?
Any kinds of insight and suggestion are highly appreciated. Thanks PS: I have already looked into, How do you run a single PHP instance with CRON?
Edit
Also the log from sh file says the process is restricted
2020.09.16-19:52:01 : Running
2020.09.16-20:08:01 : Exitting -> More than one Processing
2020.09.16-20:09:01 : Exitting -> More than one Processing
2020.09.16-20:10:01 : Exitting -> More than one Processing
2020.09.16-20:11:01 : Exitting -> More than one Processing
2020.09.16-20:12:01 : Exitting -> More than one Processing
2020.09.16-19:52:01 : Currently Busy - sleeping
2020.09.16-19:52:01 : Running
2020.09.16-20:13:01 : Exitting -> More than one Processing
2020.09.16-20:14:01 : Exitting -> More than one Processing
2020.09.16-20:15:01 : Exitting -> More than one Processing
2020.09.16-20:16:01 : Exitting -> More than one Processing
2020.09.16-20:17:01 : Exitting -> More than one Processing
2020.09.16-19:52:01 : Currently Busy - sleeping
2020.09.16-19:52:01 : Running
2020.09.16-20:18:01 : Exitting -> More than one Processing
2020.09.16-20:19:01 : Exitting -> More than one Processing
2020.09.16-20:20:01 : Exitting -> More than one Processing
2020.09.16-20:21:01 : Exitting -> More than one Processing
2020.09.16-20:22:01 : Exitting -> More than one Processing
2020.09.16-19:52:01 : Currently Busy - sleeping
2020.09.16-19:52:01 : Running