I use nginx and php7.1-fpm. I want to run a background process using PHP and exec().
My short code:
<?php
exec('/usr/local/bin/program > /dev/null 2>&1');
Unfortunately after the systemd restart php7.1-fpm the program is killed.
I have tried to run with a different user than the one running the pool:
<?php
exec('sudo -u another_user /usr/local/bin/program > /dev/null 2>&1');
However, this does not solve the problem - still kills.
I can not use ssh2_connect(). How can I solve this problem?
It seems this is due to the php-fpm service being managed by the systemd.
All processes launched from php-fpm belong to its control-group and when you restart the service systemd sends the SIGTERM to all the processes in the control-group even if they are daemonized, detached and/or belong to another session.
You can check your control-groups with this command:
systemd-cgls
What I've done is to change the KillMode of the php-fpm service to process.
Just edit it's .service file:
vi /etc/systemd/system/multi-user.target.wants/php7.0-fpm.service
and change or add the line to the [Service] block:
KillMode=process
Then reload the configuration by executing:
systemctl daemon-reload
That worked for me.
References:
Can't detach child process when main process is started from systemd
http://man7.org/linux/man-pages/man5/systemd.kill.5.html
What would be wonderful would be a command (similar to setsid) that allowed to launch a process and detach from control-group but I haven't been able to find it.
Related
I am currently working on a Symfony 6 project. Now I have the situation that I want to stop and start a systemd service which is used to consume the messages from the Symfony Messenger message queue.
The service is called "messenger-worker#.service" and is located under /etc/systemd/system
Now in my PHP script I run the following:
$output = shell_exec("sudo -u www-data /usr/bin/systemctl stop messenger-worker#1.service 2>&1");
dd($output)
The $output contains the following error message:
Failed to stop messenger-worker#1.service: Interactive authentication required.
See system logs and 'systemctl status messenger-worker#1.service' for details.
Under /etc/sudoers.d I already created a file called "www-data". With the following code:
%www-data ALL=(ALL) NOPASSWD: /usr/bin/systemctl stop messenger-worker#1.service
Does anyone have an idea what I am doing wrong here?
As #Rufinius noted in the comments. It does not make sence to try to execute the command as -u www-data. Furthermore I realized that I had to remove "2>&1" at the end of the command in order to make it work properly. I think it's because I didn't include the part ("2>&1") in my sudoers.d file in the command, but I don't need it now anyway. So I adjusted it as the following:
shell_exec("sudo /usr/bin/systemctl stop messenger-worker#1.service");
I have an Ubuntu VM running in VirtualBox that hosts a server using Apache. The concept of the server is to accept HTTP POST requests, store them in a MySQL database and then execute a Python script with the relevant POST data to be displayed in a Discord channel.
The process itself is working but each time the PHP script calls the Python script, a new process is created that never actually ends. After a few hours of receiving live data the server runs out of available memory due to the amount of lingering processes. The PHP script has the following exec call as the last line of code;
exec("python3 main.py $DATA");
I would like to come up with a way to actually kill the processes created from this exec command (using user www-data), either in the Python file after the script is executed or automatically with an Apache setting that I probably just do not know about.
When running the following command in a terminal I can see the different processes;
ps -o pid,user,%mem,command ax | sort -b -k3 -r
There are 3 separate processes that show up, 1 referencing the actual python3 exec command as marked up in PHP;
9903 www-data 0.4 python3 main.py DATADATADATADATADATADATA
Then another process showing the more common -k start commands;
9907 www-data 0.1 /usr/sbin/apache2 -k start
And lastly another process very similar to the PHP exec command;
9902 www-data 0.0 sh -c python3 main.py DATADATADATADATADATADATA
How can I ensure Apache cleans these processes up - OR what do I need to add into my Python or PHP code to appropriately exec a Python script without leaving behind processes?
Didn't realize the exec command in php would wait for a return output indefinitely. Added this to the end of the string I was using in my exec call; > /dev/null &
i.e.: exec("python3 main.py $DATA > /dev/null &");
I'm trying to create a bash script that starts two processes: PHP-FPM and Nginx.
First PHP-FPM should start and once that has finished starting up (port 9000 will then be reachable for example, but there might be other means of checking it has finished starting up) the Nginx server should be started.
My current script looks like this:
#!/usr/bin/env bash
set -e
php-fpm -F &
nginx &
wait -n
But sometimes early on nginx will give me a 502 gateway error because php-fpm is not ready yet.
What's the cleanest/best way of getting this startup in order?
Regards,
Kees.
You can modify your script in this way:
#!/usr/bin/env bash
set -e
php-fpm -F && nginx
wait -n
As you can see in this answer on Unix&Linux, the && operator allows you to run the second command only if the first exited successfully.
One of the solutions I found is this:
check process php5-fpm with pidfile /var/run/php5-fpm.pid
group php
start program = "/etc/init.d/php5-fpm start"
stop program = "/etc/init.d/php5-fpm stop"
if failed unixsocket /tmp/php-fpm.sock then restart
if 4 restarts within 5 cycles then timeout
depends on nginx
This does not work because the following file does not exist in my system:
/tmp/php-fpm.sock
When I run sudo find / -name *.sock I get:
/run/mysqld/mysqld.sock
/run/fail2ban/fail2ban.sock
/var/lib/php5-fpm/web2.sock
/var/lib/php5-fpm/ispconfig.sock
/var/lib/php5-fpm/apps.sock
If it helps, this is the file I change to configure how php behaves with the particular domain I use : /etc/php5/fpm/pool.d/web2.conf. So MAYBE the right file is `/var/lib/php5-fpm/web2.sock ?
This is a VPS with ubuntu 12.04 and nginx 1.4.4.
I have an issue with apache and php.
I call this script in php:
exec("nohup sudo QUIET=y sh foo.sh > /home/tmp/log.txt 2>&1 & echo $!", $res);
Shortly after, the script foo.sh need to restart apache to include new configuration files:
/etc/init.d/apachectl restart
In command line, it works fine but in my php script the process is killed at the same time apache is restarted. Why? I thought nohup detach the processus of its parent.
(I point out that i can't change the sh script)
Any help would be greatly appreciated.
You should use /etc/init.d/apachectl reload if the only thing you want is to reread configuration files.
/etc/init.d/apachectl reload
This will not stop the service, but keep it running and refreshes the processes configuration.
Use /etc/init.d/apachectl reload instead if you dont want to kill the process.
Since apache restart sends SIGTERM, not SIGHUP,
you should handle SIGTERM from foo.sh