Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exec a background job

Status
Not open for further replies.

tjbradford

Technical User
Dec 14, 2007
229
GB
I'm trying to build a webpage that will take an input value and call a bash script i have.

it's to start off a certificate request and the input value is for the user who will be handed the certificate. however when running this from webpage it reports that the enrollment failed (this is almost instantly)

if i run the command as from shell it works fine, i wondered if it's because it need to call the script then leave it running in the background but putting a & at the end of the $cmd doesn't help

what can i do - im not even at beginner level in php

<?php
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$TofD = $_POST["TofD"];
$cmd="bash /etc/opt/cisco-vpnclient/Profiles/enroll .$Fname. &";
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Certificate Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
<input type="submit" value="submit" name="submit">
</form>
<?
} else {
echo "Submiting request for usename, ".$Fname." .<br />";
echo "<i>".$quote."</i><br />";
echo exec('whoami');
echo "You're favorite time is ".$TofD.", and you passed ".$education."!<br />";
echo '<pre>';
exec ($cmd);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
}
?>
 
I think your diagnosis may be wrong. My first thought is that the web server user does not have the right to execute the script. If you did not set the rights to enable this, it is a good thing that the web server user is restricted as far as possible.

You can use "sudo" to execute the script as another user.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
but if i can run it in the shell as which is what the apache server runs under then i do have the permissions no ?
 
to have a command execute in the background redirect the output to /dev/null

Code:
exec ('whoami > /dev/null &');

assuming you are using *nix.


also I am not sure that your $cmd string looks right. first off I suspect that the enroll script will already have a shebang specifying the script language. also I suspect that including the dots in an unescaped $fname is unlikely to be successful

perhaps something like this?

Code:
$cmd="/etc/opt/cisco-vpnclient/Profiles/enroll [red]" . escapeshellarg(trim($Fname)) " > /dev/null &"[/red];

obviously, if you pipe output to /dev/null then your script will never know whether the command was successful or not.

also this line
Code:
echo exec('whoami');

is unlikely ever to give output. to retrieve the output from an exec call, use the second and third arguments. The manual is your friend: (
and lastly this code snip
Code:
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

will give notices as you have not declared $last_line or $retval. It looks like you might be getting the various process control functions messed up. Read the manual but do remember that pipe to /dev/null means that you will get no return value nor any screen output.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top