search
Homephp教程php手册如何用php作Linux自动执行脚本

如何用php作Linux自动执行脚本

Jun 13, 2016 am 10:27 AM
crontablinuxphptimingaccomplishimplementyesOverviewScriptautomatic

综述
Linux中crontab是实现定时执行的指令,利用这个指令我们就可以定时执行某一个php文件,从而实现用PHP做Linux自动执行脚本。
如何用PHP作Linux自动执行脚本?
在安装PHP的时候,会产生一个可执行文件,文件名为php.将它 copy 到 /usr/local/bin 下.在终端方式执行php程序:
php -q onlinnum.php
  PHP 原本是应用在网页应用的﹐因此它会送出 HTML 的HEADER﹐但是在此我们是要将 PHP 用作 Shell Script﹐"-q" 就是表示不要送 出 HEADER 的意思.你可以试试看不加上 -q 的显示结果。此时你已经可以在终端方式下执行PHP 代码了。 linux命令:cron daemon
这是一个系统中常驻的服务,功能在于执行例行性的工作,如每天一次或每月一 次检查磁盘。cron daemon 会在每分钟检查一次排定的工作表(crontab),看看是否有要执行的指令,所有的输出会以mail寄给用户。
设置 crontab
命令:crontab -e
该命令呼叫vi编辑器来编辑执行的清单。例如
0 0 1,15 * * fsck /home
1 * * * * /home/myhome/printhello
每一行代表一项排定的工作,在指令前为排定的时间,总共有5个栏位,以空格间隔,由左到右依序如下:
--------------------------
分钟 从00到 99
点钟 从0到 24
日 从01到31
月 从01到12
星期 从01到07,代表星期一到星期日
*号表示"每" ,如每天执行,就在第三栏填*号
--------------------------
所以上例的两项工作是:
每月1号和15号检查/home 磁盘


每小时的第一分执行 /home/myhome/printhello 这个文件
查看crontab:crontab -l
删除crontab:crontab -r
我们就知道该怎么利用PHP和crontab命令作Linux自动执行脚本了。
还需要注意什么问题?
如果php中有使用数据库,如oracle等,执行脚本中还需export Oracle的环境设置。因为cron下的脚本执行的用户都是在没登陆状态下执行的,少了很多系统里设置的环境变量的值,在连接数据库的时候会出问题。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools