Viewed   67 times

I'm trying to run a php-script on a scheduled basis. So I'd thought crontab was a good idea. The server I'm using is on Linux which I'm not that familiar with. So the problem I'm having is, I don't know how make the script executable from php. I need to reference the script, or put it into a folder that can run php from the command line. So I don't know what path to give my crontab, for example:

5  * * * * var/www/some/path/script.php

I found some vague information about this php executable being found in

/usr/bin/php

But I can't find any php file in there, maybe I don't have it installed? My php5 and apache installation is in:

/etc/php5

So my question becomes, is there anyway to execute a php-script with crontab in any other folder, or do I just lack the php executable in usr/bin/php?

 Answers

4

Start by typing at a command line:

whereis php

Do this as the user that the cron job will be run under. This will show you the path to your executable. You can then use that path (if it's not already in your PATH variable) in your cron entry:

5 * * * * /your/path/to/php /var/www/some/path/script.php

Edit: you may need to install the php5-cli (Ubuntu package name) package if all you have is the Apache PHP module installed. This will give you the binary executable that you can run from a command line.

Friday, October 7, 2022
1

crontab command usage

usage:  crontab [-u user] file
        crontab [-u user] [ -e | -l | -r ]
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

So,

$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');

The above can be used for both create and edit/append provided the user has the adequate file write permission.

To delete jobs:

echo exec('crontab -r');

Also, take note that apache is running as a particular user and that's usually not root, which means the cron jobs can only be changed for the apache user unless given crontab -u privilege to the apache user.

Tuesday, August 16, 2022
4

Try to call the script via http with wget like so:

* * * * * wget http://mysite.com/myscript >/dev/null 2>&1
Saturday, November 19, 2022
 
sachin
 
1

To execute devicecheck.php every 1 hour try the following:

Method A :: Execute the script using php from the crontab

# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php

Method B: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php

UPDATE::

# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php
Wednesday, November 2, 2022
 
4
  • Most likely, your script inside /var/www/html/cron is owned by www-data user. Depending on your setup the user executing the cronjob doesn't have permissions to run this file.

  • There are no $_COOKIEs on the command line. A cookie is sent by the users browser. As cli isnt a browser, so you can't read the cookies value. Though you could access a users $_SESSION, but that's another story. Have a look here Is it possible to read cookie/session value while executing PHP5 script through command prompt? for further details.

Your cronjob line looks valid, so the problem will be one of above points. To verify the first, try something without the use of $_COOKIE in your file, like simply

mkdir('/var/www/html/cron/testdir');

just to see if the file can be accessed and a directory is created inside above dir. If it can't be accessed, create a new group and add both the current user ( find out with

ls -al

in /var/www/html/cron ) and the user running the cronjob to the group, then make that group own the file you want to run. See the accepted answer on this question: Set user permissions | Artisan command will not run in code but works fine on command line I posted some time back on how to do that.

For the $_COOKIE problem, you will have to find another solution. For example use Redis or Memcached as a caching service that can be accessed both by online and cli configurations.

Consider to add

1>> /dev/null 2>&1

to the end of your cronjob line, if you don't do so and let cron run this every minute, you will get masses of logfiles.

For the sake of completenes on possible pitfalls when working with php and the cli, allways make sure to provide full paths to your files.

Saturday, August 6, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :