Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cron job won't write to file

I run a windows 2003 server and tried to run a code like this every 15 minutes:

require("db_connect.php");
$conn = db_connect();

//list online brukere - flytt funksjon til separat side for bedre ytelse
$time = time() - 900;
$query ="SELECT username FROM tbl_user WHERE last_online >= $time";
$online_users; 
if ($result = $conn->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $online_users .= $row["username"].":";
    }
    $result->close();
}   

$filename = "online_users.txt";
$fp = fopen($filename,"w");
fputs($fp,$online_users);
fclose($fp);

When I go to the url or run it from the command line it works and write to file. But the task just run and don't save the file.. What's wrong?

like image 306
ganjan Avatar asked Feb 25 '23 13:02

ganjan


2 Answers

$filename = dirname(__FILE__) . "/online_users.txt";
like image 148
dmytrivv Avatar answered Mar 05 '23 15:03

dmytrivv


$filename = "online_users.txt"; <-- this is using relative path

You might not have permission to write to relative path.

So, chose another path with enough write permission, and please, use absolute path instead.

like image 27
ajreal Avatar answered Mar 05 '23 15:03

ajreal