Vishwa
Vishwa

Reputation: 1545

PHP script running on terminal but not in background

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?

Upvotes: 1

Views: 208

Answers (2)

user19678509
user19678509

Reputation:

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.

Upvotes: 0

nirajkumar
nirajkumar

Reputation: 330

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 &

Upvotes: 1

Related Questions