0

I am running Ubuntu 16.04 OS and LAMPP inside it. I want to schedule a cron job to run every minute, and execute a PHP script. So this is what I did:

Opened terminal and entered crontab -e

It opened the CronTab file in VIM and I pressed ESC to enter the INSERT mode and then wrote the following command:

* * * * * /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/index.php

Then I pressed :wq to Save and Exit. Got installing new crontab notification in terminal.

Now my actual script should check a directory in LAMPP local server for the presence of a .pdf or .docx file and if it does, then it should do some further work, but since to start with that, I needed to check if the cron job is actually running every minute. So what I did is that made my script do this:

If a file with the extension .pdf pr docx exists in the directory, it deletes them. That way I will know if the cron job has run.

But this does not seem to work. A .pdf file is very much there and it is not getting deleted. So how would I know if my cron job is running correctly?

The sources of my learning to do this are this, this, this and this.

<?php

$pdfFileName = 'dumps/*.pdf';

$docxFileName = 'dumps/*.docx';

if ( (count(glob($pdfFileName)) > 0)   ||   (count(glob($docxFileName)) > 0) ) {

    /***********************************************/
    /*************** if the file exists ************/
    /***********************************************/

    if (file_exists($pdfFileName)) {
        unlink($pdfFileName);
    }

    if (file_exists($docxFileName)) {
        unlink($docxFileName);
    }

} else {
    echo "File does not exist.";
}

?>
Community
  • 1
  • 1
Shy
  • 542
  • 8
  • 20

2 Answers2

1
* * * * * php /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/index.php
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Thank you. By **`php`**, do you mean the path **`/opt/lampp/bin/php`** ? – Shy Jan 09 '17 at 06:42
  • 1
    usually just php is ok, for it has been write to the path variable. – LF00 Jan 09 '17 at 06:47
  • Hey can I just echo out something in the test PHP script, just to check if a cron job is executing a PHP script correctly? Where will it be echoed? In the browser? How? What is the simplest way which can tell me if my cron job is executing my php script correctly? – Shy Jan 09 '17 at 07:13
  • 1
    it's output to the stdout. not the browser. – LF00 Jan 09 '17 at 07:15
  • I installed **`* * * * * php /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/index.php`** crobjob and the PHP script only contains **`echo 'Hello World';`** , but I waited and waited but nothing output in the terminal. Here are contents of the terminal: `jes@ubuntu:~$ crontab -e crontab: installing new crontab jes@ubuntu:~$ ` – Shy Jan 09 '17 at 07:28
  • 1
    here the stdout is the process space stdout. not the terminal. – LF00 Jan 09 '17 at 07:29
  • 1
    you may write the output to a file, or use pipe stream. – LF00 Jan 09 '17 at 07:29
1

The following crontab entry:

* * * * * /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/index.php

means that /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/index.php command is run every minute just as if you typed this string in terminal and pressed Enter. The command will fail, if at least one of the following is true:

  • the current user has no execution permissions for the file/directory;
  • the file is not a shell script, and the shebang is not specified

So you should make sure that the script is executable for the user that runs the crontab, and the file contains shebang, e.g.:

#!/usr/bin/php

Alternatively, run the script by explicitly invoking the php executable:

* * * * * php /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/index.php

If php is not available in PATH, either specify the full path to php, or add the appropriate path to the PATH environment variable. The most popular implementations allow to set PATH via crontab, e.g.:

PATH=/bin:/sbin:/usr/sbin:/usr/bin:/opt/bin:/usr/local/bin

(before the entries with the commands.)

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • Hey can I just echo out something in the _test_ PHP script, just to check if a cron job is executing a PHP script correctly? Where will it be echoed? In the browser? How? What is the simplest way which can tell me if my cron job is executing my php script correctly? – Shy Jan 09 '17 at 06:59
  • 1
    @Sie, `echo` prints text to the standard output descriptor. So the text will appear at the end point connected to the standard output. You can redirect it to a file as `php script.php > /tmp/output` – Ruslan Osmanov Jan 09 '17 at 07:26
  • OK I installed **`* * * * * php /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/index.php > /opt/lampp/htdocs/Tests/16RunAPHPScriptInCronTab/output.txt`** cron job, and a file named `output.txt` IS being created at the path specified every minute but when I open it is EMPTY. Whereas the PHP script I am trying to run now is ` `. – Shy Jan 09 '17 at 07:49
  • So shouldn't `Hello, world!` be printed to `output.txt`? – Shy Jan 09 '17 at 07:50
  • 1
    @Sie, It should, if you do not overwrite the file in another command. Try this. 1. create `/tmp/test.php` with the following code: ` /tmp/test.out` into _crontab_. 3. Wait for 60 seconds. 4. `cat /tmp/test.out`. – Ruslan Osmanov Jan 09 '17 at 07:55
  • `test.out` IS created. When I manually open it with GEdit, it appears EMPTY. When I execute `cat /tmp/test.out`, nothing happens. – Shy Jan 09 '17 at 09:02
  • 1
    @Sie, looks like `php` is not available in `PATH`. Replace `php` with the output of `which php`, e.g. `/usr/bin/php`. Also redirect standard error to some file: `/usr/bin/php /tmp/test.php >/tmp/out 2>/tmp/error` – Ruslan Osmanov Jan 09 '17 at 09:03
  • `which php` does not produce any output. Nothing happens when I execute `which php`. – Shy Jan 09 '17 at 09:28
  • 1
    @Sie, okay, find out the path to the `php` executable, and use that path – Ruslan Osmanov Jan 09 '17 at 09:31
  • Is _`php` executable_ the file which has `Type: Link to executable (application/x-executable)` in its properties? It's located at `/opt/lampp/bin/php`. – Shy Jan 10 '17 at 05:01
  • 1
    @Sie, yes, I mean the PHP CLI binary. On Ubuntu you can install `php7.0-cli`, or `php5-cli` packages from the main repository – Ruslan Osmanov Jan 10 '17 at 05:04
  • At `/opt/lampp/bin/` directory, there is a file named `php` and a file named `php-7.0.9`. When I right-click them and choose `Properties` from menu, it says `Type: Link to executable (application/x-executable)` and `Type: executable (application/x-executable)`. So which one should I use? I think the second one? – Shy Jan 10 '17 at 05:04
  • 1
    @Sie, `php` is likely a symbolic link to `php-7.0.9`, so choose the former. – Ruslan Osmanov Jan 10 '17 at 05:05