0
<?php
 while(true)
    {
    set_time_limit(2);
    $reas ="wake up from sleep!!!!!!!!!! \n ";
    echo $reas;
    sleep(5);
   // $readlen = 0;
    }
?>

I try to run this code. In foreground, it runs perfectly. But when run it in background and check the process, the id is not found. I've tried to use the top command to check and

  nohup php p2.php & 

to get the process id, but when I try to kill the process using

 kill 1234

I get a error message "no such process".

What is wrong in my code? How do I run it in background and make it display in top?

1

You can use screen for that:

screen watch -n 1 ls -l /etc/

Then inside screen type Ctrl-a d to detach the screen.

The process is still running, even if you logout, see:

# ps faux | grep ls
root     13879  0.0  0.0   7828   844 pts/0    S+   08:20   0:00          \_ grep ls
root     13571  0.0  0.0  23388  1436 ?        Ss   08:18   0:00 SCREEN watch -n 1 ls -l /etc/
root     13572  0.1  0.0  14864  1780 pts/1    Ss+  08:18   0:00  \_ watch -n 1 ls -l /etc/

And there is also the pid. Or via:

#pidof watch
13572

To reattch the screen type:

screen -r

And you are back inside, to Ctrl-c or something else.

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.