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.
Related
I'm trying to execute by php exec(); a bash file which runs a process based on curl looping script. The problem is I've tried these methods:
exec();
shell_exec();
sudo -S nice -n -20
sudo -i
> /dev/null 2>/dev/null & (achro)
and started process by PHP exec(); are slow, response from curl is milliseconds slower than if I run it manually on SSH console directly as a command.
I have it this way right now:
start.php run this:
echo shell_exec('echo "password" | sudo -S nice -n -20 ./new.sh > /dev/null 2>/dev/null &');
new.sh run this
sudo -i /home/scripts/source/do.bat > /dev/null 2>/dev/null &
and do.bat run this
cd /home/scripts/a/2
echo "" > output.txt
./check.bat & echo $! > check.pid
cd /home/scripts/a/6
echo "" > output.txt
./check.bat & echo $! > check.pid
When I execute directly ./do.bat at console, script runs pretty fast, curl responses are as I need and everything goes fine, output.txt is written with a decent output speed.
But when I try to run it with PHP exec(); curl responses are slow down, and does not work as I need. Also you will ask why I run new.sh with start.php instead runs directly do.bat with start.php? Because if I do it, I get an output response very weird with an -e wrote on text.
Somebody can help me to make it run at same speed as if I run manually on console as command?
I run a php script through shell using the following:
php script.php
How do I type this command in order to run it on the background and also log the output to a file?
I've tried
php script.php 2>&1 > out.log
But once I close putty, the script stopped.
you can use nohup (no hang up)
nohup php script.php 2>&1 > out.log
or you use cron or at to run your script in background
Add a & after the command.
Try call like this:
php script.php 2>&1 > out.log &
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.
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.
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