PHP Noob
PHP Noob

Reputation: 451

Unix .sh file works incorrect when started from CronJob

Here is the issue. I'm working on a tracking script with a China tracking unit (not important). The two files I have is as follows:

7778.php

#!/usr/bin/php -q 
<?php
error_reporting(0); 
set_time_limit(0); 

$address = 'SERVER IP'; 
$port = 7778; 
$q_count = 1;

if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) 
{ 
  echo "socket_create() failed, reason: " . socket_strerror($master) . "\n"; 
} 

socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); 

if (($ret = socket_bind($master, $address, $port)) < 0) 
{ 
   echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n"; 
} 


if (($ret = socket_listen($master, SOMAXCONN)) < 0) 
{ 
  echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n"; 
} 

$read_sockets = array($master);

//---- Create Persistent Loop to continuously handle incoming socket messages --------------------- 
while (true) 
{ 
    $changed_sockets = $read_sockets; 
    $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); 
    foreach($changed_sockets as $socket)
    { 
        if ($socket == $master)
        { 
            if (($client = socket_accept($master)) < 0) 
            { 
                echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; 
                continue; 
            }
            else
            { 
                array_push($read_sockets, $client);
                print "[".date('Y-m-d H:i:s')."] ".$client." CONNECTED "."(".count($read_sockets)."/".SOMAXCONN.")\r\n";
            }
        }
        else
        { 
            $bytes = @socket_recv($socket, $buffer, 2048, 0); 
            if ($bytes == 0)
            { 
                $index = array_search($socket, $read_sockets); 
                unset($read_sockets[$index]); 
                if (socket_close($client))
                {
                    print "Connection closed\r\n";
                }
            }
            else
            {
                print $buffer."\r\n"; 
            }

            print "All Done\r\n";
            print "---------------------------------------------------------------------------------------------------------\r\n";
            $index = array_search($socket, $read_sockets); 
            unset($read_sockets[$index]); 
            if (socket_close($client))
            {
                print "Connection closed\r\n";
            }
        }
    }
}
?>

and then

port_start.sh with code:

#!/bin/sh
cd /home/path/to/script/

if netstat -tulpn | grep 7778 > /dev/null
then
    echo 7778 - Online
else
    ./7778.php 
fi 

Now what I would do to test the script is ssh onto my server, navigate to the file directory and type "./7778.php". This will then start my php file, which will open port 7778, my unit will then connect, and my script will dump the data/buffer on the screen. Even if I start the script from the .sh file by typing the following "./port_start.sh" everything works perfect. I can also use Putty and open a RAW connection to my server and the port, and it works. No how it must work is as follows. The unit will make a connection, the script will accept it and receive the data, and then close the connection. My problem occurs when I open the port by running the .sh file from my cronjob with the following code "*/1 * * * * /home/path/to/file/port_start.sh > /dev/null". It will open it, and I can verify that its open, but nothing can connect to it. Why would that be?

Upvotes: 0

Views: 65

Answers (2)

PHP Noob
PHP Noob

Reputation: 451

Thank you so much Isa for your response. In my case this was not the problem. Everything opens and works as it should. But I have a little script to check if the port is open. My problem happened there. When I ran the script, it would keep the port open but would kill my script. That's why I could see that the port was still open but nothing would be happening. The script I use to check the port is as follows if someone ever sits with the same problem:

        $errno = "";
        $errstr = "";
        $fp = @fsockopen($domain, $port, $errno, $errstr, 2); 
        $status = "";
        if (!$fp) {  
            $status = "<img src='red.gif' alt='Status: Down, Domain: $domain, Port: $port ($errstr)'>\n";  
        } 
        else 
        {  
            $status = "<img src='green.gif' alt='Status: Up, Domain: $domain, Port: $port'>\n";  
        } 
        @fclose($fp); 

But again Isa your response made sense but didn't apply to me

Upvotes: 0

isa
isa

Reputation: 462

When working from cronjob, computer cannot locate the command program. If the program inside /home/root/bin directory, you should call it like/home/root/bin/netstat ... you can locate the progrma by running "locate netstat" and "locate grep". This may solve your problem.

Upvotes: 1

Related Questions