I'm trying to launch a command from php with shell_exec which calls a bash script that launches a program that stays running, but sometimes this program throws an error and exit, what I need is a way of calling the script and after 2 seconds return whatever the program outputed, if the program launched correctly I just want it to return the PID and that it stays running on Linux but PHP continues, if it throws an error and closes I want the output of that error, here's what I tried:
#!/bin/bash
ezstream -c $1 &
ezpid=$!
echo $ezpid
sleep 2
disown
If it throws an error it returns well with the error message, but if it runs normally PHP gets stuck and never returns because the program keeps running, how can I make the script return even if it kept running after 2 seconds?
How can I do it with bash?, I was able to do this in C# with the Process class like this
Process ez = new Process();
//ez.parameters
ez.Start();
if (ez.WaitForExit(2000))
{
string message = ez.StandardOutput.ReadToEnd();
return message;
}
return ez.Id.ToString();
Best Answer
This should do the work: