Search code examples
phpsockstor

How to check if Tor is running within PHP?


I'm using Tor with PHP, for now, everything is working properly. But, when I try to start Tor from php it'll sometimes throw an error becuase Tor is currently running, so i've been wondering if there is any method to check Tor's status(running or not)

I can only use SOCKS5 protocol or Linux CLI; these are my only options.

Note: This functions connects to Tor(it works).

function init()
    {
        //Connect to Tor

        $socket = fsockopen($this->proxyIp, $this->proxyPort);      
        fwrite($socket, 'AUTHENTICATE' . PHP_EOL);

    }  

Solution

  • I found a solution to this problem, It's basically getting all running processes and searching for "tor" among them(based on Bass Jobsen's comment).

    That's the code:

    Note: this code was tested in Ubuntu only, but It should work on most Linux OS'es.

    function isRunning()
    {
        //Get all running processes and search for "tor" within them
        //The "[" and "]" are used to execlude ps and grep from the returned result.
        $CLIResult = exec('ps aux | grep -w [t]or');
    
        //If no processes was found(= Tor is not running..)
        return empty($CLIResult);
    
    }