Notes: Cron Jobs (Scheduled Tasks) in Mac OS X

These are my notes on how to create cron jobs in Mac OS X. Linux users are already familiar with cron. Cron jobs in Windows are called Scheduled Tasks.

First, I set the default editor to be nano. I like the simplicity of nano compared to the default vim editor. It is more humane!!

Edit the .bash_profile script

$ nano ~/.bash_profile

Add the following line

export EDITOR=”nano”

Save, exit and reload the bash script

$ source ~/.bash_profile

Open the cron table

$ crontab -e

Add the following lines (for example)

MAILTO=””
*/5 9-15 * * *  <command>

Save and exit

That will run <command> every 5 minutes, from 9am to 3:55pm, daily. Of course you should change <command> with a real Mac OS X command that you need to run.

Watch out for mails in:

/var/mail/darwin

If you wish to receive emails every time the cron job executes, change the MAILTO line to:

MAILTO=”your-email-address”

It worked!

Notes: Connect PHP5 to MySql in Mac OS X

I came across this error in my Apache error log file found in /var/log/apache2/error_log.

PHP Warning:  mysql_connect(): [2002] No such file or directory
(trying to connect via unix:///var/mysql/mysql.sock)

I figured out that this is due to PHP not being able to find the MySql socket to connect to the MySql database server. To be exact, PHP is looking for a file named mysql.sock.

I turned to look for mysql.sock in the MySql configuration file found in /usr/local/bin/mysql_config.

pkgincludedir='/usr/local/Cellar/mysql/5.6.13/include'
if [ -f "$basedir/include/mysql/mysql.h" ]; then
    pkgincludedir="$basedir/include/mysql"
elif [ -f "$basedir/include/mysql.h" ]; then
    pkgincludedir="$basedir/include"
fi

version='5.6.13'
socket='/tmp/mysql.sock'
ldflags=''

if [ 0 -eq 0 ]; then
    port=0
else
    port=3306
fi

# Create options

PHP was looking for the file /var/mysql/mysql.sock but the file exists in /tmp/mysql.sock.

So what I did was to simply create a symlink for the mysql.sock file in the /var/mysql/ folder.

$ sudo mkdir /var/mysql
$ ln -s /tmp/mysql.sock /var/mysql/mysql.sock

I could have modified the line in the mysql_config file to point to /var/mysql/mysql.sock but this is not good for other applications in Mac OS X that need the mysql.sock file in /tmp/mysql.sock.

Testing:

To test if indeed PHP can connect to MySql, I created a file named ~/Sites/test-php-mysql.php with the following contents.

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'password';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error: Could not connect to MySql.');

    if ($conn) {
        echo 'Successfully connected to MySql.';
    }

    $dbname = 'mysql';
    mysql_select_db($dbname);
?>

Then I launched Safari and typed http://localhost/~darwin/test-php-mysql.php in the address bar. It showed that the connection was established.

Successfully connected to MySql.

It worked!

Notes: Enable Apache2, PHP5 and Virtual Hosts in Mac OS X

Mac OS X came with Apache2 web server and PHP5 out of the box. All I had to do was enable them.

Enable PHP5:

I edited the Apache configuration file httpd.conf, but first, I made a backup.

$ cd /etc/apache2 
$ sudo cp httpd.conf httpd.conf.bak 
$ sudo nano httpd.conf

I removed the comment symbol “#” from the following line in httpd.conf.

LoadModule php5_module libexec/apache2/libphp5.so

Configure PHP5:

The configuration file for PHP is found in /private/etc/php.ini. I left it untouched and just made a backup.

$ cd /private/etc 
$ sudo cp php.ini php.ini.bak

Enable Virtual Hosts:

I proceeded to remove the comment symbol “#” from the following line in httpd.conf.

Include /private/etc/apache2/extra/httpd-vhosts.conf

Start the Apache Server:

I entered this command from the terminal.

$ sudo apachectl restart

Create a PHP script for testing:

The ~/Sites/ folder is available by default for user specific web sites. I created the following PHP script and saved it as ~/Sites/test.php.

<?php phpinfo(); ?>

Testing:

I launched the Safari web browser and entered the following in the address bar.

http://localhost/~darwin/test.php

It worked!