1

The script works!

First I type this in my CLI: php worker.php >>/home/xxx/log 2>&1 Then, I want it to run as its own process or whatever, so I add an & at the end, like this: php worker.php >>/home/xxx/log 2>&1 &

But now the script is never executed, because nothing is appended to /home/xxx/log and when I check ps -fux the script is still there.

One thing I don't understand is that when I run that last command I get this:

xxx@xxx:/path$ php worker.php >>/home/xxx/log 2>&1 &
[1] 10659

Later on when I try to logout of the shell it tells me this:

xxx@xxx:/path$ logout
There are stopped jobs.

[1]+  Stopped                 php worker.php >> /home/xxx/log 2>&1

What is going on here? All I want is to run a script in the background and append all output to a file.

Thanks!

EDIT:

Here's an example.

user@box:~/nohuptest$ vim worker.php
user@box:~/nohuptest$ touch log
user@box:~/nohuptest$ php worker.php >> log 2>&1 &
[1] 31523
user@box:~/nohuptest$ cat log

[1]+  Stopped                 php worker.php >> log 2>&1
user@box:~/nohuptest$ php worker.php >> log 2>&1 &
[2] 31626
user@box:~/nohuptest$ cat log

[2]+  Stopped                 php worker.php >> log 2>&1
user@box:~/nohuptest$
user@box:~/nohuptest$ cat worker.php
<?php
echo "hello world\n";
?>
user@box:~/nohuptest$
2
  • What you are doing should work perfectly. You probably have an error in your script. Could you post a minimal example that reproduces this behavior?
    – terdon
    Aug 20, 2013 at 14:55
  • The script is simply "echo 'hello world'" for now (the real code is commented out). It works the first time, but not when running in the background.
    – Johannes
    Aug 20, 2013 at 14:57

3 Answers 3

1

I think there must be a typo or something somewhere. What you are describing should work perfectly:

$ cat worker.php
<?
echo "hello world\n"
?>
$ php worker.php >> log 2>&1 &
[1] 16353
$ cat log
hello world
$ php worker.php >> log 2>&1 &
$ cat log
hello world
hello world

Could you try with this example and let us know if it works?


The [1] 16353 is just bash telling you that it has launched the job in the background with a process ID of 16353.

3
  • Thanks. I edited the question as you can see, added an example. A friend told me to try: echo "hei" >> test.txt 2>&1 &. That worked tho... Something wrong with PHP?
    – Johannes
    Aug 20, 2013 at 15:36
  • @Johannes This is very strange, it works fine on my system. What version of bash and php are you using? Which Linux? Why are you mentioning nohup? Are you using it anywhere?
    – terdon
    Aug 20, 2013 at 16:26
  • Not using nohup, just mixed that with the use of & sign, just a lack of knownledge. Bash: 4.1.2. Php: 5.3.3. Linux: Scientific Linux. uname -a gives me: 2.6.32-358.el6.x86_64
    – Johannes
    Aug 20, 2013 at 20:55
1

My guess is that your program opens the tty. It may not be you who do that but PHP itself.

Try:

php worker.php </dev/null >>/home/xxx/log 2>&1 &

If that does not work you can always run it in tmux/screen:

tmux [enter]
php worker.php </dev/null >>/home/xxx/log 2>&1
[CTRL-b][d]

You can probably also run it using script:

echo "php worker.php >>/home/xxx/log 2>&1" | script &
0

I found the solution.

php script.php [args] </dev/null >>/path/to/log 2>&1 &

PHP was waiting for an input... I have no idea if I can fix this in the php.ini file, but for now that works...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .