0

I have an script that is supposed to run via cmd on a local machine using this command line: @php -c ./ StartBot.php %1

It's an script for a guild chat bot for an MMORPG.

This is the php code of StartBot.php

<?php
/*
* StartBot.php - Starts and restarts the bot
*
* BeBot - An Anarchy Online & Age of Conan Chat Automaton
* Copyright (C) 2004 Jonas Jax
* Copyright (C) 2005-2010 Thomas Juberg, ShadowRealm Creations and the BeBot development team.
*
* Developed by:
* - Alreadythere (RK2)
* - Blondengy (RK1)
* - Blueeagl3 (RK1)
* - Glarawyn (RK1)
* - Khalem (RK1)
* - Naturalistic (RK1)
* - Temar (RK1)
*
* See Credits file for all aknowledgements.
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; version 2 of the License only.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
*  USA
*
* File last changed at $LastChangedDate: 2009-01-23 09:21:51 +0100 (Fri, 23 Jan 2009) $
* Revision: $Id: StartBot.php 1959 2009-01-23 08:21:51Z temar $
*/

/*
Take a decent stab at what OS we run on and try using some sane defaults
*/
$os = getenv("OSTYPE");
if (empty($os))
{
    $os = getenv("OS");
}

if (preg_match("/^windows/i", $os))
{
    /*
    This default should work for Windows installs where php is installed to the bot directory.
    */
    $php_bin = "php.exe";
    $php_args = " -c ./ ";
    $main_php = "Main.php"; 

    /*
    If the above fails you can try specifying full paths, example:
    $php_bin = "C:\php\php.exe";
    $main_php = "C:\BeBot\Main.php";
    */
}
else
{
    /*
    This is a sane default for the php binary on Unix systems.
    If your php binary is located someplace else, edit the php_bin path accordingly.
    */
    $php_bin = trim(shell_exec('which php'));
    $php_args = " -c ./ ";
    $main_php = "Main.php";
}
$confc = TRUE;
require_once "./Sources/Conf.php";
if($argv[1] != $conf -> argv)
{
    echo "Use \"StartBot.php ".$conf -> argv."\" to start bot next time\n";
    $argv[1] = $conf -> argv;
    $conf -> ask("Press Enter to load Bot");
    if(!$argv[1] || $argv[1] == "")
        $argc = 1;
    else
        $argc = 2;
}
if(!empty($conf -> pw))
{
    $pw = $conf -> pw;
    $conf -> pw = NULL;
}

// Create the command to execute in the system() call of the main loop:
$systemcommand = $php_bin . $php_args . " " . $main_php;
if ($argc > 1)
{
    $systemcommand .= " " . $argv[1];
}

while (true)
{
    if($pw)
    {
        $fp = fopen('./conf/pw', 'w');
        fwrite($fp, $pw);
        fclose($fp);
    }
    $last_line = system($systemcommand);

    if (preg_match("/^The bot has been shutdown/i", $last_line))
    die();
    else
    sleep(1);
}

?>

The problem is that I don't have SSH access from my cpanel.

Is possible to run this script via another php file or something?

Thanks

5
  • why you will not run depui the browser? Commented Mar 7, 2016 at 8:55
  • When do you need to run this? If it is periodically, you can use a task scheduler like Cron on linux. Commented Mar 7, 2016 at 8:59
  • Can you run exec in your php environment? php.net/manual/en/function.exec.php Commented Mar 7, 2016 at 9:06
  • bhrached You mean run this on my browser mydomain.com/StartBot.php ?? It will work? jeroen I need to run it once and will be on since I stop it or restart, don't know either how to restart it. @SamuelCloete I think yes I can, but I have to make a php file with the exec code and run it from browser? Commented Mar 7, 2016 at 9:56
  • "Is possible to run this script via another php file or something?" Yes, you can run exec within another php script. Commented Mar 7, 2016 at 17:18

1 Answer 1

1

Yes you can do this from another PHP file, to run a command on your server via PHP use the exec() command as so:

<?php
exec("@php -c ./ StartBot.php %1");
?>

You can then run this file via your browser.

You should try and get SSH access to your server though as a long-term solution this isn't the way to go, who is your host?

Good luck

Try this for live output:

while (@ ob_end_flush()); // end all output buffers if any
$cmd = "@php -c ./ StartBot.php %1";
$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
    echo fread($proc, 4096);
    @ flush();
}
echo '</pre>'
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but now have a problem, via cmd you are able to see status messages like script started, script failed because... etc any chances too see those messages from browser?
Not sure about by browser, who's your host
Tried this but the screen on browser dont show nothing, just blank. <?php while (@ ob_end_flush()); // end all output buffers if any $cmd = "@php -c ./ StartBot.php %1"; $proc = popen($cmd, 'r'); echo '<pre>'; while (!feof($proc)) { echo fread($proc, 4096); @ flush(); } echo '</pre>' ?> I'm trying to do it on a free webhosting, tried biz.nf and x10hosting for now
I would highly recomend moving to something like a $5 digital ocean server and you can setup LAMP and SSH, you really dont want to be doing it likr this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.