4

I have to run 2 commands through exec(); the first command is a wrapper calling for (Plesk panel) subsription, the second is also a plesk command,for dns.

Note: After i execute an add subscription, the apache WILL RESTART!,

So my Question is: can i call the exec somehow, to execute both commands at linux side without loss of the second command?

Ex:

exec(("/wrapper2 3 --create ... && /wrapper2 4 --update-soa example.com ... ) > /dev/null 2>&1 );

Php will send both commands to linux to execute, or it will restart apache after the first command, and then i can't execute the second command?

Thanks

Kszili
  • 334
  • 3
  • 16
  • What is wrapper2 actually? If it is a php script, it runs independently from apache. – Leif Jun 23 '11 at 14:13
  • If you put a semi colon in your exec script, it should run one script right after the other, and if you restarted apache in between, it'll wait for apache to restart before executing the second script. – Steve Nguyen Jun 23 '11 at 14:16
  • So you are saying that you're killing your own script by restarting apache while it runs? – hakre Jun 23 '11 at 14:23

4 Answers4

2

Um... I'm thinking bad deal. Generally it is a bad idea for a process to tell its parent to restart while the process needs to keep running. But, even if it were a good idea -- Apache is the parent process of PHP in that context (do ps -A, you'll not see PHP), I can't imagine that it would let you restart it and keep running at the same time.

I'd approach it this way: if you can bridge a delay, then have a cron job look for whether a specific file exists, if it does, then execute the two command that you need it to. At a worse-case scenario, make PHP output a file which has the two commands you want run and then have cron run that file.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • I like your out of the box approach using a file+cron, I thought of this approach as well, then found this answer using `at` http://stackoverflow.com/a/223745/457589 When you think of it, `at` is creating the script file and looking it up ... so no need to rebuild that wheel using cron ... `at` also has the gain to run immediately, when cron runs every minute – Julien Apr 09 '15 at 15:00
1

Well from my understanding the issue lies in the fact that Apache is going to be the parent of the script that is running, when Apache gets shut down so will the script.

Barring that you can deal with a sort of derp-y setup, you can set up a cron job that looks for when it needs to restart the server (either a file you created via touch or something from PHP), which can handle everything outside of the context of Apache's process.

A sort-of-dirty idea. :(

StrangeWill
  • 2,106
  • 1
  • 23
  • 36
  • I thought of the same thing, then found this answer using `at` http://stackoverflow.com/a/223745/457589 When you think of it, `at` is creating the script file and looking it up ... so no need to rebuild that wheel using cron ... `at` also has the gain to run immediately, when cron runs every minute – Julien Apr 09 '15 at 14:57
0

I think why the apache restart is your command executes too long or cost to much system resource and makes apache sub process exits. Try using fastcgi mode instead of mod_php.

You can make a shell file to execute two commands.

hanguofeng
  • 49
  • 2
  • Apache restarts because when i add a subscription it has to make a restart to make the changes apply-ed. It sets a various settings for the domain newly created, not becouse of too long execution. (~4secs) – Kszili Jun 23 '11 at 14:22
  • are you writing a web panel to control apache config? :)try using call another program to restart apache and execute your other command will be better. or try fastcgi,it's execute outside apache. – hanguofeng Jun 23 '11 at 14:30
0

Put the commands in a shell script and execute that script. It's less complicated and just in case you can call it with other tools as well like on apache restart or via cron.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • I don't think grouping the commands under a single file will protect the process to be terminated if there is a call to restart apache anywhere in that script or subscripts within ... – Julien Apr 09 '15 at 15:02
  • @Julien I don't think the OP would have accepted this answer if that would have been the case. – hakre Apr 09 '15 at 15:08