我有 5 个 PHP 文件,需要在服务器重新启动时在后台同时执行。每个文件应至少执行 4 次。
目前,我手动将整个脚本粘贴到终端中并按 ENTER 键。
这是正确的方法,还是有更好的方法来实现这一目标?
nohup php /var/myapp/send-mail.php > /var/log/myapp-log/mail.1.log &
nohup php /var/myapp/send-mail.php > /var/log/myapp-log/mail.2.log &
nohup php /var/myapp/send-mail.php > /var/log/myapp-log/mail.3.log &
nohup php /var/myapp/send-mail.php > /var/log/myapp-log/mail.4.log &
...
...
nohup php /var/myapp/send-push.php > /var/log/myapp-log/push.1.log &
nohup php /var/myapp/send-push.php > /var/log/myapp-log/push.2.log &
nohup php /var/myapp/send-push.php > /var/log/myapp-log/push.3.log &
nohup php /var/myapp/send-push.php > /var/log/myapp-log/push.4.log &
但是,当以这种方式运行时,它开始返回几条消息,如下所示:
/push.4.log &ignoring input and redirecting stderr to stdout
nohup: ignoring input and redirecting stderr to stdout
nohup: ignoring input and redirecting stderr to stdout
nohup: ignoring input and redirecting stderr to stdout
[1] Exit 255 nohup php /var/myapp/another-script.php > /var/log/myapp-log/listener.1.log
[6] Exit 255 nohup php /var/myapp/send-mail.php > /var/log/myapp-log/mail.1.log
[7] Exit 255 nohup php /var/myapp/another-script.php > /var/log/myapp-log/listener.2.log
[12] Exit 255 nohup php /var/myapp/send-mail.php > /var/log/myapp-log/mail.2.log
[13] Exit 255 nohup php /var/myapp/another-script.php > /var/log/myapp-log/listener.3.log
[18] Exit 255 nohup php /var/myapp/send-mail.php > /var/log/myapp-log/mail.3.log
[19] Exit 255 nohup php /var/myapp/another-script.php > /var/log/myapp-log/listener.4.log
如何正确运行这些批处理脚本?
答案1
尝试将 stderr 重定向到上面的 stdout - 因此在每行末尾添加a 2>&1
例如:
nohup php /var/myapp/send-push.php > /var/log/myapp-log/push.1.log 2>&1
这应该可以解决这个问题 - 但是我建议在重新启动后自动启动这些 - 查看 systemd (或适合您操作系统的服务管理器)