Scheduled Task for PHP scripting in Plesk & Linux

29 March, 2013 by Tom Elliott

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.

Step 1

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

Access to server over SSH

Step 2

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.

Scheduled PHP Task

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:

scheduled-php-task-screen3

/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
Length: unspecified
Saving to: `/dev/null’
0K ………. ………. ………. ………. ………. 873M
50K ………. ………. ………. 1.08G=0s
2013-03-28 17:00:02 (951 MB/s) – `/dev/null’ saved [82645]



Scheduling a custom php script URL with plesk »


12 Comments

  • Dale Hay says:

    After days of searching all over the internet and even in the official Plesk forums, I couldn’t find a solution to this problem and now -thanks to you- I have finally got it sorted! Thank you so much Tom! If it weren’t for you, my laptop would’ve been in pieces in the garden. 😀

    • Tom Elliott says:

      Hey Dale, thanks for your comment and glad it helped! It took me ages to figure it out the scheduled scripting myself – various forum posts seemed only to hint at solutions. 🙂

  • Nick B-D says:

    Hi Tom, I too have had a couple of fruitless, wasted days trying to figure this out. Your post was very helpful – I’m almost there but I was wondering if you could clarify something for me? I’m trying to copy a file from one location to another. The following code results in success all the way to ‘sampleresult.txt: Permission denied’:

    /usr/bin/wget ‘http://domain.com/folderone/sampleresult.txt” “http://domain.com/foldertwo/sample.txt”

    However, your code results in success all the way:

    /usr/bin/wget -O /dev/null “http://domain.com/foldertwo/sample.txt”

    But I don’t know how to get the successfully downloaded ‘null’ data into a usable file… Any light you can shed would be much appreciated.

    Thanks again for your very handy post.

    • Tom Elliott says:

      Hi Nick, ah yeah – /dev/null/ effectively deletes any output so your first method is probably the one to try and pursue. Sounds like an obvious question but have you enabled full write access ‘777’ on the target folder?

      • Nick B-D says:

        Hi Tom – thanks so much for replying. At this point no question is too obvious! But yes, I’ve tried changing the permissions on the target folders and files – same result:

        –2013-09-08 11:43:01– http://domain.com/folder/sampleresult.txt
        Resolving domain.com… 211.246.110.111
        Connecting to domain.com|211.246.110.111|:80… connected.
        HTTP request sent, awaiting response… 200 OK
        Length: 0
        sampleresult.txt: Permission denied

        Cannot write to `sampleresult.txt’ (Permission denied).
        –2013-09-08 11:43:01– http://domain.com/samplefile/sample.txt
        Connecting to domain.com|211.246.110.111|:80… connected.
        HTTP request sent, awaiting response… 200 OK
        Length: 19
        sample.txt: Permission denied

        Cannot write to `sample.txt’ (Permission denied).

        • Tom Elliott says:

          Hi Nick,
          Hmm, I can think of a couple more things you can try:
          – Maybe the use of absolute paths is restricted and you need a relative one… I’d try starting with /httpdocs/folderone (or there might even be folders above those which phpinfo should tell you)
          – You could also try creating an empty .txt file and setting the permissions of that if you haven’t already.
          Hope that helps 🙂

  • Hakan Aktas says:

    Looks like this page is the only one which mentions the SSH thing. I’ve been looking for this for hours, literally. Thanks a lot!

  • Luca says:

    I love you. I LOVE you! Thanks.

  • Alberto says:

    thank you very much, you saved my life

  • Saleha A.Latif says:

    Hi,

    Thank you very much Mr. Tom Elliott.
    I have been searching about scheduled task but couldn’t find the correct “Command”.
    Its really a good article. You have mentioned every point very well.
    Thanks Once again.

    Regards
    Saleha A.Latif

  • Tom says:

    The SSH info under FTP settings sorted my issue out.Thought I was beginning to go crazy 🙂

    Thanks very much