php script not executed from command line - linux

I'm unable to execute php scripts from command line. I have the following simple code in a file named test.php:
#!/usr/bin/php
print password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n";
On executing, I get this:
#./test.php
print password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n";
About my environment, it's a Debian 7 x64 server.
#which -a php
/usr/bin/php
#`which php` -v
PHP 5.6.9-0+deb8u1 (cli) (built: Jun 5 2015 11:03:27)
What could be wrong with my installation?

You need to encapsulate your PHP code with the proper tags else it will be interpreted as plain text. Modify your PHP file to the following:
<?php
print password_hash("rasmuslerdorf",PASSWORD_DEFAULT)."\n";
?>

Related

Gearman: troubles with first use

I tried to start with Gearman. After downloading and setting it, gearman_version() works. But, when I start server and try to init worker like so:
php myFileName.php &
I see the code:
And when I init the client, I see code too. What am I doing wrong?
i don`t know why, but the examples from youtube was not correct in my case. The first scripts, which worked I get from http://php.net/manual/ru/gearman.examples-reverse-bg.php
You likely have short open tags disabled on your install. Notice, running a php file that doesn't actually contain any PHP will just echo the contents of the file.
>$ echo 'hello' > text.php
>$ php text.php
hello
>$
You can verify the setting for your install with the following
>$ php -i | grep "short_open_tag"
short_open_tag => On => On
If tags are On you're all set.

Print HTML Response with localhost cUrl - Linux

I want to execute PHP Script on my Ubuntu Virtual Machine but I only have access to the command line. I've thought about using cUrl but I have a problem with it :
When I use the following command : "curl http://localhost/myscript.php"
The response is the plain file ("<?php echo "<p>Hello world</p>"; ?>") instead of html response ("<p>Hello world</p>")
How to solve this problem ?
Thank you
You may have to set the content-type using header().
Before the echo statement, insert this:
header('Content-Type: text/html');
See: http://php.net/manual/en/function.header.php

PHP iconv error using Zend Lucene when executing script via cron, but not on commandline

I am executing a PHP script via the command line which, for a specific user, runs fine when executed on the commandline, but when the exact same command is put into the same user's crontab, a PHP iconv error is returned.
The commandline is utilising the Yii framework and the Zend Lucene library, but I'm not sure if that's pertinent.
I've made all executable and script paths absolute in the crontab line and can verify that it works when executed directly on the commandline.
I wrapped the actual PHP invocation in a one-line shell script, as I read elsewhere here that this solved a similar problem for someone, but no joy.
The command successfully executed on the commandline is:
/bin/sh /var/www/yii-projects/projectname/protected/scripts/buildIndex.sh >> /var/lucene/lucene.log
The content of the buildIndex.sh script is:
/usr/bin/php /var/www/yii-projects/projectname/protected/scripts/cron.php lucene buildIndex
And the crontab line is:
*/10 * * * * /bin/sh /var/www/yii-projects/projectname/protected/scripts/buildIndex.sh >> /var/lucene/lucene.log
The error shown in the log when the crontab executes is:
PHP Error[8]: iconv(): Detected an illegal character in input string
in file /var/www/yii-projects/projectname/protected/vendors/Zend/Search/Lucene/Analysis/Analyzer/Common/Text.php at line 58
0 /var/www/yii-projects/projectname/protected/vendors/Zend/Search/Lucene/Analysis/Analyzer/Common/Text.php(58): iconv()
1 /var/www/yii-projects/projectname/protected/vendors/Zend/Search/Lucene/Analysis/Analyzer.php(125): Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive->reset()
2 /var/www/yii-projects/projectname/protected/vendors/Zend/Search/Lucene/Index/SegmentWriter/DocumentWriter.php(98): Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive->setInput()
3 /var/www/yii-projects/projectname/protected/vendors/Zend/Search/Lucene/Index/Writer.php(244): Zend_Search_Lucene_Index_SegmentWriter_DocumentWriter->addDocument()
4 /var/www/yii-projects/projectname/protected/vendors/Zend/Search/Lucene.php(1410): Zend_Search_Lucene_Index_Writer->addDocument()
5 /var/www/yii-projects/projectname/protected/vendors/Zend/Search/Lucene/Proxy.php(500): Zend_Search_Lucene->addDocument()
6 /var/www/yii-projects/projectname/protected/commands/LuceneCommand.php(97): Zend_Search_Lucene_Proxy->addDocument()
7 unknown(0): LuceneCommand->actionBuildIndex()
8 /var/www/yii-projects/yii-1.1.12.b600af/framework/console/CConsoleCommand.php(173): ReflectionMethod->invokeArgs()
9 /var/www/yii-projects/yii-1.1.12.b600af/framework/console/CConsoleCommandRunner.php(68): LuceneCommand->run()
10 /var/www/yii-projects/yii-1.1.12.b600af/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run()
11 /var/www/yii-projects/yii-1.1.12.b600af/framework/base/CApplication.php(162): CConsoleApplication->processRequest()
12 /var/www/yii-projects/projectname/protected/scripts/cron.php(14): CConsoleApplication->run()
I cannot think of any reason why there is any difference, given the measures taken, and the fact that the user is the same in both cases.
Please help!
Thanks
Edit - I should also confirm that the underlying data that is being indexed is not changing - I've executed both scenarios alternately many times and get the above results consistently.
Try with the -f switch and directly from crontab:
/usr/bin/php -f /var/www/yii-projects/projectname/protected/scripts/cron.php lucene buildIndex
Also are you sure that the text of the command you are passing is in UTF8? Could there be some other symbol there? Maybe a BOM? You can check this with a HEX editor - open your shell script, omit all the letters and see what's left. Usually a BOM in UTF8 is EF BB BF but it may not be a bom at all. Just check.
Necessary shell environment variables are not available to crontab job, so added this to irishhp users's crontab:
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
LANG=en_US.UTF-8
which resolved.

Linux Script Unexpected end of file error

I am new to shell scripting. I was trying to write a script to which is throwing an unexpected end of file error. I have check other solutions on stackoverflow, however I coul not solve the here. Any help would be appreciated. My script is
if [ "$(/etc/init.d/tomcat7 status)" == " * Tomcat servlet engine is not running." ]; then /etc/init.d/tomcat7 start; fi
As suggested I tried creating this in linux and now I get an error [: * Tomcat servlet engine is not running.: unexpected operator
Since you created the file on Windows your original problem was most likely due to a mismatch of line-break characters. Windows encodes line-breaks as CR-LF, while Linux/Unix uses just LF and Mac OS just CR. You can fix that with e.g.
recode ibmpc..latin1 your.sh
Also you should specify the interpreter in the first line of the script:
#!/bin/bash
Your script should probably look somewhat like this:
#!/bin/bash
if [ "$(/etc/init.d/tomcat7 status)" == " * Tomcat servlet engine is not running." ]; then
/etc/init.d/tomcat7 start
fi
As you wrote the script on windows most probably the problem is the different encoding of line-end characters \r\n on windows while a single \n on linux.
Try using dos2unix on your script and run it again.
Solved this looking at [ :Unexpected operator in shell programming.
I had to run it as bash instead of sh.

Show output of a perl script in linux

I am using a web interface written in php that runs a perl script in a linux environment.It passes parameters(username,password,...) to the script . I want to view the output of the script without interfering with the process. Note that the script in his turn also passes data and excutes another program.
The script contains print commands like
if( $# ){
print "Error :".$#."\n";
print "skip...\n"; }
else{
}
I just want to view these results from the shell, also it would do it if i can save into a txt file .
thanks a lot!
Run the Perl program from the shell to see the output from print.
$ perl theprogram
⋮
Error : blah blah
skip...
⋮
Redirect STDOUT to save it into a file.
$ perl theprogram > theprogram.log
These are the very basics of shell usage, you already should know all this if you are a programmer. If not, read a Unix book for beginners.

Resources