timmah1 Posted July 11, 2008 Share Posted July 11, 2008 i'm trying to set up a cron job to update my database. i can get the cron job to run with no problem the problem is, the script that is suppose to run, don't execute properly. the php script grabs the information from the database, puts it into a form, then updates a different database with the new POST variables. the script works perfect if I do it manually, but i need it to run by itself. i've been looking for examples of what a php script would look like as a cron job, but i haven't been able to find an example php script anywhere. does anybody know how to process a form with a cron job? thanks in advance Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 so when you run it from the command line it works fine but no in cron? Remember when you run a script, it is running in your local shell, with your local env variables set. When cron runs a script, it's running under cron's shell and those variables may be different. So make sure that you have all paths explicitly defined. bad: 0 0 * * * php myscript.php better: 0 0 * * * /usr/bin/php -q /home/phpFiles/myscript.php best: 0 0 * * */usr/bin/php -q /home/phpFiles/myscript.php >> /var/log/myscript.log Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 in the cpanel, i have the cron job set as /usr/bin/php -q /home/public_html/ranking1.php on top of my php script(ranking1.php), i have this #!/usr/local/bin/php -q (based off of tutorials I saw) than my script is a script for a cron job the same as a basic php script? Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 cron is the exact same as you typing the string in the control panel. Cron takes whatever you have there and runs it. go to a terminal and type in: cat /var/log/syslog | grep CRON and hit enter. That will bring up all logged entries for cron. Look for something like: Jul 11 17:20:01 user /USR/SBIN/CRON[24228]: (user) CMD (/usr/bin/php -q /home/public_html/ranking1.php | addts which indicates the script is actually running. better yet, change your crontab to be: /usr/bin/php -q /home/public_html/ranking1.php >> /var/log/ranking1.log let it run for a while and see if log entries are popping up in there. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 this is what I get /bin/sh: /var/log/ranking1.log: No such file or directory Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 what do you get when you type in: crontab -e and what flavor of nix is this? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 i'm confused. you say, go to a terminal, what is a terminal? where do i go to perform shell commands? Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 sorry about that, I guess root does only have write permissions to /var/log. you can as root or sudo: touch /var/log/ranking1.log chmod a+rw ranking1.log or just change the path to your home dir (change 'me'): /usr/bin/php -q /home/public_html/ranking1.php >> /home/me/ranking1.log but anyway, I have to go and I'm probably not going to be around until monday so I'll leave these parting words: 1. Double check that you can run the script (successfully) from the command line as the user who's crontab you are setting using the exact command you are setting in cron 2. Check that cron is actually running by scheduling /bin/echo "Cron is working" >> /home/mydir/crontest.log to run every minute then see if it is writing to the file The terminal is you BASH (or CSH or xterm) shell. This is Linux right? It should be somewhere in your accessories. If you're not sure look up the documentation for your distro. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 I'm getting the very form that is on the page the cron does not process the form that is on ranking1.php how can i get the form to process? this is what shows on the log <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body onLoad="document.rankform.submit()"> <form name="rankform" id="rankform" method="post" action=""> <input type='hidden' size='10' name='rank[]' value='1'>1 <input type='hidden' size='40' name='username[]' value='the roadster' />the roadster <input type='hidden' size='40' name='groupies[]' value='18' />18 <input type='hidden' size='40' name='booze[]' value='0' />0 <input type='hidden' size='40' name='condoms[]' value='0' />0 <input type='hidden' size='40' name='roadies[]' value='12' />12 <input type='hidden' size='40' name='drugs[]' value='0' />0 <input type='hidden' size='40' name='guns[]' value='38' />38 <input type='hidden' size='40' name='worth[]' value='328' />$328.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='2'>2 <input type='hidden' size='40' name='username[]' value='killgore' />killgore <input type='hidden' size='40' name='groupies[]' value='449' />449 <input type='hidden' size='40' name='booze[]' value='3185' />3185 <input type='hidden' size='40' name='condoms[]' value='3203' />3203 <input type='hidden' size='40' name='roadies[]' value='279' />279 <input type='hidden' size='40' name='drugs[]' value='1935' />1935 <input type='hidden' size='40' name='guns[]' value='640' />640 <input type='hidden' size='40' name='worth[]' value='15162' />$15,162.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='3'>3 <input type='hidden' size='40' name='username[]' value='Pro528' />Pro528 <input type='hidden' size='40' name='groupies[]' value='436' />436 <input type='hidden' size='40' name='booze[]' value='2315' />2315 <input type='hidden' size='40' name='condoms[]' value='1601' />1601 <input type='hidden' size='40' name='roadies[]' value='134' />134 <input type='hidden' size='40' name='drugs[]' value='3699' />3699 <input type='hidden' size='40' name='guns[]' value='410' />410 <input type='hidden' size='40' name='worth[]' value='11841' />$11,841.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='4'>4 <input type='hidden' size='40' name='username[]' value='Comawhite21' />Comawhite21 <input type='hidden' size='40' name='groupies[]' value='361' />361 <input type='hidden' size='40' name='booze[]' value='1396' />1396 <input type='hidden' size='40' name='condoms[]' value='1449' />1449 <input type='hidden' size='40' name='roadies[]' value='108' />108 <input type='hidden' size='40' name='drugs[]' value='1613' />1613 <input type='hidden' size='40' name='guns[]' value='384' />384 <input type='hidden' size='40' name='worth[]' value='8219' />$8,219.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='5'>5 <input type='hidden' size='40' name='username[]' value='SlaughterHead' />SlaughterHead <input type='hidden' size='40' name='groupies[]' value='0' />0 <input type='hidden' size='40' name='booze[]' value='93' />93 <input type='hidden' size='40' name='condoms[]' value='90' />90 <input type='hidden' size='40' name='roadies[]' value='1' />1 <input type='hidden' size='40' name='drugs[]' value='91' />91 <input type='hidden' size='40' name='guns[]' value='91' />91 <input type='hidden' size='40' name='worth[]' value='1139' />$1,139.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> </body> </html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body onLoad="document.rankform.submit()"> <form name="rankform" id="rankform" method="post" action=""> <input type='hidden' size='10' name='rank[]' value='1'>1 <input type='hidden' size='40' name='username[]' value='the roadster' />the roadster <input type='hidden' size='40' name='groupies[]' value='18' />18 <input type='hidden' size='40' name='booze[]' value='0' />0 <input type='hidden' size='40' name='condoms[]' value='0' />0 <input type='hidden' size='40' name='roadies[]' value='12' />12 <input type='hidden' size='40' name='drugs[]' value='0' />0 <input type='hidden' size='40' name='guns[]' value='38' />38 <input type='hidden' size='40' name='worth[]' value='328' />$328.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='2'>2 <input type='hidden' size='40' name='username[]' value='killgore' />killgore <input type='hidden' size='40' name='groupies[]' value='449' />449 <input type='hidden' size='40' name='booze[]' value='3185' />3185 <input type='hidden' size='40' name='condoms[]' value='3203' />3203 <input type='hidden' size='40' name='roadies[]' value='279' />279 <input type='hidden' size='40' name='drugs[]' value='1935' />1935 <input type='hidden' size='40' name='guns[]' value='640' />640 <input type='hidden' size='40' name='worth[]' value='15162' />$15,162.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='3'>3 <input type='hidden' size='40' name='username[]' value='Pro528' />Pro528 <input type='hidden' size='40' name='groupies[]' value='436' />436 <input type='hidden' size='40' name='booze[]' value='2315' />2315 <input type='hidden' size='40' name='condoms[]' value='1601' />1601 <input type='hidden' size='40' name='roadies[]' value='134' />134 <input type='hidden' size='40' name='drugs[]' value='3699' />3699 <input type='hidden' size='40' name='guns[]' value='410' />410 <input type='hidden' size='40' name='worth[]' value='11841' />$11,841.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='4'>4 <input type='hidden' size='40' name='username[]' value='Comawhite21' />Comawhite21 <input type='hidden' size='40' name='groupies[]' value='361' />361 <input type='hidden' size='40' name='booze[]' value='1396' />1396 <input type='hidden' size='40' name='condoms[]' value='1449' />1449 <input type='hidden' size='40' name='roadies[]' value='108' />108 <input type='hidden' size='40' name='drugs[]' value='1613' />1613 <input type='hidden' size='40' name='guns[]' value='384' />384 <input type='hidden' size='40' name='worth[]' value='8219' />$8,219.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> <input type='hidden' size='10' name='rank[]' value='5'>5 <input type='hidden' size='40' name='username[]' value='SlaughterHead' />SlaughterHead <input type='hidden' size='40' name='groupies[]' value='0' />0 <input type='hidden' size='40' name='booze[]' value='93' />93 <input type='hidden' size='40' name='condoms[]' value='90' />90 <input type='hidden' size='40' name='roadies[]' value='1' />1 <input type='hidden' size='40' name='drugs[]' value='91' />91 <input type='hidden' size='40' name='guns[]' value='91' />91 <input type='hidden' size='40' name='worth[]' value='1139' />$1,139.00 <input type="submit" name="submit" id="submit" value="Update Rankings"></td> </form> </body> </html> those values are correct, but now i need that form to process. how is this done? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 11, 2008 Share Posted July 11, 2008 How were you expecting the form to be executed? Only web browsers know how to do that and when using the cron, there is no web browser. Ken Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 That's why I'm here, to find out exactly how I can do this cron Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 I see the prob now. A php shell script doesn't use the html wrapper. It does stuff and echos plain text. What exactly are you attempting to do with this script? If you are moving data to a differeny server you would place a web page on the receiving server that would process incoming get data, then call that url with the needed get data on the sending server with cURL or some other method. Although I will say this isn't the best way to handle the transaction. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 What I need the script to do is select all the users, then add the different variables, then update the database with the new variables that were created when it originally selected the database. For example: (the actual database is not this, just an example) There are 2 users with fields name requests process test1 6 4 test2 5 5 the script will subtract process from request test1 = 2 test2 = 0 then it will update another database with those fields i would like to set this up to do it automatically if I can Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 Is the second database (the one being updated) on the same computer as the first? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 yes it is Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 this is the script that selects everything #!/usr/local/bin/php -q <?php require("*****config.php"); $result = mysql_query("SELECT * FROM profile ORDER BY worth DESC"); $i = 1; while($row = mysql_fetch_array($result)) { $group = $row['groupies']*1; $roadies1 = $row['roadies']*10; $guns = $row['guns']*5; $booze = $row['booze']*1; $drugs = $row['drugs']*1; $condoms = $row['condoms']*1; $semis = $row['semis']*500; $bus = $row['bus']*400; $number = $group+$roadies1+$guns+$booze+$drugs+$condoms+$semis+$bus; $english_format_number = number_format($number, 2, '.', ','); echo " <input type='hidden' size='10' name='rank[]' value='$i'>$i <input type='hidden' size='40' name='username[]' value='$row[username]' />$row[username] <input type='hidden' size='40' name='groupies[]' value='$row[groupies]' />$row[groupies] <input type='hidden' size='40' name='booze[]' value='$row[booze]' />$row[booze] <input type='hidden' size='40' name='condoms[]' value='$row[condoms]' />$row[condoms] <input type='hidden' size='40' name='roadies[]' value='$row[roadies]' />$row[roadies] <input type='hidden' size='40' name='drugs[]' value='$row[drugs]' />$row[drugs] <input type='hidden' size='40' name='guns[]' value='$row[guns]' />$row[guns] <input type='hidden' size='40' name='worth[]' value='$number' />$$english_format_number"; ++$i; } ?> then this is the script that updates <?php require("*****config.php"); for($i=0;$i < count($_POST['username']);$i++ ){ $rank= $_POST['rank'][$i]; $worth = $_POST['worth'][$i]; $username = $_POST['username'][$i]; $query= sprintf("UPDATE ranking SET rank='%s' WHERE username='%s'", mysql_real_escape_string($rank), mysql_real_escape_string($username)); mysql_query($query) or die ("Error in query: $query"); $query1= sprintf("UPDATE profile SET worth='%s' WHERE username='%s'", mysql_real_escape_string($worth), mysql_real_escape_string($username)); mysql_query($query1) or die ("Error in query1: $query1"); } ?> Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 You can skip all of the html. Get the data from the db and process it. Then instead of echoing html, just generate you update sql statements with the data and execute them. Does that make sense? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 11, 2008 Author Share Posted July 11, 2008 i got it to work doing this #!/usr/local/bin/php -q <?php require("******config.php"); $result = mysql_query("SELECT * FROM profile ORDER BY worth DESC"); $i = 1; while($row = mysql_fetch_array($result)) { $group = $row['groupies']*1; $roadies1 = $row['roadies']*10; $guns = $row['guns']*5; $booze = $row['booze']*1; $drugs = $row['drugs']*1; $condoms = $row['condoms']*1; $semis = $row['semis']*500; $bus = $row['bus']*400; $number = $group+$roadies1+$guns+$booze+$drugs+$condoms+$semis+$bus; $english_format_number = number_format($number, 2, '.', ','); $rank[] = $i; $username[] = $row['username']; $worth[] = $number; ++$i; } for($i=0;$i < count($username);$i++ ){ $rank1 = $rank[$i]; $worth1 = $worth[$i]; $username1 = $username[$i]; $query= sprintf("UPDATE ranking SET rank='%s' WHERE username='%s'", mysql_real_escape_string($rank1), mysql_real_escape_string($username1)); mysql_query($query) or die ("Error in query: $query"); $query1= sprintf("UPDATE profile SET worth='%s' WHERE username='%s'", mysql_real_escape_string($worth1), mysql_real_escape_string($username1)); mysql_query($query1) or die ("Error in query1: $query1"); } echo "success"; ?> thank you so much mbeals, you have been extremely helpful have a great weekend!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.