Start program in php script, continue w/o program completing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Game_Theory
Forum Newbie
Posts: 4
Joined: Mon Jul 11, 2005 11:54 am

Start program in php script, continue w/o program completing

Post by Game_Theory »

Hi,

I would like to launch a program from within my php script that acts somewhat as a service (it continues to run for a significant amount of time) without tieing up my php script waiting for completion of the program. Can this be done?

Currently I have tried things such as:
system('/path/to/file'); or
shell_exec('/path/to/file'); but each of these make the php script hang until completion of the file called.

If there is some sort of fork or create independent (non-child) process command in php I would greatly appreciate you bringing it to my attention, my searches haven't uncovered anything yet.

As an aside: the file being called is a homebrew c application which runs as suid root to start a service by using c's system command. If the c program could spawn the independent process then return to the php script that would also be an acceptable work-around.

Thanks
Glen
"game theory"
mltsy
Forum Newbie
Posts: 9
Joined: Mon Jul 11, 2005 12:46 pm
Location: Minnesota, USA

Use the Ampersand?

Post by mltsy »

Have you tried shell_exec("path/to/file &");

If you're running *nix that might do the trick.
Game_Theory
Forum Newbie
Posts: 4
Joined: Mon Jul 11, 2005 11:54 am

Post by Game_Theory »

While the & would normally run the process in the background, the php script seems to still hang waiting for completion of the program.

We've tried using fork() and setsid() in the c program to create a child and then have the parent exit/return but the php script seems to still wait for the child to also complete, even after breaking it into its own process group.

In the documentation for pass_thru it mentions re-directing output so that the script doesn't wait for completion of the program. I tried to make all output go to a file but this didn't change anything. Anyone have any other ideas or example code for how to let the script continue while the program continues to run?

Thanks
Game_Theory
Forum Newbie
Posts: 4
Joined: Mon Jul 11, 2005 11:54 am

Post by Game_Theory »

Hey, I've made a lot of progress but still have a lingering problem. My code looks something like this:
System("filelocation > outputfile"); so that the output from the program is re-directed there. The program is written in C and uses fork(), setsid() and the ignore-child signal to create a daemon process. Running the C program alone from the terminal works flawlessly, but launching it via php causes problems.

The program begins to run and it shows up in the process manager properly (as a standalone process childed only to process number 1) However, after a short period of time the process gets terminated. This has to be due to php since it does not get terminated when run from the terminal.

Anyone know why/how php is killing the process? It doesn't even seem like it should be possible, since the process has been forked off and made into a new process group.

Thanks for any help
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

As an alternate solution, that I tend to use:

I have the PHP script "touch" a specified file with a command-line script monitoring that file's last modified date. When it detects a change, it launches the various backend scripts I need.

(A few more details, the backend monitor script runs by cron with * * * * * (ie the backend script is launched every minute).
It then checks if the last modified date on the "request" file is newer than the "last processed" lock file. If it is it grabs an exclusive lock on a third file and does its work, then exists.

If the script runs for more than a few minutes and another request is received, it will process it again after the first one finishes (that's the reason for the third lock file).
Game_Theory
Forum Newbie
Posts: 4
Joined: Mon Jul 11, 2005 11:54 am

Post by Game_Theory »

Thanks for the info nielsene. I eventually did a ktrace and with some extra reading, figured out the problem. PHP sticks a SIGPROF signal on each script so that if it runs for too long it will be killed. Each process started with system() or exec() will inherit this signal and will terminate after the time expires. Putting the line

Code: Select all

signal(SIGPROF, SIG_IGN);
into my C program fixed the problem by simply ignoring that inherited signal.
Post Reply