I have a script (file1.php)in php in linux
#!/usr/bin/php -q
<?php
echo "hello world" ?>
when i run in linux (redhat # bash shell)
> php file1.php
it works. my php is in /usr/bin/php and its version is 5.3.3)
but when i run
./file1.php
it says
'./file1.php' not present.
my application requires this ('./file1.php') model to work
on my other machine this file works with ('./file1.php') model
why is it so , is there any way to fix this ..
**/usr/bin/php -v works well
file permission (file1.php): -rwxr-xr-x 1 root root**
You should indicate how your file should be executed. In bash, it's done using a shebang
Try adding this line add the very top of your php script:
#!/usr/bin/php
<?php echo 'Hello world!';
This would tell bash to run ./file.php as php /fullpath/file.php
More info http://en.wikipedia.org/wiki/Shebang_(Unix)
Related
I'm working in a shared hosting environment that came with Python 2.6 as a default. I used SSH to install Python 3.4, as well.
When I use the alias python3 to execute the script, it runs perfectly.
But in the browser, where I use PHP to call the Python script, it returns the error: python3 command not found. If I use only the command python, it uses the 2.6 version, and the script doesn't work.
I can't restart the server because it's shared.
Is there a way to work around this?
PHP SCRIPT THAT CALL PYTHON3 SCRIPT:
<?php
$command = "python3 proccess.py 2>&1";
?>
You can change the command to:
<?php
$command = "~/.bashrc && python3 process.py 2>&1";
?>
Given ~/.bashrc is the file containing alias', or use the corresponding alias file. This way, you shall be able to use all the aliases
The issue is that when you run in PHP through webserver the PATH variable is different. So there are two ways to solve it
Option 1
Get the path from shell using echo $PATH and use the same in script
<?php
$command = "PATH=/a:/b/c python3 proccess.py 2>&1";
?>
Option 2
Use the absolute path for python3. Run which python3 and the path you get in that use that in your script
<?php
$command = "/usr/bin/python3 proccess.py 2>&1";
?>
You are using a bash alias in your script. That isn't going to be accessible to your web app in a non-interactive shell. And you don't want to do that anyway.
Create a symlink which will be available to anyone, anywhere:
ln -s /usr/bin/python3.4 /usr/bin/python3
EDIT:
Of course you can also just specify the full path in your PHP command:
<?php
$command = "/path/to/your/python3.4 proccess.py 2>&1";
?>
I'm running a Half-Life dedicated server on my VPS with following bash file named startserver in HLDS:
#!/bin/bash
screen -A -m -d -S hlds ./hlds_run -console -game valve -ip **ip address** -port 27015 +maxplayers 16 +map crossfire > /dev/null >&1 &
So I type ./startserver and my server is running. I'm not owner of this code, I just copied it. And I tried to make this code work for a PHP file and I created a directory named "test" and I have "test.php" code so that I can run my php file by typing "php test.php" but I need to run it on a screen too, because I'm not always gonna run the putty client.
I tried to edit the code in the directory where my php file exists and that's what I got inside:
#!/bin/bash screen -AmdS phper php test.php > test.txt >&1 &
I supposed to create a screen named "phper" and run the test.php inside. All files exist in the directory but I face with that error:
/bin/bash: screen -AmdS phper php test.php > test.txt >&1 &: No such file or directory
Am I doing something wrong?
The first line is wrong, if it is one line
#!/bin/bash screen -AmdS phper php test.php > test.txt >&1 &
First line of text file with executable bit set may begin with "#!" to set interpreter of the file, it is called Shebang. When you run the script and there is shebang, linux kernel will recognize it and start interpreter with the name of script as its argument.
You should not add anything after "/bin/bash", or it will try to parse it as his own parameters, and there is no bash parameter "screen" and no file with name "screen" (and with bash script inside) in current directory.
So, try put bash in shebang on first line and actual bash script from second line:
#!/bin/bash
screen -AmdS phper php test.php > test.txt >&1 &
I have 500 php files i want to make a crontab to run them automatically (they store XML data in my database) my problem is :
when I make a crontab for each php file it works fine. the command like this:
* * * * * php /home/username/public_html/codes/php0.php
But when I want to run a shell script including all my php files like this :
* * * * * bash /home/username/public_html/codes/php.sh
it does not run.
php.sh:
#!/bin/sh
php php0.php
echo php0
php php1.php
echo php1
php php2.php
echo php2
.
.
.
Is it possible to wrap php files with bash script? and if yes why does not work am I miss something?
You are probably not running in the directory you believe you are.
So replace temporarily #!/bin/sh with #!/bin/sh -vx
and add a
pwd
at the beginning of your script (that is, the 2nd line)
Then perhaps add a
cd /home/username/public_html/codes
or maybe define a variable and use it:
mycodedir=/home/username/public_html/codes
php $mycodedir/php0.php
echo php0
etc...
I suggest to read the advanced bash scripting guide (even if it does have weaknesses).
At the moment, you have relative paths in your php.sh. The following line
php php0.php
means that php will look for the php0.php file in the current working directory. In a cron job, the working directory is usually /, i.e. the file system root. To check what the working directory really is, you can use the pwd command.
So your php.sh script looks for a file php0.php in your file system root, but the file is in /home/username/public_html/codes/.
If you use absolute paths in your php.sh, the current working directory does not matter. So simply add absolute paths in your php.sh. The file will look like this:
#!/bin/sh
php /home/username/public_html/codes/php0.php
echo php0
php /home/username/public_html/codes/php1.php
echo php1
php /home/username/public_html/codes/php2.php
echo php2
.
.
.
I have a cron job set like
php /home/novinarb/public_html/index.php --uri="cron/24satahr"
but the 'uri' param doesn't get to the php script at all. I also tried without the -- in front of uri but still nothing. Any ideas?
A more robust method would be to accept command-line arguments in your PHP script with getopt() or $argv and making it executable. An example with $argv called script.php:
#!/usr/bin/php
<?php
if (isset($argv[1])):
echo $argv[1];
endif;
?>
Make it executable:
chmod +x script.php
And execute:
./script.php "cron/24satahr"
Will output:
cron/24satahr
I was facing the same problem but was able to fix it after reading the manual entry for php.
Initially I had something set like:
/usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1
I was able to fix it by changing the line to:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
As per the manual entry the syntax is:
php [options] [ -f ] file [[--] args...]
Also,
args... Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin
Going by that, my cron command becomes:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
and it works!
Is the php script running at all?
I suspect you need to provide the full path to php in your crontab line. Even though cron jobs run as you, they don't have any of your login environment set up; this means they don't have your $PATH.
I have a php script in my website folder that needs to be executed weekly.
I am on debian 6 (root).
How can I run this php script (in cli) weekly wihtout using crontab ?
I mean which kind of file should I copy in /etc/cron.weekly to run my php file?
Just use an executable PHP script with a "shebang" line:
#!/usr/bin/php
<?php
// Your PHP code goes here
echo "Hello World!";
?>
Make it executable:
$ chmod +x myscript.php
Test it:
$ ./myscript.php
Hello World!
The "shebang" (#!) tells the shell that this script is to be executed with PHP, found in /usr/bin/php.
Alternatively, you could write a small shell script that invokes the PHP interpreter with your PHP script:
#!/bin/bash
/usr/bin/php -f /path/to/script.php