我想配置个定时任务,就是到某个时间执行代码里的一个文件,或是每隔多长时间执行,开始有了这样想法,但是不知道怎么执行,后来上网查了很多资料,下面是我配置成功的,我整理了一份笔记供供大家参考。
相关推荐:
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!
macOS: Identifying the Most Recent ReleaseApr 17, 2025 am 12:02 AMUse the command line tool "sw_vers-productVersion" to identify the latest system version on macOS. 1. Open the terminal and enter the command to get the version number. 2. This command can be used in the script for version comparison and operation. 3. If you need optimization, you can use the "defaultsread" command to read the system file to obtain version information.
macOS Development: Building Native Apps with Swift & XcodeApr 16, 2025 am 12:01 AMTo develop macOS applications, you need to use Swift and Xcode. 1. Install Xcode and create a new project. 2. Use InterfaceBuilder to design the interface. 3. Write logical code in Swift file. 4. Utilize advanced features such as protocols and generic optimization code. 5. Use debugging tools to resolve common errors. 6. Optimize performance through asynchronous processing.
macOS: The User Experience and DesignApr 14, 2025 am 12:02 AMThe design philosophy of macOS is simplicity, user-centered and highly personalized. 1) The simple user interface allows users to quickly find the functions they need; 2) The user-centric design improves the interactive experience; 3) Personalized settings allow the system to be tailored to users; 4) Excellent performance and stability ensure smooth operation of the system; 5) Hidden functions such as shortcut commands and air-to-air playback improve work efficiency.
Understanding the Current macOS: A Concise GuideApr 13, 2025 am 12:02 AMmacOSSonoma is the latest operating system version released by Apple in 2023. 1. It enhances the user experience through new features such as desktop widgets. 2. Rely on the SwiftUI framework to implement these functions. 3. The basic usage includes adding widgets. 4. Advanced usage such as using Automator to create workflows. 5. Common error handling includes checking system resources. 6. Performance optimization is recommended to clean the cache regularly.
How to open macos terminalApr 12, 2025 pm 05:39 PMOpen a file in a macOS terminal: Open the terminal to navigate to the file directory: cd ~/Desktop Use open command: open test.txtOther options: Use the -a option to specify that a specific application uses the -R option to display files only in Finder
How to take screenshots of macosApr 12, 2025 pm 05:36 PMThere are four screenshot methods on macOS: shortcut keys, touch bars, preview apps, and third-party apps. After the screenshot, the image will be automatically saved to PNG format on the desktop, and you can adjust the format, delay, save position, and floating thumbnail settings through System Preferences.
How to record macos screenApr 12, 2025 pm 05:33 PMmacOS has a built-in "Screen Recording" application that can be used to record screen videos. Steps: 1. Start the application; 2. Select the recording range (the entire screen or a specific application); 3. Enable/disable the microphone; 4. Click the "Record" button; 5. Click the "Stop" button to complete. Save the recording file in .mov format in the "Movies" folder.
How to open a terminal for macosApr 12, 2025 pm 05:30 PMThe following five methods can be used to open a macOS terminal: Use Spotlight Search through application folders Use Launchpad to use shortcut keys Command Shift U through terminal menus


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),






