Hi,

I have ubuntu 8 and want to set a crone job.

When ubuntu starts, i login as;
username: ubuntu
password: ubuntu

I open terminal an type these:

root@ubuntu:/home/ubuntu# crontab -e
*/10 * * * * /var/www/project/index.php

(to run index.php every 10 seconds) and save it and wait.

Nothing happens!!!

Do i miss anything? Do you think root@ubuntu:/home/ubuntu# is wrong?

Note : In the System>Administration>Users and Groups, there are "ubuntu" and "root" as users. Nothing else. Also, by the clock top right end side, it says "ubuntu".

Thanks

Dani AI

Generated

Short recap: tried to run a PHP page from cron and nothing happened. noted the difference between PHP run by Apache and PHP run from the command line. The job was fixed by making the script runnable from cron — below are practical checks and tips that avoid the common pitfalls seen in this thread.

Cron basics and common pitfalls

  • Cron runs with a very small environment and interprets the scheduled item as a command, so always use absolute paths for binaries and files. If the .php file is referenced directly, it either needs a proper shebang and execute bit or you must call the PHP CLI interpreter. The PHP used under Apache (mod_php/FPM) loads a different ini and extensions than the CLI SAPI, so behavior can differ.
  • Cron schedules operate at minute resolution; you cannot schedule jobs in seconds with cron alone. For sub-minute work consider a short loop inside a continuously running process, a dedicated daemon, or modern timer facilities.

Quick debugging checklist

  • Verify a PHP CLI exists and its path and version.
  • Run the same command interactively as the user that cron will use to see any errors.
  • Inspect cron logs and capture stdout/stderr when testing so cron mail or logs show errors.
  • Use absolute file paths and check ownership/permissions for any files the script reads/writes.

Useful references and further reading

Security/correctness reminders: do not use the database root account for automated scripts; prefer a dedicated DB user with limited privileges and use mysqli or PDO with prepared statements.

Recommended Answers

All 4 Replies

do you have permission to run that file? also, since its a php script, it should be run from the apache+php, not straight as a command line

I can run it on Firefox (localhost/test/index.php). I change permission to root and try OR permission to ubuntu and try. Both doesn't work. This is exactly what i do.

I login to ubuntu as : username= ubuntu password= ubuntu and "ubuntu user" is shown next to clock, top right end side.

root@ubuntu:home/ubuntu# sudo gedit /var/www/test/index.php

<?php
$conn=mysql_connect("localhost", "root", "");
if (!$conn) {
    echo "err";
} else {
    mysql_select_db("mydb", $conn);

    $sql="insert into login (username, moment) values ('uuu', now())";
    $run=mysql_query($sql);
}
?>
root@ubuntu:home/ubuntu# cron -e -u ubuntu
1 * * * * /var/www/test/index.php
root@ubuntu:home/ubuntu# cron -e
1 * * * * /var/www/test/index.php

root@ubuntu:home/ubuntu# ls -l /var/spool/cron/crontabs
-rw ---------- 1 root crontab 264 2009-03-15 13:58 root
-rw ---------- 1 root ubuntu 242 2009-03-15 13:58 root~
-rw ---------- 1 ubuntu crontab 264 2009-03-15 13:58 ubuntu

why don't you do the same in bash/perl/python ?

SOLVED.

I tried to run php on the terminal. It didn't run because php5-cli wasn't installed. I have installed and changed cron to */1 * * * * /usr/bin/php /var/www/test/index.php run every 1 minute.

It works now. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.