Beware this content is over 8 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.

I have seen quite a few posts about this topic floating around the internet during my search for a solution, many of them did not apply to my situation or failed to be clear and concise so I thought I would have a bash at documenting the necessary steps in setting up a cron job on your linux server.

Brief

A client required an automated newsletter to be filled by an external RSS feed. The newsletter would have all the latest news articles and posts from the website sent directly to a database of subscribers via e-mail each working day (Monday to Friday) and at a certain time each day.

This particular example refers particularly to phpList but can easily be translated to setting up any other cronjob/cron task you may need to start once you know the basics for setting one up.

Pre-Cron Check

The first thing you will need to do is make sure you can access your server via Secure Shell (SSH) so you can execute commands and setup your cron job.

The structure of a cron job is as follows:

1
2
3
4
5
6
7
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed

Some examples from Wikipedia:

1
2
3
4
5
0 0 1 1 * - runs a command once a year (on first day of first month at midnight)
0 0 1 * * - runs a command once a month (on first day of every month at midnight)
0 0 * * 0 - runs a command once a week (on sunday each week at midnight)
0 0 * * * - runs a command once a day (at midnight each day)
0 * * * * - runs a command hourly (on every hour, at the hour)

If using phpList (if not, you can skip to the next section). You should create a new administrator in your phpList backend. This user need only have full access to both getrss and processqueue. For security purposes it is good to create a seperate cron-bot account to protect your root/super-administator username and password. The example below uses the username, cronBot with the password, cronPass.

You also need to ensure that the getrss.php and processqueue.php scripts on your website have executable permissions (CHMOD: 744 should be adequate but if your having trouble try 755).

Setting up the Cron

1) Start by logging into your server via SSH.

2) Check to see if a crontab already exists on your server using the following command: crontab -l. Note that it is a hyphen/minus symbol followed by a lowercase ‘L’.

3) Determine your current working directory using the pwd command. For 1and1 customers it should look something like this:

1
2
(uiserver):u12345678:~ > pwd
**/kunden/homepages/10/d98765432/htdocs**

4) Enter into the crontab editor, which for 1and1 customers is the vi editor.

5) To start typing, press the ‘Insert’ key on your keyboard or alternatively ‘i’ to begin typing a new line.

6) Specifcy minutes, hours, days of the month (1-31), months of the year (1-12) and days of the week (Sunday to Saturday, where Sunday = 0 and Saturday = 6).

7) Specify the location of the application that is used to execute the command, which for phpList users will be PHP, so we need to point to the correct location of the PHP installation on the webserver. This may require some tweeking depending on your hosting package but 1and1 customers should try this:

1
/kunden/usr/local/bin/php

8) Then we need to point to add the second half of the command which is the script to be executed. Depending on your installation of phpList and your directory structure this may be different, so change accordingly. An example below assumes you installed phpList to your root directory eg. mydomain.com/lists/admin/ would be like this:

1
/kunden/homepages/10/d98765432/htdocs/lists/admin/index.php page=processqueue login=**cronBot** password=**cronPass**

You can see that this example is being used to process the sending of messages ready to be sent buy specifing a page reference, in this case, processqueue. This is followed by a login and password reference for our cronBot which we created at the begining and which only has admin access to specific processes/pages.

9) Putting it all together your new cronjob entry on your crontab may look something like:

1
2
3
4
[a] [b] [c] [d]
.__________. ._______________________. .___________________________________________________________________________. ._____________________________.
| | | | | | | |
5 13 * * 1-5 /kunden/usr/local/bin/php /kunden/homepages/10/d98765432/htdocs/lists/admin/index.php page=processqueue login=cronBot password=cronPass

This particular cronjob says, that:

[a] at 5 minutes past the 13th hour every day of every month from Monday to Friday;
[b] using the PHP binary;
[c] execute the processqueue task;
[d] with the cronBot login and password.

10) The last thing to do is to save and exit. While still in the ‘Insert’ mode, press the ‘Esc’ key on your keyboard to leave the ‘Insert’ mode. Then type the following command to to write (i.e. save) the modified crontab and quit the editor. To do that type the following: :wq and press the return/enter key.

You should now be back on the normal command line. To check that your new cron job has been created use the crontab -l command to display the crontab. If there are any spelling mistakes return to edit the crontab using the crontab -e as before. Otherwise, thats it..most of it…

Obviously for those phpList’ers attempting to automate their RSS feed retrieval and sending of e-mails, the principals are the same, the only things to consider is that you have given your cronBot account the correct permissions to the getrss function and you create a seperate cronjob with a page reference for getrss instead of processqueue as in the exaple above.

This was a relatively brief overview of the process but I hope it will save others some time in setting theirs up. Remeber that if you are managing large lists (1,000+) you may want to consider enabling the throttling and batch sending settings available in the config.php in your phpList installation to prevent having your account frozen by your ISP or website host.

Any other questions, or clarification needed. Ask below.