The problem:
I made a script that every minute - it restarts a program if it is told to in a mySQL database as well as if the program is not responding. I created it without a problem; it worked in the terminal, PHP and so on. Then why won't it work in Crontab? What my server's doing to the PHP file is simple - it wants to miss half of the script out! Instead of checking if the server is offline, it just automatically starts yet another process.

The code:

#!/usr/bin/php

<?php
/* Make sure the user can execute commands... */
putenv('SHELL=/bin/bash');

/* Read the mySQL database */
$sql_info  = "SELECT * FROM servers";
$result_info = mysql_query($sql_info);

while($row = mysql_fetch_array($result_info, MYSQL_ASSOC))
{    
    $m_pid = exec("pidof ".$row['server_exe']);
    if(!$m_pid)
    {
        if($row['auto_restart'])
        {
            $shellw = "cd ".$row['server_dir']." ; su nobody -c ./".$row['server_exe']." &";
            proc_close(proc_open ($shellw, array(), $foo));
            echo "Started server: ".$row['server_owner']."\n";
        }
    }
}
mysql_close($conn);
?>

Brainwave: Would I need to use Bash in order to get the process ID?

Hello David,
Usually if the script work on command line but not on crontab it's an environment problem.
Script executed in crontab have a smaller environment so my sugegstion is to check all path and do absolute path for all your exec/system.

Also try to dump the contents of the variables while the script it's executed in the crontab usually this help understand what's the problem.

Good luck

Also try to dump the contents of the variables while the script it's executed in the crontab usually this help understand what's the problem.

Sorry for such a dumb response, but how the heck would you do that?


--------------------------------------

Hmm... I think that the variable $m_pid's returning something like 'Incorrect command' or something that would cause the PHP string to be permanently true. How would I go about making a shell command that would get the array from php? Something like:

PROCESS = `pidof <?=$row['server_exe']?>`

Hmm?

Do some echo $yourvariables and in the crontab do a redirect of all your standard output to a file.

Like:
*/1 * * * * /pathtoyour/script.php >> /tmp/tmpfile

A bit raw but hope it work :D

Good luck

OH. I forgot about > and >>. Haha. BTW, could you look at the above post?

------

The PID IS: The PID IS: Started server: test
The PID IS: The PID IS: The PID IS: The PID IS: The PID IS: The PID IS: The PID IS: The PID IS: The PID IS:

Ignore the mass amounts of 'The PID IS:', that was part of the debug. The 'server started for test' is something else to ignore. But... 'The PID of' should for 3 or more prompts - should have PID's!

echo "The PID IS: ".$m_pid;

Err... that returns a value when not in cron. $m_pid is in the first post.

Alright, I has solved it. I found a UseNet topic and well, I had a look around and got some interesting stuff. I got this piece of code:

ps aux | grep samp022svr.usr | grep -v grep | awk {'print $2'}

and it works!

What this does for those people who don't understand is that the PS function gets the information, then grep looks for the application name. grep then removes itself from the pecking order, leaving only the application name to be sorted into INT2: Which turns out to the PID. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.