Jump to content

Run php process in background


Ortix

Recommended Posts

Hey guys, I've hit a huge obstacle in my coding process of a script I am writing.

 

It is basically an uploading interface script and it works like this. I have files on my server and I want a user to click a button which fires a php script which in turn fires a bash script which IN TURN fires the uploading program (plowshare).

 

So to make it simple: user logs in, sees upload button. Clicks it, file starts uploading. I then want the user to see "upload initiated" and that's it. The problem is that with what I have now, the script just won't finish loading until the file has been uploaded and as a matter of fact I don't even think it is uploading anything :S

 

Here are the scripts:

 

PHP interface to call the bash script:

<?php

error_reporting(E_ALL);

function run_in_background($Command, $Priority = 0) {
if ($Priority)
$PID = shell_exec("nohup nice -n $Priority $Command 2> /dev/null & echo $!");
else
$PID = shell_exec("nohup $Command 2> /dev/null & echo $!");
return($PID);
}

function is_process_running($PID) {
exec("ps $PID", $ProcessState);
return(count($ProcessState) >= 2);
}

$file = $_GET['file'];
// Location of file relative to the index
$where = $_GET['where'];
// Absolute index location
$absolute = '/var/www/vhosts/animekyun.com/httpdocs/icarus/';
$where = $absolute . $where;
//$outfile ='log';
echo $where . $file . '<br>';

echo "<h2>The upload has been initiated!</h2>";

$ps = run_in_background("/bin/sh uploadparser.sh $file $where > $outfile");
while(is_process_running($ps))
//echo $output;
?>

 

Bash script to call uploading program:

#!/bin/sh
{
FILE=$1
WHERE=$2
FILENAME="/var/www/vhosts/animekyun.com/httpdocs/icarus/debug/debug_mu_$FILE"
FILENAME2="/var/www/vhosts/animekyun.com/httpdocs/icarus/debug/debug_multi_$FILE"
/usr/local/bin/plowup megaupload "$WHERE$FILE" >> "$FILENAME".txt 2>&1
/usr/local/bin/plowup multiupload "$WHERE$FILE" >> "$FILENAME2".txt 2>&1
} &

 

this works fine when I pass the variables as arguments in CLI

 

I hope i have been clear enough, it does seem rather confusing :P

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.