LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script hangs (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-hangs-800564/)

junior_unix_user 04-07-2010 08:02 AM

Shell script hangs
 
Hi linux gurus and non gurus.
I have a little problem, hope I'll find a solution here.

Shell script hangs up periodically (after 1-3 weeks of work).
What can cause such behavior?
Content of the script is pretty simple:
an infinite loop in which every 2 seconds I call php script, thats it.
Is there any limitation for shell script execution time?
When I look into ps list it shows me that not only shell script but php script also hangs up. So can php script be the root of the problem?
Any advice, help, assumption will be appreciated.

Thanks in advance.

content of the script:

#!/bin/bash
while [ 1 ];do
php -f /path/to/php/script.php
sleep 2
done

pixellany 04-07-2010 08:06 AM

Quote:

So can php script be the root of the problem?
I would say **highly likely**......In your BASH script, there is not much to go wrong.
If "script.php" hangs, then the container script will hang also.

junior_unix_user 04-07-2010 08:24 AM

Thanks.
Would you mind to explain why when I kill script.php its container script doesn't resume normal operation?
Is it normal?

nuwen52 04-07-2010 08:35 AM

Well, one thing to check... Is the php instance going away?

Another possibility... Does the php script take longer than 2 seconds to run and are there things like locked files that might become a problem? One thing to try is to expand the sleep time to maybe 10 seconds. You can also use that to diagnose the problem by checking if the sleep is running in a ps.

Just some random thoughts.

Edit: Just realized you are not backgrounding the php script, so a previous run interfering isn't likely. But, if you use lock/run files or stuff like that, check to make sure you are cleaning them up right in all cases.

grail 04-07-2010 08:41 AM

Maybe if the php script is the issue you should show what it contains?

pixellany 04-07-2010 08:41 AM

Just guessing:
The container script waits for script.php to finish. If you kill the latter, then that never happens.

In the container script, try adding "echo $?" after the call to script.php. This prints the return code from the php script. In normal operation, it should print "0" each time that script.php runs.

junior_unix_user 04-07-2010 09:22 AM

nuwen52, grail
The script connects to broker gets latest quotes and writes them to DB,
and yes, sometimes it can take longer than 2 seconds to run.
But I guess this can't cause the problem... OR can???

pixellany
Thanks for the idea ($?), I'll try to modify script a little bit
to report me if the script fails.
Will the call to script.php return anything if it hangs?
I mean will I be able to check the return code when script hangs?

Thanks.

catkin 04-07-2010 09:44 AM

Quote:

Originally Posted by junior_unix_user (Post 3927419)
Will the call to script.php return anything if it hangs?
I mean will I be able to check the return code when script hangs?

Experiments can be helpful:
Code:

c@CW8:~$ sleep 60 && echo true || echo non-zero return code $?
c@CW8:~$ ps -ef | grep sleep 60 | grep -v grep
c        10309 10183  0 19:05 pts/2    00:00:00 sleep 60
c@CW8:~$ kill 10309

Terminated
non-zero return code 143

The symptoms described in the OP are entirely consistent with the php script hanging so the quest for a solution is better directed at the php script than the shell script.

If it is not possible to fix the php script so it doesn't hang, it is probably possible to program the shell script to kill it if it does not complete within a reasonable time (and to do any necessary cleaning up).

EDIT: in answer to "will I be able to check the return code when script hangs?" and presuming "script" here refers to the php script then no, you will not. A process running a hung script has not exited so has not set an exit code so has not set a return code (a return code is part of an exit code) so there is no return code to check.

junior_unix_user 04-07-2010 09:53 AM

Thanks for the advice I'll do it this way.


All times are GMT -5. The time now is 11:17 AM.