I'm trying to execute a PHP script on Windows through php.exe, while passing parameters to the script. Everywhere I look, it says it should work like this:
php -f "path\to\my\script.php" -- -t 10 -i 5
The -t 10 -i 5 should be passed to script.php, where I can access them through $argv. When I type this in on the command line, everything runs as expected. When I paste the very same line in a .cmd file, the part after script.php gets treated as a seperate command. (and yes, it is a single line in the batch file)
C:\>php -f "path\to\my\script.php" -- -t 10 -i 5
<<<output of the php script as expected>>>
C:\>mybatch.cmd
C:\>php -f "path\to\my\script.php"
<<<output of the php script not receiving the parameters>>>
C:\>-- -t 10 -i 5
'--' is not recognized as an internal or external command, operable program or batch file.
I first thought it might be a problem with the --, but even if I leave out the -- (basically passing the other parameters to php.exe instead of to the script), the same problem occurs.
Any ideas on why this is happening?
Found the problem, appearantly there was a linefeed after the filename of the script, but that was not visible in notepad. No idea how it got there (probably copy/paste related), but removing it fixed the problem.
Related
Prokka is a tool used to annotate bacterial genomes and can be installed and access using the command line in the local system or server.
So, I have downloaded Prokka using Git clone (git clone https://github.com/tseemann/prokka.git) and complied it on the CentOS (7) server along with all the dependencies with the latest version (NCBI-BLAST 2.10.1, hmmer 3.3, gnu Parallel).
In order to make a prediction server, I have created a webpage using PHP, and in that PHP file, I'm taking input from the user and passing that input to a shell file, and trying to execute that shell file using the shell_exec() function.
Here $fileName and $fn are the input from taken from the webpage.
$output = shell_exec("./path/to/shell/file/invoke.sh $script_path/$fileName $fn");
echo "<pre>$output</pre>";
invoke.sh file:
input_genome="$1";
prefix_genome="$2";
/path/to/prokka/bin/prokka $input_genome --outdir ./results/"$prefix_genome" --prefix $prefix_genome --locustag $prefix_genome --cpus 8 --kingdom Bacteria
Now, if I'm invoking Prokka (/path/to/prokka/bin/prokka) directly from the command line it's working fine and create all the files which ideally it should create. Moreover, if I'm running the shell script (invoke.sh) to invoke the Prokka (/path/to/prokka/bin/prokka) it's again working fine and generate all the files.
Expected and actual output files:
seq.faa
seq.err
seq.fna
seq.txt
seq.ffn
seq.gff
seq.tsv
seq.fsa
seq.gbk
seq.tbl
seq.log
seq.sqn
But the problem is while I'm running the PHP file from the web-browser as mentioned above to run the shell file (invoke.sh) it is running, but the Prokka command is not running properly inside the shell file and therefore not generating all of the output files.
I checked the error_log it showed me this:
[23:43:57] Will use blast to search against /home/group01/html/csspred/prokka/db/kingdom/Bacteria/sprot with 8 CPUs
[23:43:57] Running: cat \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.faa | parallel --gnu --plain -j 8 --block 3279 --recstart '>' --pipe blastp -query - -db /home/group01/html/csspred/prokka/db/kingdom/Bacteria/sprot -evalue 1e-09 -qcov_hsp_perc 80 -num_threads 1 -num_descriptions 1 -num_alignments 1 -seg no > \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.blast 2> /dev/null
[23:44:00] Could not run command: cat \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.faa | parallel --gnu --plain -j 8 --block 3279 --recstart '>' --pipe blastp -query - -db /home/group01/html/csspred/prokka/db/kingdom/Bacteria/sprot -evalue 1e-09 -qcov_hsp_perc 80 -num_threads 1 -num_descriptions 1 -num_alignments 1 -seg no > \/home\/group01\/html\/csspred\/results\/01\-Nov\-23_43_779958414\/seq\/seq\.sprot\.tmp\.18401\.blast 2> /dev/null
and generated only four files :
seq.HAMAP.hmm.tmp.12617.faa
seq.HAMAP.hmm.tmp.12617.hmmer3
seq.fna
seq.log
I even used a C file to invoke the execute a shell file which ultimately invokes the Prokka command but again the problem remained the same.
PHP: $output = shell_exec("./invoke.out $script_path/$fileName $fn");
C:
`int main(int argc, char *argv[])
{
char command[1000];
sprintf(command, "%s %s %s", "sh /home/group01/html/csspred/scripts/CSS_pred_new_additions/prokka_test.sh", argv[1], argv[2]);
system(command);
}`
What is the problem and how to solve this? I just wanted to execute my invoke.sh or invoke.out so that at the backend my Prokka command will be able to run correctly and gives me all expected output files.
I have a PHP script that executes an external bash script to make an SSH connection but even though i am using ssh's move to background (-f) as well as an '&' my PHP script hangs.
Problem line in PHP script
system('/usr/local/bin/startProxy 172.16.0.5 9051');
I have also tried :
system('/usr/local/bin/startProxy 172.16.0.5 9051 &');
And the startProxy script is simply :
#!/bin/bash
#
# startProxy <IP_Address> <Proxy_Port>
#
# Starts an ssh proxy connection using -D <port> to remote system
#
ssh -o ConnectTimeout=5 -f -N -D $2 $1 &
Calling the startProxy script from command line works find and the script returns immediately.
When the system() command is run, the called script does run (I can see ssh running via ps), it just never appears to return to the PHP script.
I have run into the same issue when trying to call ssh directly via the system() method as well.
Thanks #Martin,
For future self and others, I had to change the system call to
system('/usr/local/bin/startProxy 172.16.0.5 9051 2>&1 >/dev/null');
and to change the startProxy script to :
ssh -o ConnectTimeout=5 -f -N -D $2 $1 2>&1 >/dev/null
before PHP return to the rest of the script!
Anyone able to explain why PHP stops like this if output is not redirected (I presume PHP isn't scanning the command and seeing the redirection part) and also why PHP hangs is i don't include the redirection in the ssh command within startProxy, dispite the fact that both the PHP system call and the ssh command in startProxy where using background options (-f and '&' )
I'm trying to use PHP to trigger a bash script that should never stop running. It's not just that the command needs to run and I don't need to wait for output, it needs to continue running after PHP is finished. This has worked other times (and the question has been asked already), the difference seems to be my bash script has a trap for when it's closed.
Here is my bash script:
#!/bin/bash
set -e
WAIT=5
FILE_LOCK="$1"
echo "Daemon started (PID $$)..."
echo "$$" > "$FILE_LOCK"
trap cleanup 0 1 2 3 6 15
cleanup()
{
echo "Caught signal..."
rm -rf "$FILE_LOCK"
exit 1
}
while true; do
# do things
sleep "$WAIT"
done
And here is my PHP:
$command = '/path/to/script.sh /tmp/script.lock >> /tmp/script.log 2>&1 &';
$lastLine = exec($command, $output, $returnVal);
I see the script run, the lock file get created, then it exits, and the trap removes the lock file. In my /tmp/script.log I see:
Daemon started (PID 55963)...
Caught signal...
What's odd is that this only happens when running the PHP via Apache. From command line it keeps running as expected.
The signal on the trap that's being caught is 0.
I've tried wrapping my command in a bash environment, like $command = '/bin/bash -c "' . addslashes($command) . '"';, also tried adding nohup to the beginning. Nothing seems to be working. Is this possible to do for a never ending script?
Found the problem thanks to #lxg.
My # do things command was giving errors, which was causing the script to exit. For some reason they were suppressed.
When removing set -e from the beginning of my bash script I started seeing the errors output to my log file. Not sure why they didn't show up before.
The issue was in my bash loop it was running PHP commands. Even though my bash user and Apache user are the same, for some reason they had different $PATHs. This meant that when running on command line I was using a PHP7 binary, but when Apache trigged bash commands it was using a PHP5 binary (even though Apache itself is configured to use PHP7). So the application errored out and that is what caused the script to die.
The solution was to explicitly set the PHP binary path in my bash loop.
I was doing this with
BIN_PHP=$(which php)
But on true command line it would return one value (/path/to/php7/bin/php) vs command line initiated by Apache (/path/to/php5/bin/php). Despite Apache being the same as a my command line user, it didn't load the ~/.bashrc which specified my correct PHP path.
I am using PHP built in server for testing and I was wondering is there a way you can hide cmd window when launching built in server using command php -S 127.0.0.1:809 -t Folder
I am currently working on Windows 10 so I need a Win solution.
not hundred percent sure on this, but you might try this one from:
What is cmd's equivalent to Bash's & (ampersand) for running a command without waiting for it to terminate?
so yours could be something like:
start /B php -S 127.0.0.1:809 -t Folder
You can create vbs script (run.vbs) and you can put this code in it
Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /C CD resource\php && php -S 127.0.0.1:809 -t HTML", 0
Set oShell = Nothing
0 in line signal for not displaying command line window.
tail -n 1 -f /tmp/remoteinput | php ./myscript.php conf.conf
I run the above command to have myscript accept redirected input. This bit is working.
The issue I am facing is when myscript.php finishes execution it exit's however the
pipe is still left open. I think tail is still lingering.
What I want to archive is when myscript.php exits to have the whole pipe killed
Use tail without -f parameter. -f makes it continuing to listen, so command never dies and pipe is never closed.
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent