By creating Crontab, you can run scheduled tasks on macOS! Let's do an example.
Assume you want to run a script named work.sh
that can do some work for you - what the script actually does is not important. But spending a few minutes every day to run this script is a waste of time, which can be easily handled by scheduled tasks.
Ensure the script works properly#
First, make sure work.sh
can run properly and place it in a fixed location, such as, I placed it in the ~/.script
directory.
Configure scheduled tasks#
Then, launch Terminal and run the command:
crontab -e
It will open a text file with vim
for you. If you haven't configured scheduled tasks before, the text content should be empty, with only a line number "1".
Then press i
to enter vim
's edit mode, enter the cron expression and the command to execute.
* * * * * command
For example:
0,15,30,45 * * * * cd ~/.scripts && ./work.sh
Save, test#
Finally, press esc
to exit to vim
's command mode, enter the command wq!
to save and exit. Then wait for the scheduled task to execute and check the results.
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