Schedule scripts/tasks with Cron

Last updated

Antonio Ufano avatar

Antonio Ufano

In one of my last projects I had a requirement to run a small PHP script two days a week at a certain time. In other projects I solved this requirement using ControlM, but for this project that wasn't an option so I had to search for an alternative which, to my surprise, was pretty easy to use and learn, already included in my hosting server and also included in my personal laptop. In case you didn't know already, I'm talking about Cron.

As I just mentioned Cron is included by default in OS X and most Linux distributions. As defined in Wikipedia:

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals.

In my case, I just needed to use Cron to execute a small PHP script that sends an email. The script just had a single of line:

 mail('', 'Test Cron from my Mac', 'This is a test of an email sent from my Mac using Cron');

In order to see the list of jobs already scheduled in Cron, we can run the following commant in the Terminal:

$crontab -l

It will probably return and empty list :) In order to include our script in the job list, we need three values

  • The schedule, which will be informed by 5 numbers (or the _ symbol as a wild card): minutes, hours, days of the month, month and days of the week. For example, if we want to run it every minute and every day of the week and month, the value will be: _ \* \* \* _. Another example 10 8,16 3,13,23 3 _ will execute it at minute 10, past hour 8 and 16, on day-of-month 3, 13, and 23 and in March. You can check CronTab guru to help you define the schedule.
  • The second value is the folder conaining program that will run the script. In our case it will be the PHP folder of our system. In my case it's "/usr/bin/php".
  • The last value we need is the full path to the PHP script we want to run. In my case it's "/Users/uf4no/Projects/PHP_Schedule/mailTest.php".

Now that we have all values required we just have to include them in the Crontab. Run the edit command which will open an editor:

$crontab -e

Move the cursor to the top of the file using the arrow keys and press the "i" key to insert a line. Now include the 3 values defined above separated by a space. It should look like this:

To save the file press escape (to enable command line) and then type :wq ( to Write and Quit the editor). Now if you list the Cron jobs again with your script should appear:

If everything goes right the job will run the next minute and, in my case, I'll receive an email in my Inbox.

Summary and references

As you can see, scheduling a PHP script in Mac is pretty easy using the Cron, which is already installed in the system. You just need to know how to the schedulle times defined by 5 variables, the PHP folder and the path to you script. In the example above we just ran a script that sends an email but could use something like a bot that retrieves data from a webservice and then post it in social media. Or we can use it to do housekeeping of a logs folder

Hope this little tutorial help you if you find a similar requirement. You can find more info about Cron in the following links:

Happy coding!

If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️

Oh! and don't forget to follow me on Twitter where I share tons of dev tips 🤙

Other articles that might help you

my projects

Apart from writing articles in this blog, I spent most of my time working on my personal projects. logo

theLIFEBOARD is a weekly planner that helps people achieve their goals, create new habits and avoid burnout. It encourages you to plan and review each week so you can easily identify ways to improve your productivity while keeping track of your progress.

Request early access logo

I'm very interested in blockchain, smart contracts and all the possiblilities chains like Ethereum can bring to the web. SolidityTips is a blog in which I share everything I learn about Solidity and Web3 development.

Check it out if you want to learn Solidity logo

Quicktalks is a place where indie hackers, makers, creators and entrepreneurs share their knowledge, ideas, lessons learned, failures and tactics they use to build successfull online products and businesses. It'll contain recorded short interviews with indie makers.

Message me to be part of it