PDA

View Full Version : Cron/ PHP / EXEC



corradodev
09-15-2009, 05:12 PM
Filename:sftp_reporting.sh

#!/usr/bin/expect
#get filename
set arg1 [lindex $argv 0]
#timeout in max 100 mins
set timeout 6000
#http://www.linuxquestions.org/questions/linux-general-1/transfer-file-using-expect-and-sftp-626822/
#spawn sftp dcorrado@paypaltech.com
spawn sftp sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com
#spawn sftp corradod@corradodev.com
#password
expect "password:"
#send "xxxx\n"
#send "xxxxx\n"
send "xxxx\n"
#get file
expect "sftp>"
send "get /ppreports/outgoing/$arg1 \r"
#back to command
expect "sftp>"
send -- "!\r\n"
#search for c* in response and move file
expect -indices -re "c*"
send -- "mv $arg1 /home/corradod/public_html/cart3/scripts/reports/ \r"
#search for c* then exit command
expect -indices -re "c*"
send -- "exit \r"
#exit sftp
expect "sftp>"
send "exit \r"
Filename:test.php

<?php
exec("echo '************************************************* *******' >> sftplog.txt");//put seperator in log file
//exec("./sftpcorradodev.sh >> sftplog.txt");//run script and put in log
exec("./sftp_reporting.sh STL-20090908* >> sftplog.txt");//run script and put in log
//exec("./scp_reporting.sh STL-20090908* >> sftplog.txt");//run script and put in log
exec("echo '************************************************* *******' >> sftplog.txt");//put seperator in log file
?>





So I have been working on this a while and I am getting there. It seems my issue is with running a php script using exec calling a shell script in cron.

It works if I run the shell script by it self.
./sftp_reporting.sh STL-20090908*

./sftp_reporting.sh STL-20090908*
spawn sftp sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com
Connecting to reports.sandbox.paypal.com...
sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com's password:
sftp> get /ppreports/outgoing/STL-20090908*
Fetching /ppreports/outgoing/STL-20090908.01.001.CSV to STL-20090908.01.001.CSV
/ppreports/outgoing/STL-20090908.01.001.CSV 100% 1130 1.1KB/s 00:00
sftp> !

mv STL-20090908* /home/corradod/public_html/cart3/scripts/reports/
exit
corradod@corradodev.com [~/public_html/cart3/scripts]#
corradod@corradodev.com [~/public_html/cart3/scripts]# mv STL-20090908* /home/corradod/public_html/cart3/scripts/reports/
corradod@corradodev.com [~/public_html/cart3/scripts]# exit
exit
sftp>

It works if I run the php script that runs the shell script in ssh.
php test.php

spawn sftp sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com
Connecting to reports.sandbox.paypal.com...
sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com's password:
sftp> get /ppreports/outgoing/STL-20090908*
Fetching /ppreports/outgoing/STL-20090908.01.001.CSV to STL-20090908.01.001.CSV
/ppreports/outgoing/STL-20090908.01.001.CSV 0% 0 0.0KB/s --:-- ETA/ppreports/outgoing/STL-20090908.01.001.CSV 100% 1130 1.1KB/s 00:00
sftp> !

mv STL-20090908* /home/corradod/public_html/cart3/scripts/reports/
exit
corradod@corradodev.com [~/public_html/cart3/scripts]#
corradod@corradodev.com [~/public_html/cart3/scripts]# mv STL-20090908* /home/co rradod/public_html/cart3/scripts/reports/
corradod@corradodev.com [~/public_html/cart3/scripts]# exit
exit
sftp>

Cron Job Simple in HostMonster works when I use the shell script
exec /home/corradod/public_html/cart3/scripts/sftp_reporting.sh STL-20090908*

spawn sftp sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com
Connecting to reports.sandbox.paypal.com...
sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com's password:
sftp> get /ppreports/outgoing/STL-20090908*
Fetching /ppreports/outgoing/STL-20090908.01.001.CSV to STL-20090908.01.001.CSV

/ppreports/outgoing/STL-20090908.01.001.CSV 0% 0 0.0KB/s --:-- ETA
/ppreports/outgoing/STL-20090908.01.001.CSV 100% 1130 1.1KB/s 00:00
sftp> !

mv STL-20090908* /home/corradod/public_html/cart3/scripts/reports/
exit
sh-3.2$
sh-3.2$ mv STL-20090908* /home/corradod/public_html/cart3/scripts/reports/
sh-3.2$ exit
exit
sftp>

cron doesnt work when I use php and exec call to shell script



************************************************** ******
spawn sftp sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com
Connecting to reports.sandbox.paypal.com...
sftphO_dcorradocartpaypal.com@reports.sandbox.payp al.com's password:


As you can see it just timesout in the middle of the exec. I tried with different sftp servers and it still gives the same result. But if I use my hostmonster and try to sftp into that it works fine. So that leads me to believe its related to a timeout in the exec.


I just cant figure it out. I tried a whole bunch of things. Such as adding php.ini max_input_time. I also tried adding set_time_limit

Anyone have any further input. I would appreciate it whatever you have. It would be awesome if someone could try this on their hostmonster account and see what server settings I need to adjust or whatever. I appreciate any help. Thanks.

corradodev
09-18-2009, 12:32 AM
So I have something working and I figured I would share. I found a script on the exec php guide. What it does is exec the script but then it loops until the exec is done. I am sure theres a better way to do it but below is what I got:


<?php
exec("echo '\n*********************************************** *********' >> sftplog.txt");//put seperator in log file
//exec("./sftpcorradodev.sh >> sftplog.txt");//run script and put in log
exec("date +'%D %T' >> sftplog.txt");//put date in log file


$finished=PsExecute("./sftp_reporting.sh STL-20090908* >> sftplog.txt");//run script and put in log
//exec("./scp_reporting.sh STL-20090908* >> sftplog.txt");//run script and put in log
if($finished = true )
{
exec("echo '\n*********************************************** *********' >> sftplog.txt");//put seperator in log file
}

function PsExecute($command, $sleep = 5) //exec function and keep open
{
// First, execute the process, get the process ID

$pid = PsExec($command);

if( $pid === false )
return false;

// Second, loop for $timeout seconds checking if process is running
while( 1 ) {
sleep($sleep);
// If process is no longer running, return true;
if( !PsExists($pid) )
return true; // Process must have exited, success!
}
}

function PsExec($commandJob) //exec process
{

$command = $commandJob.' & echo $!';
exec($command ,$op);
$pid = (int)$op[0];

if($pid!="") return $pid;

return false;
}

function PsExists($pid) //if process exists
{

exec("ps ax | grep $pid 2>&1", $output);

while( list(,$row) = each($output) ) {

$row_array = explode(" ", $row);
$check_pid = $row_array[0];

if($pid == $check_pid) {
return true;
}

}

return false;
}
?>

The shell script is the same.