I tried the following command line code to run a php-script in the background.
php server.php &This works. The problem is: if I close the terminal window on my computer, the php-script also stops. I want it to run "infinite". It's a websocket server so it has to be running constantly.
How can I achieve this?
Thanks in advance!
Edit: I found the answer already (asked this question way to early I guess).
nohup php server.php & 2 Answers
You can use screen to open a virtual terminal, where the script can run.
I recommend to use screen -S php_script. Then you can run your script (even without &). With CTRL + A, D you can disconnect from the termial. You can reconnect with screen -r back to the terminal.
While Boba Fit's answer will help you, I think there is a better way of running your server in the background. In case of a reboot, you likely want to have your server running again.
I used Supervisor in a similar use case. I my case it was a long running PHP process to schedule background tasks by using JMSJobQueueBundle.
The only thing you have to setup is supervisor and configure your process:
[program:your_websocket_server]
command=php server.php &
process_name=%(program_name)s
numprocs=1
directory=/tmp
autostart=true
autorestart=true
startsecs=5
startretries=10
user=www-data
redirect_stderr=false
stdout_logfile=%capistrano.shared_dir%/jms_job_queue_runner.out.log
stdout_capture_maxbytes=1MB
stderr_logfile=%capistrano.shared_dir%/jms_job_queue_runner.error.log
stderr_capture_maxbytes=1MB