PHP Cron Job Error - php

I want to execute a cron job, but I recieved an e-mail that tells me:
No input file specified.
And I run the following cron command every day at 15:00
/usr/bin/php -q /home/popasur/public_html/analytics/savedata_script.php?paramz=savesmdata
If I remove the "?" I recieve an e-mail with the output, but I also recieve this warning:
Notice: Undefined offset: 1 in /home/popasur/public_html/analytics/savedata_script.php on line 15
$arguments = array();
if (is_array($argv) && !empty($argv)) {
foreach ($argv as $a) {
$a_explode = explode("=", $a);
$arguments[$a_explode[0]] = $a_explode[1]; //line 15
}
}

If you are using $argv, you should run your script like this:
php myScript.php arg1 arg2 arg3 ...
In this situation, you have:
$argv[0] // myScript.php
$argv[1] // arg1
$argv[2] // arg2
and so on.

Thats not usually how you pass parameters on the command line. The reason your script isnt running is that the shell is trying to find a file called /home/popasur/public_html/analytics/savedata_script.php?paramz=savesmdata
and what I think you want is :
/home/popasur/public_html/analytics/savedata_script.php paramz=savesmdata
Then perform whatever exploding you want on $argv[1]
(or use something like getopts to do it properly)

Related

"ps -ef" returns different result when run within php script versus command line

In my test.php script I have this:
$out = exec ( 'ps -ef' );
echo $out;
Which outputs just this when I run "php test.php":
root 16682 2 0 Jan30 ? 00:00:00 [NFSv4 callback]
However, when I run "ps -ef" from the command line I get the usual long list of processes for all users..
Any ideas why the php script produces such different results?
Please try
shell_exec ( 'ps -ef' );
This will return the entire output, whereas exec returns the last line of output:
http://php.net/manual/en/function.exec.php

How to execute two CMD queries in PHP simultaneously

$command1 = "interfacename -S ipaddress -N nms -P company ";
$command2 = "list search clientclass hardwareaddress Mac address ";
if ( exec( $command1 . "&&" . $command2 ) ) {
echo "successfuly executed";
} else {
echo "Not successfuly executed";
}
If command 1 (cmd query) successfully executed, I want command 2 (which also contains some cmd queries) to be executed next. In the above script, only command 1 is executed. It doesn’t show any result for command 2.
I have wasted two days on this without finding any solution.
You can use either a ; or a && to separate the comands. The ; runs both commands unconditionally. If the first one fails, the second one still runs. Using && makes the second command depend on the first. If the first command fails, the second will NOT run. Reference
You can use shell_exec() PHP function to run Shell Command directly in your script.
Syntax : string shell_exec (string $cmd)
Example :
$output = shell_exec('ls -lart');
var_dump($output); #Showing the outputs
You can use multiple conditions in a single command line.
Example :
$data = "rm a.txt && echo \"Deleted\"";
$output = exec($data);
var_dump($output);
if($output=="Deleted"){
#Successful
}
In above example "Deleted" string will assign to $output when the file deleted successfully. Otherwise the error/warning/empty string will assign to $output variable. You should make condition with $output string.
Here is the documentation of shell_exec()
Note : There will be a new line character of the function shell_exec() output.
If I understand your question correctly, you want to execute $command1 and then execute $command2 only if $command1 succeeds.
The way you tried, by joining the commands with && is the correct way in a shell script (and it works even with the PHP function exec()). But, because your script is written in PHP, let's do it in the PHP way (in fact, it's the same way but we let PHP do the logical AND operation).
Use the PHP function exec() to run each command and pass three arguments to it. The second argument ($output, passed by reference) is an array variable. exec() appends to it the output of the command. The third argument ($return_var, also passed by reference) is a variable that is set by exec() with the exit code of the executed command.
The convention on Linux/Unix programs is to return 0 exit code for success and a (one byte) positive value (1..255) for errors. Also, the && operator on the Linux shell knows that 0 is success and a non-zero value is an error.
Now, the PHP code:
$command1 = "ipcli -S 192.168.4.2 -N nms -P nmsworldcall ";
$command2 = "list search clientclassentry hardwareaddress 00:0E:09:00:00:01";
// Run the first command
$out1 = array();
$code1 = 0;
exec($command1, $out1, $code1);
// Run the second command only if the first command succeeded
$out2 = array();
$code2 = 0;
if ($code1 == 0) {
exec($command2, $out2, $code2);
}
// Output the outcome
if ($code1 == 0) {
if ($code2 == 0) {
echo("Both commands succeeded.\n");
} else {
echo("The first command succeeded, the second command failed.\n");
}
} else {
echo("The first command failed, the second command was skipped.\n");
}
After the code ends, $code1 and $code2 contain the exit codes of the two commands; if $code1 is not zero then the first command failed and $code2 is zero but the second command was not executed.
$out1 and $out2 are arrays that contain the output of the two commands, split on lines.
I'm not sure to know about simultaneous execution but I'm sure about one cmd dependent on another cmd execution action. Here I'm running single execution cmd first to clear all set path, second I've declared my file path, third I install angular cmd npm install.
$path = "D:/xampp/htdocs/tests/omni-files-upload/aa-test/src";
$command_one = "cd /";
$command_two = "cd ".$path;
$command_three = "npm install";
#exec($command_one."&& ".$command_two."&& ".$command_three);

GetOpt doesn't read full URL

I have a script that need to run from a terminal or a command prompt. I'm using PHP. GetOpt is the function that I use to get data or a parameter that the user input in the terminal.
This is my script.
<?php
$opt = getopt("f:");
$input = $opt['f'];
$u = fopen($input, 'r');
echo "\n\n$input\n\n";
I tried to run it like this:
$ php myscript.php -f http://myurl.com/file.csv?city=london&status=3
My url is http://myurl.com/file.csv?city=london&status=3, but it only outputs http://myurl.com/file.csv?city=london. The status parameter is lost from the full URL.
How can I get this to work?
it's because you have to wrap your link around into quotes:
$ php myscript.php -f "http://myurl.com/file.csv?city=london&status=3"
I'll go ahead and assume you are running your script in Bash, and & in Bash might be interpreted as bitwise AND in your case:
$ echo $(( 98 & 7 ))
2

PHP command-line script - program usage text

I have a PHP script that takes to command line arguments. I want the user to type the name of the program with no arguments:
$ ./foo.php
and I want it to output something like:
usage: $ ./foo arg1 arg2 where arg1 is something and arg2 is something else
Is there a standard way of doing this?
Many thanks :).
Just output it.
if (count($_SERVER['argv']) <= 1) {
echo 'Usage: $ ' . $_SERVER['argv'][0] . ' arg1 arg2 where arg1 is something and arg2 is something else' . PHP_EOL;
}
$_SERVER['argv'] contains all arguments from the command line and especially the first item is always the scriptname.
See "reserved variables: $argv" and "reserved variables: $_SERVER". Note, that $argv is no available in any case, thus I recommend using $_SERVER['argv']
You have to use ARGV to get these values from command line input.
http://jm2.php.net/manual/en/reserved.variables.argv.php

how to send multiple commands to a file in cron?

I have a file that is having some multiple dynamic parameters.I want to send these parameters at the time of writing a file in main cron file. Something like this ->
*/15 * * * * /usr/bin/php /a/b/c.php parameter1 parameter2 parameter3 parameter4
Now i tried working this up but my file is not executing.
What im concerned about is that how will my php file will fetch these parameters ??
And how will i write this command when there is only 2 parameters to be passes parameter1 and parameter4???
and how will my cron and php will recognoze that which parameter is for which data and all??
please advice!!
*/15 * * * * "/usr/bin/php /a/b/c.php parameter1 parameter2 parameter3 parameter4"
maybe...
also you can pass it as a query string? p1=123&p2=432&p3=456
within the script you can just do a parse_string to convert it into an array...
In this case it will be $argv array in your script with those parameters.
Also you can make an executable file with contents:
#!/usr/bin/php
<?
require('yourscript.php');
And make it executable with chmod +x thisfile. Just not to pass your file name to php interpreter in crontab.
The way you need to go about making this script work is the following:
In the cron script set the parameters to each be query strings. For example, set p1=1234 as parameter1, etc.
Within the PHP script, you will need to reference the $argv array to get the parameters passed in.
$argv[1] will be parameter1, $argv[2] is parameter2, and so forth.
You can identify which parameter is which by using the explode command to separate the key from the value:
for($x=1; $x < $argc; $x++) {
list($key, $value) = explode('=', $argv[1]);
$paramarray[$key] = $value;
}

Categories