①开机自动运行: 先写了测试脚本,在/usr下,trans.sh gedit /etc/init.d/rc.local 在最后加上脚本的地址就OK了 ②定时运行脚本: 以下部分转自:http://hi.baidu.com/michaelxdt/item/a8e4fec22a59867388ad9e62 cron,是一个Linux定时执行工具,可以在无需人工干预的情况下运行作业。 1. 关于crontab 在Ubuntu server 9.10下,cron是被默认安装并启动的。通过/etc/crontab文件,可以看到以下内容: ----------------------------------------------------------------------------------------------------------------------- # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) # ----------------------------------------------------------------------------------------------------------------------- ununtu 通过调用 run-parts 命令,定时运行四个目录下的所有脚本。 1)/etc/cron.hourly,目录下的脚本会每个小时让执行一次,在每小时的17分钟时运行; 2)/etc/cron.daily,目录下的脚本会每天让执行一次,在每天的6点25分时运行; 3)/etc/cron.weekly,目录下的脚本会每周让执行一次,在每周第七天的6点47分时运行; 4)/etc/cron.mouthly,目录下的脚本会每月让执行一次,在每月1号的6点52分时运行; 当然,以上的时间均是系统默认时间,可以根据自己的需求进行修改。 2. cron 服务的启动与停止 在Ubuntu 9.10下,cron是被默认安装并启动的。而 ubuntu 下启动,停止与重启cron,均是通过调用/etc/init.d/中的脚本进行。命令如下: 1)/sbin/service crond start // 启动服务 2)/sbin/service crond stop // 关闭服务 3)/sbin/service crond restart // 重启服务 4)/sbin/service crond reload // 重新载入配置 可以通过以下命令查看cron是否在运行(如果在运行,则会返回一个进程ID): # pgrep cron 3. crontab crontab 命令用于安装、删除或者列出用于驱动cron后台进程的表格。也就是说,用户把需要执行的命令序列放到crontab文件中以获得执行,每个用户都可以有自己的crontab文件。以下是这个命令的一些参数与说明: 1)crontab -u // 设定某个用户的cron服务 2)crontab -l // 列出某个用户cron服务的详细内容 3)crontab -r // 删除没个用户的cron服务 4)crontab -e // 编辑某个用户的cron服务 /etc/crontab文件语法如下: Minute Hour Day Month Dayofweek command 分钟 小时 天 月 天每星期 命令 每个字段代表的含义及取值范围如下: Minute :分钟(0-59),表示每个小时的第几分钟执行该任务 Hour : 小时(1-23),表示每天的第几个小时执行该任务 Day : 日期(1-31),表示每月的第几天执行该任务 Month : 月份(1-12),表示每年的第几个月执行该任务 DayOfWeek : 星期(0-6,0代表星期天),表示每周的第几天执行该任务 Command : 指定要执行的命令(如果要执行的命令太多,可以把这些命令写到一个脚本里面,然后在这里直接调用这个脚本就可以了,调用的时候记得写出命令的完整路径) 在这些字段里,除了“Command”是每次都必须指定的字段以外,其它字段皆为可选字段,可视需要决定。对于不指定的字段,要用“*”来填补其位置。同时,cron支持类似正则表达式的书写,支持如下几个特殊符号定义: “ * ” ,代表所有的取值范围内的数字; " / ", 代表"每"(“*/5”,表示每5个单位); " - ", 代表从某个数字到某个数字(“1-4”,表示1-4个单位); " , ", 分开几个离散的数字; 举例如下: 5 * * * * ls // 指定每小时的第5分钟执行一次ls命令 30 5 * * * ls // 指定每天的 5:30 执行ls命令 30 7 8 * * ls // 指定每月8号的7:30分执行ls命令 50 7 * * * root run-parts/etc/cron.daily // 每天7:50以root 身份执行/etc/cron.daily目录中的所有可执行文件 4. 新增 cron 任务 推荐使用crontab -e命令添加自定义的任务(编辑的是/var/spool/cron下对应用户的cron文件,在/var/spool/cron下的crontab文件不可以直接创建或者直接修改,crontab文件是通过crontab命令得到的)。 # crontab -e 1)直接执行命令行 每2分钟打印一个字符串“Hello World”,保存至文件/home/laigw/cron/HelloWorld.txt中,cron 格式如下: */2 * * * * echo "Hello World." >> /home/HelloWorld.txt 2)shell 文件 每3分钟调用一次 /home/laigw/cron/test.sh 文件,cron 格式如下: */3 * * * * /home/laigw/cron/test.sh 文件 /home/laigw/cron/test.sh 的内容如下: ----------------------------------------------------------------------------------------------------------------------- #!/bin/sh cd /home/laigw/cron echo "shell" >> shell.txt ----------------------------------------------------------------------------------------------------------------------- 3)php 文件 php 文件需要增加命令行 “#!/usr/local/php/bin/php”,其 “/usr/local/php/bin/php” 的意思是,需要引入PHP程序安装目录下的/bin/php 文件(该ubuntu系统中,PHP程序的安装目录是/usr/local/php),而这种引入文件的处理方式有两种。 a. 在 php 文件中引入命令行(推荐使用),cron 的格式如下: */1 * * * * /home/laigw/cron/test.php 文件 /home/laigw/cron/test.php 的内容如下: ----------------------------------------------------------------------------------------------------------------------- #!/usr/local/php/bin/php <?php $file = '/home/laigw/cron/php/first_'.date('H').'-'.date('i').'-'.date('s').'.txt'; file_put_contents($file, date('Y-m-d h:i:s')); ?> ----------------------------------------------------------------------------------------------------------------------- 注:需要把test.php文件赋予可执行权限:# chmod +x test.php b. 在写 cron 任务时引入命令行,cron 的格式如下: */1 * * * * /usr/local/php/bin/php /home/laigw/cron/test.php 文件 /home/laigw/cron/test.php 的内容如下: ----------------------------------------------------------------------------------------------------------------------- <?php $file = '/home/laigw/cron/php/second_'.date('H').'-'.date('i').'-'.date('s').'.txt'; file_put_contents($file, date('Y-m-d h:i:s')); ?> ----------------------------------------------------------------------------------------------------------------------- 注:需要把test.php文件赋予可执行权限:# chmod +x test.php 5. 其他 /var/spool/cron/ 该目录下存放所有用户的cron服务 /var/log/cron 记录cron运行的日志信息 6. 一个超级用户的 crontab #Run the ‘atrun’ program every minutes #This runs anything that’s due to run from ‘at’.See man ‘at’ or ‘atrun’. 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/lib/atrun 40 7 * * * updatedb 8,10,22,30,39,46,54,58 * * * * /bin/sync 7. 例子 30 5 * * * root /sbin/init 6 这样就将系统配置为了每天早上5点30自动重新启动。