Setting up scheduled tasks or ‘cron’ jobs in Plesk is an incredibly useful feature. I’ve typically used scheduled tasks for MySQL database backups, automatic email notifications and twitter feed caching.
After a bit of scrabbling around various internet forums and threads, I worked out that a couple of steps were needed to get a PHP scheduled task working on a Linux dedicated/VPS server using the Plesk control panel.
Before you start however, double check that your PHP script works by running it direct from a browser.
From the main ‘Websites & Domains tab’, select FTP Access and the main system user. Make sure the drop down option ‘Access to the server over SSH’ is changed from ‘forbidden’ to /bin/bash’. Enabling access to the server over SSH allows remote shell services to be executed, such as a wget command which we’ll use next. If this option is not selected, you might get an error similar to -: /usr/bin/wget: No such file or directory
From ‘Websites & Domains’, select ‘scheduled tasks’ followed by the same system user we gave server access for in step 1. Select ‘schedule new task’ and enter the scheduled settings similar to the below, substituting site.com and script.php with the full path to your PHP script.
I’ve found it more straightforward to use wget – a utility to download files from the Internet – rather than the PHP utility as PHP sometimes involves modifying the PHP script itself.
If you want to have the scheduled task run at a specific time, enter the hour/day/month etc as a numeric value. Using ‘*’ means ‘all’ and using ‘*/’ sets an interval, so the above schedule will be set to run at 10 minute intervals every hour, every day of the week.
This will be summarised in the scheduled tasks page as below:
/usr/bin/wget is the server path to wget and -O /dev/null tells wget to discard the contents of the file rather than the default behaviour of trying to save the file.
You can check the script works by selecting ‘settings’ and entering your email. Hopefully, you’ll get an email with the wget status that looks something like the below:
–2013-03-28 17:00:01– http://www.site.com/script.php
Resolving www.site.com… 123.456.789.123
Connecting to www.site.com|123.456.789.123|:80… connected.
HTTP request sent, awaiting response… 200 OK
Saving to: `/dev/null’
0K ………. ………. ………. ………. ………. 873M
50K ………. ………. ………. 1.08G=0s
2013-03-28 17:00:02 (951 MB/s) – `/dev/null’ saved