How to keep process running after apache restart.


 
Thread Tools Search this Thread
Top Forums Web Development How to keep process running after apache restart.
# 1  
Old 09-04-2010
Question How to keep process running after apache restart.

Hi, I have a PHP application that starts a couple of processes on the server...the problem is that if I restart apache those running apps will die.

How can I start them in a way that they are not killed when I restart/stop apache ?

PHP Code:
      $cmdstr "nohup ".$config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
      
$cmdstr .= " > /dev/null & echo $!";
      
$pid shell_exec($cmdstr); 
This is how I start that app.
# 2  
Old 09-04-2010
You should do this:

Code:
service httpd reload

'reload' option does not close alive connections.
# 3  
Old 09-04-2010
Thank you for your reply but that is not the solution I am looking for, still waiting for suggestions.
# 4  
Old 09-04-2010
I'm guessing that the parent process is sending a term signal to each child process. The nohup command will not be effective in this case as it only ignores the hangup signal; it may also not be effective if the invoking shell is not associated with a tty, but that's neither here nor there.

What might help would be to wrap your command in a shell subcommand that causes interrupt, term, hup and quit signals to be ignored. I've no experience with PHP, but from your example it might look something like this:

Code:
 $cmdstr = "ksh -c '(  trap \"\" 1 2 3 4 15;  "
$cmdstr .= ".$config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
      $cmdstr .= " > /dev/null  )' &;  echo $!";
      $pid = shell_exec($cmdstr);

The idea behind this is that the PHP causes kshell (probably works in bash too) to be invoked to execute the command. The command given to the shell begins with a trap command that causes signals 1,2,3,4 and 15 to be ignored (the "") before executing the intended command.

I've assumed that the .= PHP operator is a += like operator and appends the string to the current value.

For a simple example, if my assumptions about PHP are wrong, consider the following that invokes the command t24 using this technique.

Code:
ksh -c '( trap "" 1 2 3 4 15; t24 )' &

I specifically invoke ksh as some systems invoke sh with a 'shell-exec' command and I don't know if plain Bourn shell supports this or not. If ksh is not in your path you will need to use the fully qualified pathname.

Hope this helps.
# 5  
Old 09-04-2010
Thank you for the reply, I have implemented your method but when apache is restarted the process still dies,

PHP Code:
  $cmdstr  "ksh -c '(  trap \"\" 1 2 3 4 15;  ";
  
$cmdstr .= $config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
  
$cmdstr .= " > /dev/null  )' & echo $!"." ;";
  
$pid shell_exec($cmdstr); 
# 6  
Old 09-04-2010
One more slight tweek to try, then Im out of ideas:

Code:
$cmdstr  = "ksh -c '(  trap \"\" 1 2 3 4 15;  ";
  $cmdstr .= $config['hsp_path']."/".$config['sc_trans']." ".$config['hsp_path']."/autodjs/".$port."/".$srvname.".conf";
  $cmdstr .= " > /dev/null & )' & echo $!"." ;";
  $pid = shell_exec($cmdstr);

Adding an amper (&) before the closing paren should cause the process to become 'detached' from its process group and parent process (ppid should be 1) and unavailable to be signaled as a part of the process group (assuming that this is how/why the process is terminating as apache is bounced). The side effect of this is that the pid assigned to $pid will be incorrect; it will be that of the subcommand that invoked the process rather than the process itself.

Good luck.
# 7  
Old 09-04-2010
Thank you for your efforts, it's getting killed , is there no way of keeping a process alive that was started from php ? The process dies on reload/stop/etc...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Apache service not coming up after restart

When I update the server with new SSL certificates, and restart httpd, I get errors : # service httpd restart Stopping httpd: rm: cannot remove `/usr/local/apache2/bin/httpd/logs/httpd.pid': Not a directory Starting httpd: ... (0 Replies)
Discussion started by: anaigini45
0 Replies

2. Red Hat

Error Restart apache

Hi Im apache service restart Error $ service httpd reload Reloading httpd: service httpd reload Reloading httpd: service httpd start Starting httpd: (98)Address already in use: make_sock: could not bind to address :80 (98)Address already in... (1 Reply)
Discussion started by: mnnn
1 Replies

3. Red Hat

Service restart and check if running

Hello, I'l like to create a script that restart a service (/etc/init.d/httpd restart) and also check if after restart the service is actually running. Sometimes it happen that at the first try the service fails to restart. Thanks (2 Replies)
Discussion started by: bazzola
2 Replies

4. Red Hat

Process not running: /opt/java15/jdk/bin/java -classpath /opt/apache/apache-ant-1.7.0-mod/lib/ant-la

Have no idea on what the below error message is: Process not running: /opt/java15/jdk/bin/java -classpath /opt/apache/apache-ant-1.7.0-mod/lib/ant-launcher.jar org.apache.tools.ant.launch.Launcher -buildfile build.xml dist. Any help? (3 Replies)
Discussion started by: gull05
3 Replies

5. Shell Programming and Scripting

Error while running restart script -

Hi All I have written below basic restart script but it is giving me the following error - error - syntax error at line 40 : `else' is not matched . below is the script can someone assist me what i am doing wrong - #!/bin/ksh cd bin . ./set_sysm sleep 60 ./swstop -f 0 ... (1 Reply)
Discussion started by: honey26
1 Replies

6. Shell Programming and Scripting

Script to restart apache when running out of memory

Hi, I have a problem with running out of memory in my Ubuntu web server (4GB RAM) because many people try to access my server. I used to restart manually my apache server to clear out the memory & swap. Can anyone tell me how to write bash script that can automatically restart apache when... (2 Replies)
Discussion started by: nica
2 Replies

7. Shell Programming and Scripting

How to keep process running after apache restart.

I have posted this on the Web subforum but it seems that nobody knows to do this, maybe someone has a solution here. Thank you I have a PHP application that starts a couple of processes on the server...the problem is that if I restart apache those running apps will die. How can I start them... (1 Reply)
Discussion started by: valiadi
1 Replies

8. Web Development

Can't restart apache. PHP is broken.

I need to restart my apache, but apachectl configtest tells me that Syntax error on line 208 of /etc/httpd/conf/httpd.conf: Cannot load /usr/local/apache/libexec/libphp4.so into server: /usr/local/apache/libexec/libphp4.so: undefined symbol: ap_signal I've tried rebuilding php, I'm not... (2 Replies)
Discussion started by: tatebn
2 Replies

9. SuSE

Restart process

I have a process that gradually eats up memory, it's currently at 80.2% and slowing down the linux server > ps aux | grep SNMPME root 3129 0.0 80.2 3591752 2480700 ? Sl Feb13 5:04 /opt/nampe/lib/snmpme/SNMPME config/startup.xml Is there a command I can execute to restart this... (3 Replies)
Discussion started by: brendan76
3 Replies

10. Shell Programming and Scripting

need help: shell script to restart apache when no. of processes keeps growing

I need a shell script to kill apache and restart it, in case the number of processes keeps growing. The logic is like the below, but I don't know how to get the number and neither the syntax. Could somebody kindly help? if no_of_processes (ps ax ¦ grep httpd) > 200 then killall httpd... (14 Replies)
Discussion started by: _joshua_
14 Replies
Login or Register to Ask a Question