Bash script to run php script - php

I have a php script that I want to be run using a bash script, so I can use Cron to run the php script every minute or so.
As far as I'm aware I need to create the bash script to handle the php script which will then allow me to use the Cron tool/timer.
So far I was told I need to put:
#!/pathtoscript/testphp.php
at the start of my php script. Im not sure what to do from here...
Any advice? Thanks.

If you have PHP installed as a command line tool (try issuing php to the terminal and see if it works), your shebang (#!) line needs to look like this:
#!/usr/bin/php
Put that at the top of your script, make it executable (chmod +x myscript.php), and make a Cron job to execute that script (same way you'd execute a bash script).
You can also use php myscript.php.

Sometimes PHP is placed in non standard location so it's probably better first locate it and then try to execute.
#!/usr/bin/env bash
PHP=`which php`
$PHP /path/to/php/file.php

A previous poster said..
If you have PHP installed as a command line tool… your shebang (#!) line needs to look like this: #!/usr/bin/php
While this could be true… just because you can type in php does NOT necessarily mean that's where php is going to be... /usr/bin/php is A common location… but as with any shebang… it needs to be tailored to YOUR env.
a quick way to find out WHERE YOUR particular executable is located on your $PATH, try..
➜which -a php ENTER, which for me looks like..
php is /usr/local/php5/bin/php
php is /usr/bin/php
php is /usr/local/bin/php
php is /Library/WebServer/CGI-Executables/php
The first one is the default i'd get if I just typed in php at a command prompt… but I can use any of them in a shebang, or directly… You can also combine the executable name with env, as is often seen, but I don't really know much about / trust that. XOXO.

You just need to set :
/usr/bin/php path_to_your_php_file
in your crontab.

I'm pretty sure something like this is what you are looking for:
#!/bin/sh
php /pathToScript/script.php
Save that with your desired script name (such as runPHP.sh) and give it execution rights, then you can use it however you want.
Edit: You might as well not use a bash script at all and just add the "php ..." command to the crontab, if I'm not mistaken.
Good luck!

The bash script should be something like this:
#!/bin/bash
/usr/bin/php /path/to/php/file.php
You need the php executable (usually found in /usr/bin) and the path of the php script to be ran. Now you only have to put this bash script on crontab and you're done!

a quick way to find out WHERE YOUR particular executable is located on your $PATH, try.
Even quicker way to find out where php is ...
whereis php
I'm running debian and above command showing me
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz
Hope that helps.

If you don't do anything in your bash script than run the php one, you could simply run the php script from cron with a command like /usr/bin/php /path/to/your/file.php.

I found php-cgi on my server. And its on environment path so I was able to run from anywhere. I executed succesfuly file.php in my bash script.
#!/bin/bash
php-cgi ../path/file.php
And the script returned this after php script was executed:
X-Powered-By: PHP/7.1.1
Content-type: text/html; charset=UTF-8
done!
By the way, check first if it works by checking the version issuing the command php-cgi -v

Create file.php with first line in files: file.php(#!/bin/php) file.sh(#!/bin/bash).
Check installed php.Run command in terminal:
which php
If set there will be an answer:
/usr/bin/php
Run file.php with command:
php file.php
if the file has started then you can write this command to file.sh:
#!/bin/bash
run_php=`php file.php`
echo $run_php
Be careful ' and ` different!!!

Related

Link php interpreter from Docker to bash somehow

I'm trying to use my docker php as an interpreter in my terminal. What I need:
I don't keep my dev envs on my host os. That's important to keep it isolated
It should be available as $php or $/usr/bin/env php
I'd like to be able to run something like phpcs in my vim. It requires that thingy above.
I've tried this:
alias php='docker-compose exec php php'
But it's not available through /usr/bin/env
/usr/bin/env tries to locate the executable via the $PATH variable, see https://unix.stackexchange.com/a/12749
So if you put your command in to a script called PHP and add it to your $PATH before other PHP executables, it might work.

PHP shell_exec which mysqld refuses to work

I attempt to run the command echo shell_exec('which mysqld'); in PHP which yields nothing.
I manually go to the terminal and run $ which mysqld which yields the answer.
I run the command echo shell_exec('whoami'); in PHP which gives me user.
I go back to the terminal and run $ which mysqld as the user PHP is running as and get the answer.
How do I force PHP to return echo shell_exec('which mysqld'); whether it "feels" like it or not?
Yes, the database is running.
It sounds like php doesn't know what the executable is, it's possible it doesnt know what the command is (it might not be in your $PATH), or it's a shell builtin, you may be better off running a command with bash -c "which mysql", or something of that nature, to force your script to run it in the context of an actual shell session.

PHP exec on local script

Hello i have a PHP script, and its added to cron, it is possible to execute from this script shell command (with exec() or something) without enabling it on php.ini? I don't want to enable exec on my site
It's called PHP CLI, check here
Usually when you install php, there's option to install php_cli too.
So long you can run php on shell prompt, then it can work.
Open bash (or other shell), try this:
php -v
If the version printed, then it's working.
Then you can
php -f phpfile
or put
#!/usr/bin/php
At the beginning of your php file as a line, and chmod +x file.php, and then
./file.php
#or
/path/to/file.php
to run it.
(Note /usr/bin/php is the usual place of php executable, it might change, eg in unix is ually /bin/php. Use whereis php to check its place.)

Memecached not found when using exec() in php

I'm using CentOS, and I am able to execute php via CLI from the terminal, but when I run the exact same command via exec(), and it appears that the Memcached class doesn't exist.
so this works:
~$ php script.php args
But this doesn't:
exec('php script.php args');
Anyone have any ideas why it behaves differently in these cases?
PS: I've tried specifying the ini file like this:
exec('php -c /etc/php.ini script.php args');
This was fixed by reinstalling memecache... I'm not sure why or how that worked.

Why wget backticks shell command works in PHP CLI and not in a PHP script?

just for fun I am trying to make wget downloads from php cli (/usr/bin/php -a) and it works:
php > `wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
php > // a HTML file is downloaded and its content is placed into /Users/user/path/to/file.html
However, when I try to do the same thing from a PHP script, it does not work:
<?php
`wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
// After requesting the script from the browser and after the script is executed, the file doesn't exist on the specified path
I would like to say that the user which executes apache and therefore PHP server side scripting is the same as the user which executes the php command from the command line (so I guess this should not be a problem of permissions).
Why if I use a .php script and call wget inside the script the file is not saved?
Thanks for the attention!
PS: please, do not tell me that I can use curl for such a task, I know I can, I am just curious to know how can I do something similar without using PHP tools, that's it
use the full path to wget, since the daemon doesn't run .bash_profile.
`/opt/local/bin/wget -q -nd -O /Users/user/path/to/file.html http://host.com/some_file.html`;
BTW, there's no need to escape / in shell commands.
The backtick operator runs a shell command and returns the output from the shell command.
In this situation, simply logging (or if you have to, echo-ing) the result will probably reveal the error. E.g:
$result = `wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
trigger_error($result, E_USER_NOTICE);

Categories