To run scripts on the server in the background I usually use the nohup command. I tried to call it using exec and shell_exec without any additional parameters. When run, the script hangs until until the command is executed. Below command code:
exec('nohup rsync -avz-e "ssh-p 22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" firstname.lastname@example.org:/original/ /destination/s_24/ && echo $? >> /destination/l_24.txt 2>&1 &');
So how do I run rsync in the background with php so that the script is not waiting until the run command?Important clarifications/observations
1. The PHP script is run from browser, not from terminal
2. PHP is running via php-fpm, i.e. without Apache
I replaced the background to the simplified command to analyze the problem. The following options hang up script:
sleep 15 &
nohup sleep 15 &
setsid sleep 15 &
I assume this is normal behavior for nohup. The command run via nohup, can theoretically run without terminal, if to get out of it. But in the call because there is no exit from the terminal.
However, the options:
nohup sleep 15 && exit &
nohup sleep 15 & exit
also not working as it should.
I also tried to replace the exec to shell_exec, but this gave no result.