-1

I have about 5 different PHP scripts that must be run by the server continuously even with the terminal closed.

How do I run the 5 scripts once via nohup, each with a log of what it generated, and be able to close the terminal?

I tried to run the script below and an error occurs.

sudo nohup php send-mail.php > mail.txt
-bash: mail.txt: Permission denied

nohup php send-mail.php > sudo mail.txt
-bash: sudo: Permission denied
Tom
  • 289
  • 3
  • 13
  • 2
    In short, you should look into running at as a systemd service. Regarding the permission denied you get... You have to read up on how redirects work. The file is opened in the context of your user, because the *shell* opens it, not sudo. This is *basic* Unix shell know-how. – vidarlo Mar 09 '23 at 14:52

1 Answers1

0

There are two parts in your question.

  1. Permission denied case: which you may write the logs in a path which your regular user doesn't have write permissions to the root directory.
  2. Running scripts when terminal is closed: To run the five PHP scripts continuously with nohup and generate log files for each script, you can use the following commands:
nohup php /path/to/script1.php > ~/script1.log 2>&1 &
nohup php /path/to/script2.php > ~/script2.log 2>&1 &
nohup php /path/to/script3.php > ~/script3.log 2>&1 &
nohup php /path/to/script4.php > ~/script4.log 2>&1 &
nohup php /path/to/script5.php > ~/script5.log 2>&1 &
Zareh Kasparian
  • 753
  • 5
  • 20