How to close PHP after running the program by using $ cmd

Source: Internet
Author: User
How can I close a PHP program when I borrow $ cmd to run it? I want to use PHP to run an exe program (do not use CRON or other scheduler programs, and there is other content in PHP. This is convenient for testing. take notepad as an example). after it is opened for 3 seconds, close it. What should I do? Can I disable it by calling the task manager? Find the solution code. Thank you. PHPcode & lt ;? Php $ cm PHP after running the program using $ cmd, how can I close it?
I want to use PHP to run an exe program (do not use CRON or other scheduler programs, and there is other content in PHP. This is convenient for testing. take notepad as an example). after it is opened for 3 seconds, close it.
What should I do?
Can I disable it by calling the task manager? Find the solution code. Thank you.

PHP code
  
  


------ Solution --------------------
PHP code
  
------ Solution --------------------
Popen-open process file pointer

Description
Resource popen (string $ command, string $ mode)
Open an Pipeline pointing to a process, which is generated by running the command derived from a process.

Returns the same file pointer as fopen (), except that it is unidirectional (can only be used for read or write) and must be disabled using pclose. This pointer can be used for fgets (), fgetss (), and fwrite ().

If an error occurs, FALSE is returned.

Note:

If bidirectional support is required, use proc_open ().



Example #1 popen () Example

$ Handle = popen ("/bin/ls", "r ");
?>

Note:

If the command to be executed is not found, a valid resource is returned. This looks strange, but it makes sense. It allows access to any error message returned by the shell:

Error_reporting (E_ALL );

/* Add redirection to get stderr output with a standard error. */
$ Handle = popen ('/path/to/spooge 2> & 1', 'r ');
Echo "'$ handle';". gettype ($ handle). "\ n ";
$ Read = fread ($ handle, 2096 );
Echo $ read;
Pclose ($ handle );
?>



------ Solution --------------------
Discussion
PHP code

$ Handle = popen ('C: \ WINDOWS \ system32 \ notepad.exe ', "r ");
Pclose ($ handle );
?>

------ Solution --------------------
Popenet returns the process pointer of notepad.exe in the php environment. this pointer can only be used to read and output data to notepad. only the pointer of pclose is closed, not the notepad itself.

I do not know how to disable notepad, but I want to find out how to obtain the process in the task manager from the basics of windows programming, and then use the system function to call the program to close windows.
------ Solution --------------------
Popen is unidirectional. use proc_open.
Try it
PHP code
/*** Windows only */$ descriptorspec = array (0 => array ("pipe", "r"), 1 => array ("pipe ", "w"); $ cwd = 'C: \ WINDOWS \ system32 '; $ process = proc_open('notepad.exe', $ descriptorspec, $ pipes, $ cwd ); $ s = proc_get_status ($ process); // all the information obtained is the status of the parent node. exe, rather than the child node. notepad.exe. therefore, you cannot kill the process idsleep (3) directly; exec ('taskkill/pid '. $ s ['pid ']. '/t'); // tree deletion: deletes all parent processes and their child processes. I thought that the child process id must be greater than the parent process. I wrote a piece of code and later found that it was not, and found the command proc_close ($ process );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.