3

I have one simple PHP file which I am running from command line like

php my-file.php

and its working fine without any issue. I want run another file when some event happen like below

<?php
echo " hello this is sample";
exit();
// I want run another file here after exit command called 
next-file.php
echo "next-file is successfully running";
?>

Let me know if someone can give idea how can I achieve this? Thanks

5
  • Why not just create a batch file with 2 calls to 2 scripts Commented Nov 17, 2018 at 12:11
  • @RiggsFolly sorry! I am learning and does not know how to create batch file and how it will work. Thanks Commented Nov 17, 2018 at 12:12
  • 2
    Hi Mira, you are a bit short on reasons for doing this. Tell us more and we can probably me more useful help Commented Nov 17, 2018 at 12:13
  • actually I have socket connection based file...so I want reload file when sometime connection error arrived in it. I am looking only one line code which can run any php file from php script. Thanks Commented Nov 17, 2018 at 12:15
  • Your last comment makes it sounds like you want to keep a single script running, rather than starting a different one. If that's the case then you might be looking for something like Supervisor Commented Nov 17, 2018 at 12:19

2 Answers 2

3

Use include_once 'next-file.php'; to import and run the code.

Then related to PHP Doc, here is the definition of exit():

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

So add this code line into the shutdown function as below:

function shutdown()
{
  include_once 'next-file.php';
  echo "next-file is successfully running", PHP_EOL;
}

register_shutdown_function('shutdown');

Be sure to record this function before the exit().

Sign up to request clarification or add additional context in comments.

Comments

0

you can execute a shell command from php using exec()

http://php.net/manual/en/function.exec.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.