user1206214
user1206214

Reputation: 63

cron job giving me error on functioning script

I'm pretty new to cron jobs so this might be an easy answer but I can't figure out what is going wrong here. I'm using a cron job to run a php script to rename a file. I know the script works because when I load the php file it does what it is supposed to do. However the cron job sends me an error email every time. Here is the script I'm running:

<?php
rename("login/pools.php", "login/poolsbk.php");
?>

I am using the cron command: php -q /home/dollarca/public_html/renamescript1.php

and here is the error it generates: Warning: rename(login/pools.php,login/poolsbk.php): No such file or directory in /home/dollarca/public_html/renamescript1.php on line 2

From the error it's putting out, it seems to me that it is trying to rename a file within the script instead of the files in the directory. Am I using the wrong cron command?

Upvotes: 1

Views: 351

Answers (2)

Carsten
Carsten

Reputation: 18446

I suppose if you run the PHP script manually, you do it from the directory /home/dollarca/public_html. If cron runs it, it won't do that. What you should do is to either use absolute file paths (i.e. /home/dollarca/public_html/login/pools.php instead of login/pools.php) or use the chdir() to change to that directory first:

<?php
chdir("/home/dollarca/public_html");
rename("login/pools.php", "login/poolsbk.php");
?>

Upvotes: 2

Fosco
Fosco

Reputation: 38526

The working directory when cron runs the job is different than the working directory you are manually running the script from.

The easy solution is to use the full path in the function.

rename("/home/dollarca/public_html/login/pools.php", "/home/dollarca/public_html/login/poolsbk.php");

Upvotes: 3

Related Questions