Current location: Hot Scripts Forums » Programming Languages » PHP » Cron job problem with php


Cron job problem with php

Reply
  #1 (permalink)  
Old 08-05-08, 12:55 PM
Keyne's Avatar
Keyne Keyne is offline
Newbie Coder
 
Join Date: May 2007
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Cron job problem with php

Hi all,

I'm using this code below to create a cron job that start an script to save images one minute after the call:

PHP Code:

// ....

        
$date date("i h d m w"mktime(date("h"), date("i") + 1date("s"), date("m"), date("d"), date("Y")));

        
$command $date." root /opt/lampp/bin/php -f ".$cfg['path']."pages/atualizarFotosUsuarios.php?acao=start"
        
$cron_file $cfg['path']."cron/fotosUsuarios"
        
        
# Check for fotosUsuarios file. If it doesn't exist create it.
        # You must create the file from the browser to associate the proper group
        
if (file_exists($cron_file)) {  // if it exists, write new command
            
$open fopen($cron_file"w"); // This overwrites current line
            
fwrite($open$command);
            
fclose($open);
        } else { 
            
# If it Doesn't exist, Create it then write command
            
touch($cron_file); // create the file, Directory "cron" must be writeable
            
chmod($cron_file0777); // make new file writeable
            
            
$open fopen($cron_file"w");
            
fwrite($open$command);
            
fclose($open);
        }
        
        
# Start the cron job!
        
exec("crontab ".$cron_file);
//.... 
But this didn't work and to see why, I access the server via ssh and check if the cron was created, and it was not. To check if the comand is correct I try to execute by comand line: crontab /filename, and shows I my screen that has erros, and I open the file with VI and save again with out changes I try again put in cron tab, now works! Have you any idea about this problem?
Reply With Quote
  #2 (permalink)  
Old 08-05-08, 11:58 PM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Apache doesnt have permissions to create cron jobs?
Reply With Quote
  #3 (permalink)  
Old 08-06-08, 12:45 AM
id10tn00b's Avatar
id10tn00b id10tn00b is offline
Newbie Coder
 
Join Date: Oct 2004
Location: new mexico
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
wouldnt
PHP Code:

exec("crontab ".$cron_file); 

be
PHP Code:

exec("crontab $cron_file"); 

or
PHP Code:

exec('crontab '.$cron_file); 

?

thats just what i saw off the bat... of course, if you are starting crontab as a different user than the one for the crontab you want

PHP Code:

exec("crontab -u username $cron_file"); 

from man crontab:
Code:
SYNOPSIS
       crontab [-u user] file
       crontab [-u user] [-l | -r | -e] [-i] [-s]
__________________
http://www.yougetalife.com
i <?php therefore $i == am;
http://www.coredumpcomputerservice.com
Reply With Quote
  #4 (permalink)  
Old 08-06-08, 08:13 AM
Keyne's Avatar
Keyne Keyne is offline
Newbie Coder
 
Join Date: May 2007
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, I'm gonna try this.

But about use the variable inside the quotes, this change nothing. And I always use the variables outside the quotes, my premise is that inside the quotes I put only string, variables always go outsite for proporuse of organization and readiness (code coloring). I hate when I open a code and have to see variables with red color.
Reply With Quote
  #5 (permalink)  
Old 08-06-08, 12:13 PM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Going slightly off topic but PSPad highlights variables inside strings (when a normal quote (") is used) with the default variable color, which sometimes is good and sometimes not. Though it also parses ?> inside a string as a ending tag for php which is a pain when writing more complex regex patterns for example.
Reply With Quote
  #6 (permalink)  
Old 08-06-08, 01:52 PM
id10tn00b's Avatar
id10tn00b id10tn00b is offline
Newbie Coder
 
Join Date: Oct 2004
Location: new mexico
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
notepad2 and vim also color variables inside double quotes. it reminds me when i put a variable inside a single quote that it will be processed as a text string instead of a variable...
__________________
http://www.yougetalife.com
i <?php therefore $i == am;
http://www.coredumpcomputerservice.com
Reply With Quote
  #7 (permalink)  
Old 08-06-08, 04:25 PM
Keyne's Avatar
Keyne Keyne is offline
Newbie Coder
 
Join Date: May 2007
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
I think this is irrelevant, its only a personnel particularity. The two editor I use don't do that (Eclipse and DW).
But my educational premisse is: Inside quotes, only strings.

Last edited by Keyne; 08-06-08 at 04:27 PM.
Reply With Quote
  #8 (permalink)  
Old 08-07-08, 07:57 AM
Keyne's Avatar
Keyne Keyne is offline
Newbie Coder
 
Join Date: May 2007
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
id10tn00b,

I didn't have sucess with your code, I think it can be related with utf8 enconding? What do you think? The php file that create the cron file is in utf8 and always I open an php file with VI the accent didn't appear.

Have any way to create a file with touch saying the codification? Or nothing there to be ?


Quote:
Originally Posted by <?Wille?> View Post
Apache doesnt have permissions to create cron jobs?
I have total access to the server via ssh with root.


The error I receive when I try to put in crontab is:

"cronFile":1: premature EOF
errors in crontab file, can't install.

Last edited by Keyne; 08-07-08 at 08:02 AM.
Reply With Quote
  #9 (permalink)  
Old 08-07-08, 08:43 AM
Keyne's Avatar
Keyne Keyne is offline
Newbie Coder
 
Join Date: May 2007
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
I solve the problem "premature EOF" adding a new line at end of the file, but the php do not add the file to the crontab even with the the line

PHP Code:

exec("crontab ".$cron_file); 

but if I test it in ssh, it's ok.

Someone can help me?
Reply With Quote
  #10 (permalink)  
Old 08-08-08, 02:59 PM
id10tn00b's Avatar
id10tn00b id10tn00b is offline
Newbie Coder
 
Join Date: Oct 2004
Location: new mexico
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
im not sure, but this might help:

Code:
       crontab [-u user] file
       crontab [-u user] [-l | -r | -e] [-i] [-s]
it may be that your apache user doesnt have a shell (apache shouldnt) so i dont think it can execute a cron... i could be off on that but you might try using:

PHP Code:

exec("crontab -u user ".$file); 

replacing user with a valid username of course...
maybe?

when your server parses it, the crontab uses the current user by default and apache usually runs as apache:apache
when you are logged in and run the command straight from a php script on a command line, the crontab entry goes as your current user.

just like if you use su- to go into root to edit crontab for root for instance, you should use crontab -u username so that crontab doesnt try to use the underlying username instead.

thats just from what i learned about crontab
__________________
http://www.yougetalife.com
i <?php therefore $i == am;
http://www.coredumpcomputerservice.com
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Expert PHP Programmer Needed : Cron Job INTEL Job Offers & Assistance 5 09-06-06 07:31 PM
PHP & MySQL Optimization Job.... SwitchBlade Job Offers & Assistance 1 08-11-05 09:01 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
PHP & Cron DA Master PHP 4 05-04-05 02:48 PM
print "Content-type: problem with cron wannauseit Perl 3 05-01-05 09:37 AM


All times are GMT -5. The time now is 07:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.