banner
半米牙

半米牙的笔记

分享技术、记录生活
email

Running scheduled tasks using Crontab on macOS.

By creating a Crontab, you can run scheduled tasks on macOS! Let's do an example.

Assuming you want to run a script called work.sh that can do some work for you - it doesn't matter what the script actually does. But you spend a few minutes every day running this script, which is a waste of time and can be easily handled by a scheduled task.

Ensure the script works properly#

First, make sure work.sh can run properly and place it in a fixed location, such as ~/.script directory.

Configure the scheduled task#

Then, open the Terminal and run the command:

crontab -e

This will open a text file with vim. If you haven't configured any scheduled tasks before, the file should be empty with only one line number "1".

Enter the editing mode of vim by pressing i, and input the cron expression and the command to be executed.

* * * * * command

For example:

0,15,30,45 * * * * cd ~/.scripts && ./work.sh

Save and test#

Finally, press esc to exit the command mode of vim, and enter the command wq! to save and exit. Then wait for the scheduled task to execute and check the result.

Appendix: Cron expression#

* * * * *

Explanation:

* - minute (0-59)
* - hour (0-23)
* - day (1-31)
* - month (1-12)
* - day of the week (0-6, 0 is Sunday)
(from left to right)

You can generate Cron expressions using Crontab.guru

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.