0

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

6
  • Could you redirect stderr ? Append 2>&1 to the end of your call or alternatively redirect to an error file 2>/location/script.err.
    – thecarpy
    Commented Apr 13 at 15:05
  • doing a shell_exec to make a nohup 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. Commented Apr 13 at 15:07
  • and it's very usual that webservers are configured (or rather, their execution sandboxes are) to not allow for long-running processes); you might need to figure whether on your specific platform, that's the case (we can't know what you're running this all on, and what protection mechanisms are in place. Commented Apr 13 at 15:08
  • Running on Debian 12 this error occurrs. If I run sudo systemctl restart apache2 (for example) I have to type a password, that conflicts with an Ajax call execution.
    – cli
    Commented Apr 13 at 16:05
  • The problem is, the script behaves differently when run from PHP/Ajax/nohup. When typing the command into SSH everything is ok. No errors in 2>out.err.
    – cli
    Commented Apr 13 at 16:14

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.