If you are on a Mac you can use crontab to automatically run scripts. Crontab is a scheduling tool that will run scripts at regular intervals.
What it should look like
* * * * * [command to execute] │ │ │ │ │ │ │ │ │ └─── day of week (0 - 6) (0-6 are Sun to Sat; 7 is Sun, the same as 0) │ │ │ └──────── month (1 - 12) │ │ └───────────── day of month (1 - 31) │ └────────────────── hour (0 - 23) └─────────────────────── min (0 - 59)
Example
Here's an example script on how to make regular backups of MySQL databases.
In this example, a script has been already created called backup.sh, and will dump the MySQL database to a zip file. To do everything below, you need to be able to open Terminal.
The script can then be set to run as a cronjob by editing the job list (with the nano editor):
env EDITOR=nano crontab -e
Now enter the following and press CTRL+O and CTRL+X to save and exit.
0 12 * * * cd ~/my/backup/folder && ./backup.sh
This will execute the command every day at 12:00, which changes to the folder of the script and runs it. The double ampersand &&
allows you to specify multiple commands that will run after each other.
Notice that if your computer is shut down or sleeping at the time, the script will not run until the next day at the specified time.
To see a list of active crontab jobs, use the following command:
crontab -l
You can, of course, set any script to run, or alternatively a terminal command. The above is just an example at how you would get a specific pre-created script to run. You can adjust what you need accordingly.
And if you get 'command not found'
If the above error shows when you run crontab jobs for the first time, it's because crontab executes commands without any of the normal environment variables set up.
To fix this, you can add the PATH in the beginning of any script you wish to run, so it knows how to find programs like mysqldump:
#!/bin/sh PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
Disable mail alerts
After setting up any scripts, you may notice that Terminal starts sending you e-mails.
You can disable this behaviour by inserting this line at the very top of your crontab file:
MAILTO=""
Some Examples
Description | Script |
---|---|
Execute on workdays 1AM | 0 1 * * 1-5 /bin/execute/this/script.sh |
Execute every 10 minutes | */10 * * * * /bin/execute/this/script.sh |
Log output to file | */10 * * * * /bin/execute/this/script.sh >> /var/log/script_output.log 2>&1 |
Cron in English
In case you need an easy way to work out time intervals for running your cron jobs, you can try using the website cron.help
Cron as a different User
If you want to run a cron task as a different user, all you have to do is change the command when editing the crontab file. This can be particularly useful if you are running a web server, and need to run something under the web server username. In the below instance, we are using "_www" as the user as an example for an Apache installation on macOS (but you can change "_www" to the user you want:
sudo -u _www env EDITOR=nano crontab -e
You will need to use sudo, for which you need to type in an administrator password, to perform this task.
Containing information from Ole Michelson's Blog
0 Comments