0

I'm running a php socket. I run the program through nohup. Run this program properly through root. But my problem is running the program via the exec () function in php. When I run the command this way the program runs correctly but the program output is not printed in nohup.out.

my command in ssh: nohup php my_path/example.php & #is working

my command in user php: exec('nohup php my_path/example.php >/dev/null 2>&1 &', $output); #not update nohup.out

please guide me...

Amir
  • 3
  • 4
  • 2
    You just redirected the output to `/dev/null`, why would you expect `nohup.out`? See [How do I use the nohup command without getting nohup.out?](https://stackoverflow.com/questions/10408816/how-do-i-use-the-nohup-command-without-getting-nohup-out) (though the question is the reverse of this one, the answer is pertinent). – Amadan Apr 05 '22 at 07:43
  • Yes, I ignored the output. If I do not do this, my server will hang. Is there a way to run it correctly and print the output at nohup.out? – Amir Apr 05 '22 at 07:50

1 Answers1

0

From PHP docs on exec:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

From man nohup:

If standard input is a terminal, redirect it from /dev/null. If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use 'nohup COMMAND > FILE'.

To satisfy both - redirect manually to nohup.out:

exec('nohup php my_path/example.php >>nohup.out 2>&1 &', $output);
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Excellent, it works properly. If we want the output to stay in the default path as before (/home/admin/nohup.out) how should we route? – Amir Apr 05 '22 at 08:24
  • Just redirect to that, then? `exec('nohup php my_path/example.php >>/home/admin/nohup.out 2>&1 &', $output);` – Amadan Apr 05 '22 at 08:26
  • Unfortunately I did, but it does not work – Amir Apr 05 '22 at 08:28
  • In fact, routing is done from my_path. How do I redirect to the admin user? – Amir Apr 05 '22 at 08:35
  • Are you `admin`? And which user is your Web server executing as? It is likely to be a permission issue, as your Web server and your command line execute as different users. (Also, I am not sure I understand your usage of the word "routing" - it has several meanings in computing, and none of them seem to fit) – Amadan Apr 05 '22 at 08:35
  • I run the command through a user other than the admin. Is there a way to save the output to the default file? – Amir Apr 05 '22 at 09:02
  • If you just ran the command with a different user, it will not work because of permissions. If you use the same code in PHP that will be executed by a Web server like Apache, and it is executing as `admin`, then the code should work. If you want to be able to write both as `admin` and not, then as `admin` create the file if it does not yet exist (`touch /home/admin/nohup.out`) and then change its permissions so that both users can write (easiest being `chmod 666 /home/admin/nohup.out`, but not as secure - ideally you'd make a group that includes both users, and use `660` instead). – Amadan Apr 05 '22 at 09:08