linux之定时任务

定时任务

所谓定时任务,就是指在某个规定的时间点执行规定的任务,可以分为两类:

  • 定时运行一次就结束了,不会执行第二次
  • 周期性的定时运行

针对第一种场景,linux提供了at命令来应对,而针对第二种场景,linux系统则是由cron这个系统服务来控制的,linux系统本身就有很多的计划性工作,所以,这个服务是系统系统,会默认启动的,另外,用户可以设置计划任务,所以linux系统也提供了操作计划任务的命令--crontab命令

延迟型定时任务--at命令

at命令说明:

at格式:

at #执行的时间#

at->#执行的任务脚本#

at->#执行的任务脚本#

at->

在命令行输入at,然后指定执行的时间,在输入enter键换行,接下来输入要执行的任务,多任务继续输入enter键换行隔开。输入的任务完毕,此时按下ctrl+d,则会出现“at-> ”,此时输入完毕。屏幕底下会出现类似“job 3 at 2018-01-14 12:17”的字样,证明任务指定成功。

【备注】at命令是有atd服务提供的,atd这个服务默认是没有安装的,需要手动安装,如果执行at 执行时间,有如何提示信息:-bash: at: command not found,说明atd服务没有安装,则就需要先安装

yum install -y at

systemctl start atd

 

相对时间

 

绝对时间

 

 

 

周期型定时任务

 

 

 

 

 

 

crontab命令使用

 

 

 

php操作定时任务

一模一样的任务,即时间,任务内容完全相同,那么返回的任务id是一样的,不过这个基本不会出现,同一个任务可以多次添加,但任务id是一样的

引入了2个文件,

CrontabManager.php
CronEntry.php

定时任务列表

//定时任务列表
public function index(){
	$job_list = $this->crontab->listJobs();
	$crontab = explode('\n\n',$job_list);
	return $job_list;
}

新增定时任务

//添加定时任务
public function add(){
	//任务的唯一标识
	$task_id = 'xxx';
	//验证是否存在
	$is_exists = $this->crontab->jobExists($task_id);
	
	if(empty($is_exists)){
		$time = '* * * * *';
		$task_content = 'curl -fsSL http://job.jiehun8.top/index/index/x1';
		$task = $this->crontab->newJob();
		$task->on($time)->addComments(array($task_id))->doJob($task_content);
		$this->crontab->add($task);
		
		//此操作什么也不会返回,
		$result = $this->crontab->save();
		
		$result = ['code'=>200,'msg'=>'创建任务成功'];
	}else{
		$result = ['code'=>100,'msg'=>'任务已存在'];
	}
	return json($result);
}

删除定时任务

public function del(){
	$task_id = $this->request->param('id','');
	//验证是否存在
	$is_exists = $this->crontab->jobExists($task_id);
	if(!empty($is_exists)){
		 $task_id = str_replace('#','',$task_id);
		 $task_id = str_replace('\n','',$task_id);
		 $del_nums = $this->crontab->deleteJob($task_id);
		 if($del_nums){
			$this->crontab->save(false);
			$result = ['code'=>200,'msg'=>'删除任务成功'];
		 }else{
			$result = ['code'=>100,'msg'=>'删除任务失败'];
		 }
	}else{
		$result = ['code'=>100,'msg'=>'任务id不存在'];
	}
	return json($result);
}

这里修改了原来的删除方法

function deleteJob($job = null) {
	$jobsDeleted = 0;
	if (!is_null($job)) {
		$job = strtolower($job);
		$data = array();
		//$oldJobs = explode("\n", $this->listJobs()); // get the old jobs
		//$oldJobs = $this->listJobs(); // get the old jobs
		$oldJobs = explode(" # jwd2r", $this->listJobs()); // get the old jobs
		
		if (is_array($oldJobs)) {
			//放置comment 信息
			$_comment        = '';
			array_pop($oldJobs);
			foreach ($oldJobs as $oldJob) {
				if ($oldJob != '') {
					$task_info = explode("\n",trim($oldJob));
					if(!empty($task_info)){
						$task_id = trim(str_replace('#','',$task_info[0]));
						if($job == $task_id){
							 $jobsDeleted++;
						}else{
							$newJob = new CronEntry($task_info[1], $this);
							$newJob->lineComment = '';
							$newJob->comments    = array($task_info[0]);
							$data[] = $newJob;
						}
					}
				}
			}
		}
		$this->jobs = $data;
	}
	return $jobsDeleted;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值