I have a PHP Script with the command
shell_exec('nohup /usr/bin/python3 -u /location/script.py > /location/script.log &');
This PHP Script is called via ajax, the script runs, logfile is created, output is written to logfile, but the python script terminates after 5-6 seconds. The same command runs flawlessly in a SSH terminal.
The PHP is an Ajax-called script, there is no issue with the Ajax call. I can tell because the logfile is created and the first few output lines are correct, but then the python script terminates.
Whatever I did, it did not change, I tried prefixing with sudo -u username
and sudo -u www-data
, appending & disown &
, changed the user that runs apache2, but all attempts didn't change the result. The python3 code is working correctly, because i can manually start the command from SSH and it runs flawlessly. There is no stacktrace in the logfile, it just stops. It vanishes from ps aux | grep script
I need the Ajax method of calling the script, what can I do?
Running on Debian 12
2>&1
to the end of your call or alternatively redirect to an error file2>/location/script.err
.shell_exec
to make anohup
exec? This is really very indicative of you having gotten something wrong. If you want to be able to run another process daemonized, you should probably actually implement that instead of what you're doing here.sudo systemctl restart apache2
(for example) I have to type a password, that conflicts with an Ajax call execution.