[PHP] endless loop mimics a cron. Make sure only one instance is running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [PHP] endless loop mimics a cron. Make sure only one instance is running
# 1  
Old 12-24-2008
[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi,

PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime.

I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file.

However, I'd like to make sure that only one instance of the cron script is running. It would also be useful for me to be a 100% certain that the cron script is actually running when it tells me it is.

Can it be done? If yes, how? I have unfortunately no experience in this area.

Regards,

-jj. Smilie
# 2  
Old 12-24-2008
Your script has a log file, right? Have it write to the log file after it's finished doing whatever it does, and before it sleeps:

Code:
echo 'date' Done - going to sleep >> /tmp/script.sleep.log


You can then tail -f the log and watch it roll, check the timestamp on it to see when last updated, figure out when it failed, etc.

I usually count processes by doing something like this:

Code:
ps -aef | grep -v grep | grep very_unique_scriptname | wc -l | bc

# 3  
Old 12-24-2008
Thank you very much. Smilie

I'm very new to this, so please excuse a fire of questions.

Quote:
Originally Posted by tderscheid
Your script has a log file, right? Have it write to the log file after it's finished doing whatever it does, and before it sleeps:

Code:
echo 'date' Done - going to sleep >> /tmp/script.sleep.log

I hadn't thought about a log file. But it makes sense. However, since the script executes itself every 30 seconds, 8 hours a day, 5 days a week, I guess I would only log failures (if not, I would end up with a huge log file). What would be your approach?

Quote:
Originally Posted by tderscheid
You can then tail -f the log and watch it roll, check the timestamp on it to see when last updated, figure out when it failed, etc.
The tail command reads and print the final lines of an input. So I would basically read the log file, correct?

Quote:
Originally Posted by tderscheid
I usually count processes by doing something like this:

Code:
ps -aef | grep -v grep | grep very_unique_scriptname | wc -l | bc

Very interesting, even though it will take some serious time for a command newbie like me to translate it into an understandable sentence Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with accidental endless loop

I was practicing writing simple loops as I am a new bash user and I created this script, which turned out to be an endless loop where the echo output does not stop and I do not see where my mistake is. #!/bin/bash echo 'enter a number from 1 to 100' read number while do ... (2 Replies)
Discussion started by: goldenlinx
2 Replies

2. Shell Programming and Scripting

Script runs in endless loop

Hi, AM very new to shell scripting and try to run a simple do while loop statement, but it ends up running endlessly. please can anyone assist, dunno what am doing wrong, any useful suggestions will be welcomed. #!/bin/ksh ### To check a running process instance #################... (5 Replies)
Discussion started by: bayoo
5 Replies

3. UNIX for Advanced & Expert Users

Running multiple php scripts into one php only, cron mail alert problem...

hi, while separated they produce the usual mail alert and i can see the output... if i write into the php script: <?php system('php -f /var/www/vhosts/domain.com/httpdocs/folder/script1.php'); system('php -f /var/www/vhosts/domain.com/httpdocs/folder/script2.php'); system('php -f... (0 Replies)
Discussion started by: 7stars
0 Replies

4. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

5. Shell Programming and Scripting

KSH - Issue with endless loop.

First time post. I did a search so I didn’t see this specific issue. It seems to be a head scratcher for me. I have an hourly job that on rare occasions, gets into an endless loop. I’ve tried different scenarios but the current version does basically the following. Find all the *.arc files and... (18 Replies)
Discussion started by: Sylvan303
18 Replies

6. Shell Programming and Scripting

Passing variable to child instance of make

hi, I am trying to export a variable from the parent makefile to a child instance of my makefile within the same directory. the relevant code in my parent makefile is: libvar := $(notdir $(basename ../src/file1)) export libvar .PHONY: library library: make -f makechild libvar=file1... (0 Replies)
Discussion started by: bacpp
0 Replies

7. Shell Programming and Scripting

Running php from cron - working up to a point

Hi there, I am struggling to try and work out what is going wrong with a cron job i have running daily which opens up an imap mailbox and extracts a rar file to a directory on the web server. The script works ok when run manually, and when it is running from cron, I can see that it opens up the... (2 Replies)
Discussion started by: nicky77
2 Replies

8. Shell Programming and Scripting

Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this: grep -r "searchstring" dir/subdir/ > listofoccurrences.txt For brevity sake one can enter the intended directory and use this: grep -r "searchstring" . > listofoccurrences.txt which as I found out leads to an endless loop,... (2 Replies)
Discussion started by: figaro
2 Replies

9. Shell Programming and Scripting

Endless Loop

Hi, I'm pretty new to UNIX shell scripting and need some help. We have an Informatica interface that dumps any files that have errors into a directory. I need to check that directory periodically for any of up to 9 files that might be in it and run a specific process for each file found. The... (3 Replies)
Discussion started by: JeffR
3 Replies

10. Shell Programming and Scripting

Endless loop - Fork function failed?

I need a quick script that will serve as a sort of "real time monitor" for watching some log files. I am using Bourne shell in HP-UX 10.20. I have basically created a script that never ends, unless of course I manually terminate it. Here's the script (it's called qhistory): clear echo "REAL... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question