Heim  >  Artikel  >  Backend-Entwicklung  >  php自动运行 win32service效能

php自动运行 win32service效能

WBOY
WBOYOriginal
2016-06-13 13:06:42844Durchsuche

php自动运行 win32service功能

什么?windows服务? ??

? ?? ? ? ? ?Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序 。 这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这使服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算 机上工作的其他用户,需要长时间运行功能时使用。还可以在不同于登录用户的特定用户帐户或默认计算机帐户的安全上下文中运行服务

  • 简单的说就是可以长时间、自动运行在? Windows 上的PHP程序。 问:这对我来说重要吗?

有什么用?

? ? ? ?哈哈哈!有了这个,我们就可以做更多的事了。如:定期执行一个PHP任务、自动更新数据....等等。

?

如何实现?

? ? 前提:

  1. 必须有一台 Windows服务器 或者Windows?PC机
  2. 必须安装得有PHP运行环境
  3. 必须在PHP 的安装目录的\ext\下有这个 php_win32service.dll 文件
  4. 必须有php.ini文件里面开启? extension=php_win32service.dll ?这个选项

?

如果你运行不起来(请看一哈上面的说明哦)最重要代码来了,如下

<?
/**
 * 利用PHP安装windows自动运行的服务
 *
 * Project: Tiwer Developer Framwork
 * This is NOT a freeware, use is subject to license terms!
 * 
 * Site: http://wgw8299.cnblogs.com
 * 
 * $Id: WinService.class.php 258 2011-03-07 02:18:42Z wgw8299 $
 *
 * Copyright (C) 2007-2010 Tiwer Developer Team. All Rights Reserved.
 */
class WinService
{
	/**
	 * 服务名称
	 */
	var $name;

	/**
	 * 定义服务名称
	 */
	var $info_name;

	/**
	 * 定义php.exe存放路径
	tus = win32_query_service_status( $this->name );

		if ( $svcStatus == 1060 ) {
			echo   "服务[" . $this->name . "]未被安装,请先安装";
		} else {
			if ( $svcStatus['CurrentState'] == 1 ) {
				$s = win32_start_service($this->name);

				if ( $s != 0 ){
					echo  "服务无法被启动,请重试! ";
				} else {
					echo  "服务已启动! ";
				}

			} else {
				$s = win32_stop_service($this->name) ;
				if ( $s != 0 ) {
					echo " 服务正在执行,请重试! " ;
				} else {
					$s = win32_start_service( $this->name ) ;
					if ( $s != 0 ){
						echo   "服务无法被启动,请重试! ";
					} else {
						echo   "服务已启动! ";
					}
				}
			}
		}
	}

	/**
	 * 启动服务
	 *
	 * @access public
     *
	 * @return void	 
	 */
	public function start() {
		$s = win32_start_service(_SERVICENAME);
		if ( $s != 0 ){
			echo   " 服务正在运行中! " ;
		} else {
			echo   " 服务已启动! " ;
		}
	}

	/**
	 * 停止服务
	 *
	 * @access public
     *
	 * @return void
	 */
	public function stop() {
		$s = win32_stop_service(_SERVICENAME );
		if ( $s != 0 ){
			echo   " 服务未启动! " ;
		} else {
			echo   " 服务已停止! " ;
		}
	}
}
?>

撒旦法

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn