Unix Cron Job, Error

  • Funny_Fuzz
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1517

Post 3+ Months Ago

Hi. This is my first post in the Linux forum.
I have setup a cron job using cPanel, and have done everything correctly, but it doesn't seem to like something in my PHP code I've done. I've told the Cron Job to every 5 minutes, execute a script in my directory called Member-Status.php.
The path is correct, the code is correct, the command is correct, but for some odd reason, it keeps outputting:

/home/sharepix/public_html/pixeljunk/Member-Status.php: line 1: ?php: No such file or directory/home/sharepix/public_html/pixeljunk/Member-Status.php: line 2: syntax error near unexpected token `"Config.php"'/home/sharepix/public_html/pixeljunk/Member-Status.php: line 2: `include("Config.php");'

Inside Member-Status, I have:
PHP Code: [ Select ]
<?php
 
include("Config.php");
 
$result = mysql_db_query($Database, "select * from Member_List") or die (mysql_error());
 
 
 
if (mysql_num_rows($result)) {
 
   while ($qry = mysql_fetch_array($result)) {
 
 
 
if($qry[Login] == "Online") {
 
$time1 = $qry[Last_Activity_Time_Hours];
 
$time2 = $qry[Last_Activity_Time_Minutes];
 
 
 
$year = date("y");
 
$day = date("d");
 
$month = date("m");
 
$hour = date("h");
 
$minute1 = date("i");
 
$minute = $minute1 + 10;
 
if($minute == 60) {
 
$minute = 0;
 
 
 
$time3 = date("y-m-d H:i:s",mktime($hour, $minute, 0, $month, $day, $year));
 
}
 
 
 
 
 
if( ($time1 == date("H")) && ($time2 == $time3) ) {
 
 
 
include("Config.php");
 
mysql_query("UPDATE Member_List SET `Login`='Offline' WHERE Username='$qry[Username]'")
 
or die("Could not update member's login status because ".mysql_error());
 
 
 
}
 
 
 
 
 
}
 
 
 
 
 
 
 
       }
 
       }
 
       ?>
  1. <?php
  2.  
  3. include("Config.php");
  4.  
  5. $result = mysql_db_query($Database, "select * from Member_List") or die (mysql_error());
  6.  
  7.  
  8.  
  9. if (mysql_num_rows($result)) {
  10.  
  11.    while ($qry = mysql_fetch_array($result)) {
  12.  
  13.  
  14.  
  15. if($qry[Login] == "Online") {
  16.  
  17. $time1 = $qry[Last_Activity_Time_Hours];
  18.  
  19. $time2 = $qry[Last_Activity_Time_Minutes];
  20.  
  21.  
  22.  
  23. $year = date("y");
  24.  
  25. $day = date("d");
  26.  
  27. $month = date("m");
  28.  
  29. $hour = date("h");
  30.  
  31. $minute1 = date("i");
  32.  
  33. $minute = $minute1 + 10;
  34.  
  35. if($minute == 60) {
  36.  
  37. $minute = 0;
  38.  
  39.  
  40.  
  41. $time3 = date("y-m-d H:i:s",mktime($hour, $minute, 0, $month, $day, $year));
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. if( ($time1 == date("H")) && ($time2 == $time3) ) {
  50.  
  51.  
  52.  
  53. include("Config.php");
  54.  
  55. mysql_query("UPDATE Member_List SET `Login`='Offline' WHERE Username='$qry[Username]'")
  56.  
  57. or die("Could not update member's login status because ".mysql_error());
  58.  
  59.  
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.        }
  76.  
  77.        }
  78.  
  79.        ?>

I've posted this in the programming forum, but they told me I'd get better suggestions over here.
Any help guys?
Thanks in advance!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

You need a shebang line on the top of your php script if you're trying to run that from a shell:
Code: [ Select ]
#!/usr/bin/php

The path needs to point to your php executable. If this isn't the correct path and you don't know what the correct path is, you can try:
PHP Code: [ Select ]
<?php
 
echo `which php`;
 
?>
  1. <?php
  2.  
  3. echo `which php`;
  4.  
  5. ?>

in a standard web script.
  • Funny_Fuzz
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1517

Post 3+ Months Ago

I don't seem to be able to get it to run from Shell for some odd reason. I first type "crontab -e". Next, I type in the correct command, but when I press enter to execute the command, it just goes onto the next space. Any ideas?
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

what happens if you just call the script itself from the command line?
  • Funny_Fuzz
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1517

Post 3+ Months Ago

Ok! I finally got it working. Not through shell though. I used cPanel to set it up for me automatically. I put a line of code that writes something to a text file everytime it's executed, so I could see if the Cron Job is working (Which it is). Now the only problem is: I can't think of a way for me to fix the code :?. Any ideas this213?

PHP Code: [ Select ]
<?php
 
 
 
$file = fopen("Text.txt","a");
 
fwrite($file,"If you can read this, cron job is working!");
 
fclose($file);
 
 
 
include("Config.php");
 
$result = mysql_db_query($Database, "select * from Member_List") or die (mysql_error());
 
 
 
if (mysql_num_rows($result)) {
 
   while ($qry = mysql_fetch_array($result)) {
 
 
 
if($qry[Login] == "Online") {
 
$time1 = $qry[Last_Activity_Time_Hours];
 
$time2 = $qry[Last_Activity_Time_Minutes];
 
 
 
$year = date("y");
 
$day = date("d");
 
$month = date("m");
 
$hour = date("h");
 
$minute1 = date("i");
 
$minute = $minute1 + 10;
 
if($minute == 60) {
 
$minute = 0;
 
 
 
$time3 = date("y-m-d h:i:s",mktime($hour, $minute, 0, $month, $day, $year));
 
}
 
 
 
 
 
if( ($time1 == date("h")) && ($time3 - 10 == $time2) ) {
 
 
 
include("Config.php");
 
mysql_query("UPDATE Member_List SET `Login`='Offline' WHERE Username='$qry[Username]'")
 
or die("Could not update member's login status because ".mysql_error());
 
 
 
}
 
 
 
 
 
}
 
 
 
 
 
 
 
       }
 
       }
 
       ?>
  1. <?php
  2.  
  3.  
  4.  
  5. $file = fopen("Text.txt","a");
  6.  
  7. fwrite($file,"If you can read this, cron job is working!");
  8.  
  9. fclose($file);
  10.  
  11.  
  12.  
  13. include("Config.php");
  14.  
  15. $result = mysql_db_query($Database, "select * from Member_List") or die (mysql_error());
  16.  
  17.  
  18.  
  19. if (mysql_num_rows($result)) {
  20.  
  21.    while ($qry = mysql_fetch_array($result)) {
  22.  
  23.  
  24.  
  25. if($qry[Login] == "Online") {
  26.  
  27. $time1 = $qry[Last_Activity_Time_Hours];
  28.  
  29. $time2 = $qry[Last_Activity_Time_Minutes];
  30.  
  31.  
  32.  
  33. $year = date("y");
  34.  
  35. $day = date("d");
  36.  
  37. $month = date("m");
  38.  
  39. $hour = date("h");
  40.  
  41. $minute1 = date("i");
  42.  
  43. $minute = $minute1 + 10;
  44.  
  45. if($minute == 60) {
  46.  
  47. $minute = 0;
  48.  
  49.  
  50.  
  51. $time3 = date("y-m-d h:i:s",mktime($hour, $minute, 0, $month, $day, $year));
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. if( ($time1 == date("h")) && ($time3 - 10 == $time2) ) {
  60.  
  61.  
  62.  
  63. include("Config.php");
  64.  
  65. mysql_query("UPDATE Member_List SET `Login`='Offline' WHERE Username='$qry[Username]'")
  66.  
  67. or die("Could not update member's login status because ".mysql_error());
  68.  
  69.  
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.        }
  86.  
  87.        }
  88.  
  89.        ?>

What I'm trying to do is, check the last time each user did something on my website. If nothing has been done for about 10 minutes, change their status to "Offline". I've gotten everything working "except" for the bit where checking if they're inactive for 10 minutes. Any help please!
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

Funny_Fuzz wrote:
What I'm trying to do is, check the last time each user did something on my website. If nothing has been done for about 10 minutes, change their status to "Offline".

Gosh, you should have said that at the outset.

Instead of going through all of this, just flush all of the stale sessions when you authenticate your users.

In other words, whenever a user logs in or whenever you're checking a user session, just flush all the stale sessions first. By doing so, you can then just pull out your still-good sessions for your "users online" display.

I don't know anything about your server specifically, but on most *nix systems, if you want to run a php script from the command line, you have to have a shebang line, so your script should look more like:
Code: [ Select ]
#!/usr/bin/php
<?php

$file = fopen("Text.txt","a");
fwrite($file,"If you can read this, cron job is working!");
fclose($file);
...
?>
  1. #!/usr/bin/php
  2. <?php
  3. $file = fopen("Text.txt","a");
  4. fwrite($file,"If you can read this, cron job is working!");
  5. fclose($file);
  6. ...
  7. ?>

And then your script has to be executable - at least mode 500 (I usually make cron scripts 700).

On a slightly different note, this would be more efficient to write it like so:
Code: [ Select ]
#!/usr/bin/php
<?php
$string = '"If you can read this, cron job is working"';
`echo $string >> Text.txt`;
?>
  1. #!/usr/bin/php
  2. <?php
  3. $string = '"If you can read this, cron job is working"';
  4. `echo $string >> Text.txt`;
  5. ?>

Rather than using fwrite and fopen, you're using native system calls (the backquotes `` are almost like doing exec() or shell_exec() or system()). Notice also that I enclose the double quotes in single quotes so they get passed to the echo statement.
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

Sorry about the double post, but I thought I should also mention that any time you use a cron script, you need to use the full path to the file. So instead of "Text.txt", you should have something like "/var/www/mydata/Text.txt" - or wherever you store that.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 29 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.