In this short article, I will set up scheduled tasks known as Cron Jobs on a Ubuntu server. This will allow me to automatically run PHP scripts on the server at various points in time without any interaction.
Any user account on the Ubuntu server can set up their own Cron Jobs which will run under their own account. If you want to make sure the scheduled task has full access to everything then it will need to run under a privileged account such as the “root” user and you can do that from a user account that has sudo permission.
In my case, I want to run server-wide jobs so I’ll use sudo to create and run them as the “root” user. Before that I had better check that I have cron installed (note that installing cron will also enable and start it running).
sudo apt-get install cron
Now to create/edit cron as root for server-wide access.
sudo crontab -e
The first time you edit cron you will be asked which text editor to associate with it. I will select 2 for nano.
Next you need to scroll to the bottom of the editor. I have left the comments in but you can delete them if you want.
I’ve entered 4 jobs in the screenshot below. A job is made up of a schedule and an action. All of these 4 jobs I want to run as php files at various times using the php settings of Apache. So the action for cron is to run PHP5 using Apache2 settings (-c parameter) and execute the PHP file (-f paramenter).
The schedule is made up of 5 time slots; minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12) and weekday (0-6 where 0 = Sunday). You use * to mean every unit or you can us a comma separated list (i.e. 5,10,15 to run at minutes 5, 10 and 15 past the hour) or an asterisk and divisor for every so often (i.e. */15 to run every 15 minutes).
In the first job called bounces.php, I have this scheduled to run at 15 and 45 minutes past each hour. I could have scheduled the minutes as */30 to run every 30 minutes but I wanted to control at which minutes past each hour it ran. The second job, checksite.php, simply runs every hour of every day at 30 minutes past the hour. The next one, filecheck.php, runs every day at 2AM and the final one, backupdb.php, runs at 9PM each day from Monday to Friday (not weekends).
Be aware that if any of these scheduled jobs fail to execute you will probably find that none of them will.
Now it is best to restart cron.
sudo service cron restart
Archiving and encrypting files
If your scheduled jobs on the server will be compressing files like mine then you will need some compression tool. You can archive (TAR) files from PHP without installing anything extra but if you need to encrypt that archive from PHP, you will need to install ZIP – you might already have this installed (like me) but it is worth checking.
sudo apt-get install zip
If your scheduled jobs send emails outside of the server then it is best to make sure that the domains that it sends from are in the sendmailâs local hosts file (etc/mail/local-host-names).
sudo nano /etc/mail/local-host-names
You will also need to make sure you have IMAP installed and enabled to be able to use “sendmail” in PHP.
sudo apt-get install php5-imap
sudo php5enmod imap
If you have had to install or enable anything here that has anything to do with PHP then you will also need to restart Apache – if unsure, restart anyway.
sudo service apache2 restart
I hope you have found that useful. It is not something I do that often so I know I will be referring back here as a useful reminder in the future. Particularly, to remember to add the sending domain to the local-host-names file on my multi-site server.
This is part of a series of short articles covering setting up and maintaining a multiple domain web hosting environment using an Ubuntu Linux server. You can find an up-to-date list of those from the Hosting – Ubuntu category.