When apache restart, php background process are killed - php

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

Related

How run PHP script file in the background forever

My issue seems to be asked before but hold on, this one is a bit different.
I have 2 php files, I run the following commands:
nohup php file_1.php >/dev/null 2>&1 &
nohup php file_2.php >/dev/null 2>&1 &
This will create basically 2 php processes.
My problem is that sometimes one of the files or even both of them are killed by the server for unknown reason and I have to re-enter the commands over again. I tried 'forever' but doesn't help.
If the server is rebooted I will have to enter those 2 commands, I thought about Cronjob but I'm not sure if it would launch it twice which would create more confusion.
My question is how to start automatically the files if one or both of them got killed? What is the best way to achieve this which would check exactly that file_1.php or that file_2.php is indeed running?
There are a couple of ways you can do this. As #Chris Haas mentioned in the comments, supervisord can do this for you, or you can run a watchdog shell script from cron at regular intervals that checks to see if your script is running, and if not, start it. Here's one I use.
#!/bin/bash
FIND_PROC=`ps -ef |grep "php queue_agent.php --env prod" | awk '{if ($8 !~ /grep/) print $2}'`
# if FIND_PROC is empty, the process has died; restart it
if [ -z "${FIND_PROC}" ]; then
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo queue_agent.php failed at `date`
cd ${DIR}
nohup nice -n 10 php queue_agent.php --env prod -v > ../../sandbox/logs/queue_agent.log &
fi
exit 0
I think u can try to figure out why these two php scripts shut down as the first step. To solve this problem, u can use this php function:
void register_shutdown_function(callback $callback[.mixed $parameter]);
which Registers a callback to be executed after script execution finishes or exit() is called. So u can log some info when php files get shut down like this:
function myLogFunction() {
//log some info here
}
register_shutdown_function(myLogFunction);
Instead of putting the standard output and error output into /dev/null, u can put them into a log file(Since maybe we can get some helpful info from the output). So instead of using:
nohup php file_1.php >/dev/null 2>&1 &
nohup php file_2.php >/dev/null 2>&1 &
try:
nohup php file_1.php 2>yourLog.log &
nohup php file_2.php 2>yourLog.log &
If u want to autorun these two php files when u boot the server, try edit /etc/rc.local(which is autorun when the os start up). Add your php cli command lines in this file.
If u can't figure out why php threads get shut down, try supervisor as #Chris Haas mensioned.

Execute PHP script after php-fpm master process is restarted

Say I have a script to populate the PHP APC cache when PHP is restarted, the cache is empty.
Is it possible to create a mechanism to autorun some kind of script when the master php-fpm process is started?
Open php-fpm
vim /etc/init.d/php-fpm
In the file, find the start function, add your commands at the end.
start () {
...
# Your commands here
}
Save and restart php-fpm
I have one article for you please once read it, It might be helpful to you Link
Open php-frm through CMD
Ubantu sudo gedit /etc/init.d/php-fpm
You put the code under following function.
function start {
/*
Your code here
*/
}

PHP kill exec() background process after php-fpm restarted

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.

PHP shell_exec doesn't return anything when using sudo. Sudoers edited properly?

I have a page thats called form a browser which at the end needs to run one command as root. I am very well aware of the security implications of running shell_exec commands from the browser, so I have locked down my sudoers file for "apache all no password" to the one command:
apache ALL = (ALL) NOPASSWD: /usr/sbin/rndc
I have made my PHP page hard-coded so no part of the command is run from user-accessible inputs.
This process just refreshes the config for Bind9 (named) by issuing
shell_exec("/usr/sbin/sudo /usr/sbin/rndc reload");
However, it seems this does not run, but when I have make /bin/bash the default shell for apache and as apache, this process runs when I try it in apache shell:
[root#localhost zones]# su - apache
-bash-4.2$ /usr/bin/sudo /usr/sbin/rndc reload
server reload successful
My whole PHP code:
<?php
error_reporting(E_ALL);
$result = shell_exec("/usr/bin/sudo /usr/sbin/rndc reload");
print_r($result);
?>
I get no responses. Any ideas? SELinux is now set to permissive.
turned out to be the require_ttl parameter in my sudo files. Apache was erring in /var/log/httpd/error_log.
Thats to those who viewed :)

How to shut down ubuntu using exec (php)

I would like to shut down ubuntu in php script.
I tried this exec("shuttown"); and exec("sudo shuttown");, but it didn't work.
The command is shutdown, and it requires a time given:
exec("shutdown now");
Also note that only root can run shutdown.
exec("shutdown -h now");
It's a scary thought though that you would want a PHP script to have root privileges though
You are almost right; you have just misspelled shutdown, and have not given a time to shutdown.
As Tim Nordenfur said, simply calling shutdown now will execute that command.
For future reference, if you want to shutdown and restart it is exec("sudo shutdown -r now");.

Categories