Let's assume that you have created a simple script called hello.sh
:
bashecho Hello world >> log.txt
The script prints 'Hello world' and appends it to log.txt
.
You can test the script by executing:
terminalsh hello.sh
At this point you should see a new file called log.txt
containing 'Hello world'.
To execute the script periodically, for example, one time per day, we can configure a new cron task using the crontab
command.
terminalcrontab -e
Type 'A' to edit the file and schedule the script execution time:
terminal* * * * * [script to execute] | | | | | | | | | |__ Weekday (0 - 6) (Sunday = 0) | | | |____ Month (1 - 12) | | |______ Day (1 - 31) | |________ Hour (0 - 23) |__________ Minute (0 - 59)
Here is an example on how execute the script one time per day at 14:00:
terminal0 14 * * * cd /Users/Erik/Desktop && sh hello.sh
Here is another example on how to execute the script one time per minute:
terminal* * * * * cd /Users/Erik/Desktop && sh hello.sh
Press 'Esc' and type :wq
to write and quit.
To check the configured task:
terminalcrontab -l
At this point, you may need to allow full access to the disk to be able to allow crontab
execution.
Go to > System Preferences > Security & Privacy > Privacy > Full Disk Access
. Click on the +
button and allow access to usr/sbin/cron
.
Note: this configuration may be risky, since now cron tasks have full access to your computer.
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.