我想配置个定时任务,就是到某个时间执行代码里的一个文件,或是每隔多长时间执行,开始有了这样想法,但是不知道怎么执行,后来上网查了很多资料,下面是我配置成功的,我整理了一份笔记供供大家参考。
相关推荐:
1.《MAC环境安装php、apache、MacPorts等环境配置》
2.《MAC环境下安装node、vue等环境配置》
3.《MAC环境下Laravel的安装及配置》
开启crontab服务
(1)、用下面命令检查需要的文件是否存在,页面显示 ls: /etc/crontab: No such file or directory 表示crontab文件不存在。
$ ls -al /etc/crontab
(2)、创建crontab文件
$ sudo touch /etc/crontab
(3)、查看 crontab 是否启动
$ sudo launchctl list | grep cron -0com.vix.cron #否 320650com.vix.cron #是
(4)、确保本地现有一个被执行的文件,我在本地新建一个test.txt文件。路径: /Users/gaopeng/Downloads/test.txt
$ cd Downloads #放到Downloads目录下 $ touch test.txt
查看一下这个文件的权限,如果不可读,不可写,还要开一下权限
$ sudo chmod 777 test.txt
(5)、编辑自动执行脚本内容,此内容是临时内容,关机后不保存
$ sudo crontab -e
(6)、每1分钟输出 1234 到脚本(注意使用绝对路径)
*/1 * * * * echo 1234 >> /Users/gaopeng/Downloads/test.txt
(7)、使用crontab执行脚本,如果没有执行,查看脚本是否有执行权限(可忽略)
$ sudo chmod 777 crontab
(8)、查看一下启动项的配置
$ locate com.vix.cron
页面提示/var/db/locate.database) does not exist 表示不存在,需要创建一下
$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
(9)查看配置
$ cat /System/Library/LaunchDaemons/com.vix.cron.plist
(10)、crontab服务的重启关闭,开启
$ sudo /usr/sbin/cron start cron: cron already running, pid: 32065 #页面显示 $ sudo /usr/sbin/cron restart cron: cron already running, pid: 32065 #页面显示 $ sudo /usr/sbin/cron stop
以下示例是在具体项目里配置的定时任务
一、php形式配置
(1)、编辑要执行的内容
*/5 * * * * /opt/local/bin/php56 /Users/mac/git/ssos_web/batch/batch_os_sll.php 2>&1
(2)、被调用batch_os_sll.php的文件的配置
<?php require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'application' .DIRECTORY_SEPARATOR.'app' .DIRECTORY_SEPARATOR.'batch' .DIRECTORY_SEPARATOR.'Controller.php'; Batch_Controller::main_CLI('Batch_Controller', 'batch_lili_yinbiao');
(3)、batch_lili_yinbiao.php业务逻辑文件「这里不做说明」
二、sh 形式的配置:
(1)、编辑要执行的内容
*/2 * * * * bash /Users/mac/git/ssos_web/batch/bin/batch_sh_os_all.sh
(2)、被调用batch_sh_os_all.sh的文件配置
SCRIPT_DIR=`dirname $0` #取当前脚本所在路径的父目录 cd $SCRIPT_DIR#cd到返回的父目录 /usr/bin/php56 ../lcriptt/batch_os_sll.php $1 2>&1
(3)、被调用batch_os_sll.php的文件配置
require_once dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'application' .DIRECTORY_SEPARATOR.'app' .DIRECTORY_SEPARATOR.'batch' .DIRECTORY_SEPARATOR.'Controller.php'; Batch_Controller::main_CLI('Batch_Controller', 'batch_lili_yinbiao');
(4)、batch_lili_yinbiao.php业务逻辑文件「这里不做说明」
以上是我配置的定时任务,可以根据自己的需要执行项目里的文件。如果大家想配置按照上面步骤一步步操作就好,如果哪有不懂可以留言。谢谢大家支持。希望能为小白提供帮助。
The above is the detailed content of MAC environment configuration scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

The charm of macOS lies in its simplicity and strength, and its core advantages include stability, security and ecosystem. 1. The simple design is reflected in details such as the Dock and Notification Center. 2. Powerful features such as Spotlight Search and Continuity. 3. Unix-based stability and security. 4. Apple's software ecosystem is seamlessly connected and suitable for developers. 5. Multitasking and shortcut key systems improve work efficiency. 6. Performance optimization suggestions include cleaning caches, using virtual desktops and TimeMachine backups.

The main difference between the underlying technologies of macOS and Linux is kernel design and file system. 1.macOS uses Mach microkernel and APFS file system to provide stability and efficient storage. 2. Linux adopts a modular kernel design, supports a variety of file systems such as ext4, XFS and Btrfs, to meet various needs.

The latest macOS brings new control centers, performance optimization and enhanced security and privacy controls. 1. The control center integrates commonly used settings to improve operational efficiency. 2. System performance has been improved in startup speed and application response, but attention should be paid to old hardware compatibility. 3. Security and privacy controls are more meticulous, and users can accurately manage application permissions.

macOS is an operating system designed by Apple for Mac computers, combining the power of UNIX and the ease of use of Macs. Its main function is to provide an efficient, secure and user-friendly computing environment.

macOSSonoma14wasreleasedonOctober26,2023.Itintroducesdesktopwidgets,enhancesvideoconferencingwithPresenterOverlayandReactions,andaddsaGameModeforbettergamingperformance.Italsoimprovessystemresponsivenessandsecurity.

macOSSonoma (14.0) brings new features such as desktop widgets, video conferencing enhancements and performance improvements. 1. The desktop widget allows users to quickly view information. 2. Video conferencing enhancement provides virtual background function. 3. System performance is improved, and application startup and response are faster.

No,macOSisnotbasedonLinux;itisbuiltonDarwin,aUnix-basedsystemdevelopedbyApple.1)macOSusesDarwin,whichincorporatesBSDelements,whileLinuxusestheLinuxkernel.2)macOSintegrateswithApple'shardwareforoptimizedperformance,unlikeLinux.3)macOSusesHomebrewforpa

Automator and AppleScript are automation tools that come with macOS, which can simplify daily tasks through graphical programming and scripting languages. 1. Automator creates workflows through drag and drop actions, suitable for codeless users. 2. AppleScript provides higher flexibility and customization capabilities, suitable for complex tasks.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor
