exec() command from web server in php - php

Is there anyway to use atrm command to remove queue job from PHP web application?
I wrote a shell script to remove the queue job but it doesn't work well.
#! /bin/sh
export PATH=/usr/local/bin:$PATH
echo atrm 3700 2>&1

You can try doing:
echo exec('export PATH=/usr/local/bin:$PATH ; atrm 3700 2>&1');

Try atrm 3700 2 > &1 without echo

You can use shell_exec or exec PHP function :
$output=shell_exec($commandstring);
print_r($output);
or
exec($commandstring,$output,$returnval);
print_r($output);
print_r($returnval);

Related

shell script with SFTP from PHP

I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}

Execute multiple sage commands from php script

I have installed Sage math on my ubuntu server. I can run sage commands by ssh terminal such as
$sage:
$sage: f = x^2
$sage: f.diff(x)
I would like to do on a php script
exec('sage');
exec('sage: f = 5x^3');
$fprime = exec('sage: latex(f.diff(x))');
echo $fprime;
I would expect "15x^2" as output but that's not happening .. however on ssh terminal all is good..
all help would be greatly appreciated..
I'm not completely sure of what you want to achieve. PHP's exec function executes a shell command, and that's all. Read http://fr2.php.net/manual/en/function.exec.php.
sage: f = 5*x^3 is not a shell command. You can run sage scripts directly via the command line using the -c switch :
./sage -c 'print latex((5*x^3).diff())'
will print 15 \, x^{2} to the terminal. In PHP you can grab this output by passing a second argument to exec:
exec("./sage -c 'print latex((5*x^3).diff())'", $output);
echo $output[0];

shell_exec not executing sh file for inkscape command

one.sh
#! bin/bash
command="cp 357.svg 000.svg"
echo "Executing Command";
exec $command;
from shell by executing sh one.sh runs perfact and even in php shell_exec("sh one.sh"); works fine.
two.sh
#! bin/bash
command="/usr/bin/inkscape -f 357.svg -e 357.png"
echo "Executing Command";
exec $command;
From shell sh two.sh works fine
but using php shell_exec("sh two.sh") not executing
can any one please tell why it is not executing?
try :
echo shell_exec("sh two.sh 2>&1;")
and see what the output is, maybe it will give you a permission denied error.
Maybe also worth checking which user you are running with (probably something like www-data)

running shell command from php with sudo

I want to run this code from php
echo <password> | sudo -S /usr/sbin/asterisk -rx "dongle show devices"
but it's not working. Can anyone help?
You can just use the 'backtick' character (`) around your shell string like:
<?php
$output = `command_goes_here`;
echo $output;
?>
Keep in mind this will only work if the shell_exec() function would work on that server, which could also be used in a similar way.
Use php function shell_exec or exec to execute shell commands
For more details
http://www.php.net/shell_exec
http://php.net/manual/en/function.exec.php
u can try by this if u want to run shell script command in php file
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
u can also try other way by create .sh file for shell script and run that .sh file by php function
$output = shell_exec('./deploy.sh');
echo "<pre>".$output."</pre>";

execute a linux echo command (.sh) in Php

I created a .sh file in my linux and want to run it from webpage (php + Apache).
Some of the simple example execute without problem. But I can't run with echo Pipe:
#!/bin/sh
set +v
cp /tmp/test/test1.tar.gz.gpg /tmp/test/ts2.gpg
echo 'myPassword' | /usr/bin/gpg --passphrase-fd 0 --output /tmp/test/test1.tar.gz --decrypt /var/backups/test1.tar.gz.gpg
exit 0
It can copy ts2.gpg but can't run the second command (decrypt).
You want shell_exec.
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
Enjoy

Categories