1

I'm getting the weird error, trying to figure it out since a day with no clue.

I've a script at /home/myname/script.php which contains

 <?php
while(True) {
    echo "You said: ".$argv[1];
    sleep(5);
}

When I run directly on terminal like this:

/usr/bin/php /home/myname/script.php hello

I'm getting the expected output and the script doesn't stop. But if I do

/usr/bin/php /home/myname/script.php hello &

it is stopping immediately instead of running in background, like this.

[1]+  Stopped  /usr/bin/php /home/myname/script.php hello

Any ideas why?

2
  • Duplicate of stackoverflow.com/questions/6989161/… Commented Jul 6, 2015 at 10:23
  • Thanks. Using nohup solved the problem. But it is supposed to run without nohup, right? Commented Jul 6, 2015 at 11:04

2 Answers 2

1

Use nohup /usr/bin/php /home/myname/script.php hello & .

To validate you can re-direct your output to nohup /usr/bin/php /home/myname/script.php hello > log.txt &

Sign up to request clarification or add additional context in comments.

Comments

0

Use:

nohup php file.php &

or avoid sending output to nohup.out

nohup php file.php &> /dev/null

To run it in the background without nohup, put:

<?php
ignore_user_abort(1);

at the begining of the php code.

By default, the php file terminates when the user aborts it (whether its on the server or running on the terminal) and putting it in the background somehow makes it think its been aborted.

Also:

set_time_limit(0);

will keep it running incase there is a max_execution_time limit set in your php.ini settings

Note that other factors such as memory issues can cause your script to terminate, in which case you are better off using nohup.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.