PHP cronjob on Windows with parameter and pipe - php

I want to run a PHP file via Task Scheduler on Windows.
Everything runs except a parameter comes in.
This runs:
cmd /C C:\php\php.exe
"C:\cron\cronrun.php" >>
"C:\cron\log\cronrun.log" 2>&1
This is not running:
cmd /C C:\php\php.exe
"C:\cron\cronrun.php" --run >>
"C:\cron\log\cronrun.log" 2>&1
How can I run a job which includes an argument?
Thanks for help.
-lony
Sources:
Set PHP cron job on windows server 2003
http://richarddingwall.name/2009/01/26/capture-the-output-from-a-scheduled-task/

Try adding -- before the --run like this:
cmd /C C:\php\php.exe "C:\cron\cronrun.php" -- --run >> "C:\cron\log\cronrun.log" 2>&1
The clue is in the usage message:
$ php --help
Usage: php [options] [-f] <file> [--] [args...]
...
args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin

Related

Running PHP Script with Windows Task Scheduler

So I found some posts on here about running PHP CLI scripts in the Task Scheduler, and it works like a charm! However, when I normally run the script in command line I output the results to a .log file:
ScriptName.php -arg1 -arg2 > log.log 2>&1
This doesn't seem to work in the "Add arguments" section of the Task.
I have it where Program/script is: C:\PHP\php.exe my argument is ScriptName.php -arg1 -arg2 > log.log 2>&1
Is there something I should be doing different?
Programs run from Scheduled Tasks don't generate any standard output or standard error. You need to launch a new cmd instance:
cmd /C C:\PHP\php.exe ScriptName.php -arg1 -arg2 > log.log 2>&1
So as a workaround I will just run the script from a .bat file
PHP ScriptName.php -arg1 -arg2 > log.log
and go from there.

run php output as shell script

I want to use shell and php together.
First I tried:
shell_exec vs functions is disabled and must be so.
But php does not give me permission to run shell_exec()
So, I gave up and tried to make a .sh, call php,store output of php as sh file and run sh file.
Here is sh code
#!/bin/bash/
php test.php -> running php file/ test.php saves commands in script.sh
sh script.sh -> running the commands
rm script.sh -> removing the commands
But there must be a better way from this file process.
Can I run output of test.php directly in .sh file?
Can I run shell_exec ?
Note: I have root access of the server.
You can pipe (|) the output of PHP to sh to be executed:
$ php -r 'echo "echo \$PATH";' | sh
outputs:
/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/MAMP/Library/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I've read some statements using -r for simplicity, but you can read from a file by passing a script to php:
$ php test.php | sh

CLI php run php in command prompt

I have installed PHP CLI to execute php commands from console.
I have installed PHP CLI using this command -
sudo apt-get install php5-cli
When I run this
$vr=3; echo $vr;
Result :-
=3: command not found
If I run echo "test";
Result :- test
displays..
Can anyone tell why "command not found" displays..
The "echo "test" line is working because echo is a bash command.
You have to write your own php script, the run it by command line like this:
$ php myscript.php
In alternative, you can run php from your command line, then directly write or paste your script.
Then press CTRL+D to run it. Remember the at the beginning and at the end.
As third option, you can write a php script, putting in the first line this code:
#!/usr/bin/php
Obviously the php executable path must match the one in your system.
This way, you can chmod +x the script, then run it directly like this:
$ ./myscript.php
The fourth option is the interactive shell:
$ php -a
Interactive shell
php > echo 5+8;
13
[$ in front of commands means a command run by user]
You are entering PHP code into the Unix shell (e.g. bash). The Unix shell does not understand PHP code, so you have to run php first.
To run your PHP code from the command line:
$ php -r '$vr=3; echo $vr, "\n";'
3
To run your PHP code from the PHP interactive shell (which may or may not be compiled into PHP):
$ php -a
Interactive shell
php > $vr=3; echo $vr, "\n";
3
php >
(Hit Ctrl+D or type exit to get out of the PHP shell.)
To run your PHP code from a file named prog.php (which contains <?php before the code):
$ php prog.php
3
It seems you want something like this: http://www.php.net/manual/en/features.commandline.interactive.php
This gives you an interactive mode, where you can type PHP code and have it executed directly.

PHP execute command and log output without waiting

I use exec() to execute command, either linux or windows.
How do you execute a command, linux and windows, and log the output without waiting?
I know for linux, to not wait for the output: command* > /dev/null 2>/dev/null &
And to log output for linux: command* > /path/to/log.txt 2>/path/to/error.txt
How would you go about logging and setting it to background in one command? How would windows look like too?
On Linux you can do:
exec('command* > /dev/null 2>/dev/null &');
On Windows you can do:
pclose(popen('start /B cmd /C "command* >NUL 2>NUL"', 'r'));
Both examples disable output and errors, those go to /dev/null (linux) or NUL (windows) which means they are stored "nowhere".
You can replace these with valid paths on your system.
On Linux, a & at the end places it into background. On windows this is more complicated and needs start to invoke the process and cmd to allow redirection of the streams.

How to execute remote SSH command in background using PHP exec()?

Is there a simple way to execute SSH commands in the background on remote machines from PHP without using ssh2_*? The PHP script is executed by the user from bash (no Apache involved), so it's not an issue of rights. I've tried doing this:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i {$keyFile} {$user}#{$ip} {$remoteCommand} 2>&1 >/dev/null </dev/null");
For example:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/data/id_rsa user#192.168.0.19 '/home/user/script.sh ; exit' 2>&1 >/dev/null </dev/null");
All PHP variables have been escaped with escapeshellarg() and $remoteCommand is a bash script on the remote machine that sleeps for a few minutes and then starts executing some commands.
My problem is that if I execute that SSH command from bash, it gives control back to bash immediately. If I execute it from php using exec() it waits until the remote command executes. I've tried adding 2>&1 >/dev/null </dev/null after /home/user/script.sh, but the execution still doesn't return control to the PHP script.
I think you are missing an & at the end of your command for sending the execution to the background.

Categories