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:


ezstream -c $1 &
echo $ezpid
sleep 2

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();

    if (ez.WaitForExit(2000))
        string message = ez.StandardOutput.ReadToEnd();
        return message;

    return ez.Id.ToString();

This should do the work:


ezstream -c $1 >log.txt 2>error.txt & 
echo $ezpid
sleep 2
if ps | grep $ezpid ; then
    echo ezsteam is still running!
    cat log.txt
    exit 0
    echo ezstream is dead
    cat error.txt
    exit 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.