I need to run a command line Linux program from php via exec
function. The main problem is that i need to wait the result of a program and it returns after a while (from 30 sec up to 2 minutes). Because PHP doesn't have multithreading all site will stuck for that time. Is there any way to run exec
like a "new thread" (not in the background)?
Asked
Active
Viewed 2,180 times
1

Kin
- 4,466
- 13
- 54
- 106
-
What do you mean by a "new thread", if you don't mean in the background? As you mentioned, PHP doesn't have multithreading, so your options are waiting for it to finish, or leaving it to run in the background. – jcsanyi Jun 20 '13 at 07:59
-
why not execute it via ajax, its as good as a thread in this case. – DevZer0 Jun 20 '13 at 08:06
-
@DevZer0 and so all site will be stuck as i said before – Kin Jun 20 '13 at 08:14
-
@jcsanyi i need to get the result from console - in background it's not possible. – Kin Jun 20 '13 at 08:14
-
how can the site be stuck, its running as in `async` mode, but if the user navigates to another page you loose the request. so you need to implement it bit differently having a div in your page do the navigation while you can keep the references to ajax request made from the document. – DevZer0 Jun 20 '13 at 08:16
-
please, explain better what is your scenario. Are you running a CLI script, are you serving a web page? – m4t1t0 Jun 20 '13 at 08:28
-
PHP does do multi-threading. Out of the box it implements fork() and the related functionality. Light Weight processes (Posix threads) are available as an extension. But this doesn't really have much to do with the question being asked here. – symcbean Jun 20 '13 at 09:47
1 Answers
1
also if you said you don't want to run it in background it's a common solution to do so and then regularly check if the process has finished.
the executed program can write it's "return-value" into a temp-file when it has finished. so if the file exists you know the process has finished and you can work with the result.
also it's good practice to not only check for the existance of a temp-file but for the process ID of the programm executed. if it does not provide it's PID you can start it with a tool like start-stop-daemon which is able to provide this information.

Andreas Linden
- 12,489
- 7
- 51
- 67