Using PHP exec() or similar

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
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Using PHP exec() or similar

Post by mjseaden »

Sami | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Dear All,

Is there a PHP command, such as [php_man]exec()[/php_man], that I can use that will suspend operation until the completion and termination of the command given in exec(). To give an example;

Code: Select all

<?php
exec('sleep 10');
?>
on a UNIX machine does not wait for 10 seconds before continuing operation. Is there a way in which I can do this? It's pretty important!

Many thanks,

Mark
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

[php_man]sleep[/php_man]()
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi mudkicker,

I'm talking about a generic approach. I simply want a shell execution command that doesn't continue processing the PHP script until after the command has been executed.

So for instance above, I would want the PHP script to suspend execution for 10 seconds. exec() would start, and then wait until the command had finished before resuming the PHP script. In this sense, if I echoed 'start' before exec() and 'finish' after there would be a 10 second delay between echoing 'start' and 'finish'.

Also, exec() doesn't seem to report any errors even if I put a random program that doesn't exist into its brackets ().

Any ideas what's wrong?

Many thanks

Mark
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

couldn't you just put the exec() in a if() or while loop to stop the rest of the php script from running while the command is doing its thing?

there is also [php_man]passthru[/php_man] and [php_man]system[/php_man] that might do what you are looking for.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi phpScott, I'm actually now using system('cmd', $retval).

The return value is consistently reporting as 127, with no varience whether I put a random program (non-existant) command into system() or one that exists. The last line, which system(), always returns an empty string.

I've noticed from phpinfo() that the server is currently running in safe mode - could this be anything to do with it? Is there something else that I should be looking for?

Cheers, Mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You should have a look through this:
http://php.net/manual/en/features.safe- ... ctions.php

Safe mode does restrict or disable certain functions.

Mac
Post Reply