Launch simple php daemon on Ubuntu - php

I want launch simple php daemon on Ubuntu without fork.
source f.php:
#!/usr/bin/php
<?php
file_put_contents('/var/www/3.txt',date("H:i:s", time()) . "\n", FILE_APPEND);
while (true) {
file_put_contents('/var/www/3.txt',date("H:i:s", time()) . "\n", FILE_APPEND);
sleep(1);
}
launch:
root#ubuntu:/var/www# ./f.php &
[3] 10323
On FreeBSD this work well on Ubuntu file 3.txt is not updating :(
permissions are ok (777)

can you check that the path you are pointing to for the binary file of PHP is correct, i mean PHP is really installed in /usr/bin/php, you can check that by using below command
which php
also you can try to run it manually instead of running it as an executable by below command
php -f f.php &
and also check if your php file is giving any errors in apache logs, you can find apache logs in /var/log/apache2/error.log
and also make sure that php5-cli package is installed by executing following command
apt-get install php5-cli
Update:
according to http://www.freelance-it-consultant.com/blog/php-cli-script-running-background-ubuntu, there is some bug in ubuntu when php is run through CLI it expects some input from user, so can you try this method if that works for you?
php -q f.php < /dev/null &

Related

PHP 8.1 on Apache: shell_exec works for shell commands but not other commands

I have Lubuntu 20 running with latest Apache2 and PHP8.1.
I realized using "shell_exec" it only works for commands like "dir" or "echo" or "touch".
But i need to run a command i installed into /usr/local/bin. Calling that one simply makes shell_exec return nothing.
I am also appending "2>&1" at the end of the command, but no luck.
Folders and files are owned by the same user i am running apache with, chown and chmod is fine.
It seems it is blocking such commands, while systems commands are working from PHP.
PHP does not block any of the functions. Any idea why it is failing to run the command?
Update:
Non-working examples
echo shell_exec('/usr/local/bin/sfdx --help 2>&1');
echo shell_exec('/sfdx --help 2>&1');
echo passthru('/usr/local/bin/sfdx --help 2>&1');
Running all these on CLI on my Lubuntu works fine even with "php -r" command using the CLI mode of php works. Made a diff of apache php.ini and the cli one, only difference is my display_error and memory_limit setting.
Also tried with popen with no luck, it simply returns nothing.
$cmd = "/usr/local/bin/sfdx --help";
$handle = popen($cmd . " 2>&1", 'r');
$read = fread($handle, 2096);
pclose($handle);
Working examples
echo shell_exec('dir 2>&1');
echo shell_exec('touch test.log 2>&1');
Solution was to install "sfdx-cli" locally and not globally.
When it is installed globally (npm install -g sfdx-cli) then PHP is for whatever reason not able to run it. No errors returned even when shell_exec returns NULL, which means "error".
After installing it locally and running with "npx sfdx" everything works as expected.

How to exec a command installed with `npm` command in PHP using `exec()`?

In PHP running on Ubuntu, I can run exec('npm -v') and the output is good,
but I can't run exec('gitbook xxxx').
gitbook is a npm package I installed by
npm install gitbook -g
I can run gitbook xxxx in the Ubuntu terminal, how can I run it from my PHP code?
If you run php by nginx or apache (for example, visit url example.com/index.php), sometime you need to export the PATH
exec("export PATH=/usr/local/bin && gitbook build);
after I added export PATH, everything works fine.
I tried once like this on UNIX-based OS:
You can run shell commands via the exec() function:
// make an php file to execute shell script
exec("node yourscript.js &", $output);
Well output here become array of each line of output along with process id. You can kill process by processid also.
exec("kill " . $processid);
This how I was did. Other then this you can use node supervisor. Hope it will help you. Try your also with node command.

Execute PHP script on linux with PHP LAMPP service

So, I have that question. I've installed xampp on my ubuntu, and I run my web system from there ( /opt/lampp/htdocs ). I can open my php web systems from navigator with no troubles. So now I want to run a php script from linux console, like this:
php -f file.php
but ubuntu tells me to install php7.0-cli or hhvm packages to run a php script on console. The real question is, that I've installed lampp running php there. Is there any chance to run a php script on console with the lampp's php service? or I have to install those packages for ubuntu anyway?
When you run:
php -f file.php
from the linux terminal you are excecuting the system php enviroment (in ubuntu you install it using apt-get install), if you need run the script with the LAMPP php environment you must run:
/opt/lampp/bin/php -f file.php
If you run this command repeatedly, you can set alias by appending the following command in the "~/.bash-aliases" file or by directly running the command in the terminal:
alias php='/opt/lampp/bin/php'

Phalcon devtools not working

I'm attempting to install Phalcon Dev Tools on OSX. I have Phalcon installed and it works fine.
I've followed the instructions from here: http://docs.phalconphp.com/en/latest/reference/mactools.html
When I run the command phalcon in the terminal I get the following output:
Phalcon Developer Tools Installer
Make sure phalcon.sh is in the same dir as phalcon.php and that you are running this with sudo or as root.
Installing Devtools...
Working dir is: /Users/me/phalcon-tools
Done. Devtools installed!
Now how do I use the devtools? When I enter phalcon commands the output is exactly the same as above, and continues to tell me that it's installed.
Am I missing something here?
I noticed in the phalcon.sh script, at the end it had:
if check_install; then
php "$PTOOLSPATH/phalcon.php" $*
fi
So, if check_install passes, run phalcon.php. I've tried to run this script manually and nothing happens at the terminal.
$PTOOLSPATH is defined. I confirmed this using echo $PTOOLSPATH.
My /usr/bin/env php is correct and points to MAMP's PHP. I have Phalcon installed using MAMP at the moment. My PHP is correct:
which php
/Applications/MAMP/bin/php/php5.5.23/bin/php
Inspecting the phalcon.php script, and using xdebug, I detected the issue to be lying here:
if (!extension_loaded('phalcon')) {
throw new Exception(
sprintf(
"Phalcon extension isn't installed, follow these instructions to install it: %s",
Script::DOC_INSTALL_URL
)
);
}
So the Phalcon extension isn't loaded. Not sure why it's not printing the output of the exception in the terminal. But, PHP's error log is showing:
[21-May-2015 22:37:48 Europe/Berlin] PHP Fatal error: Class 'Phalcon\Script' not found in /Users/me/phalcon-tools/phalcon.php on line 41
Now I'm stumped.
Edit:
Running php -m showed me Phalcon isn't installed. Which is odd, because I am using Phalcon in my web application, and it works fine. As you can see, I've loaded the extension in the php.ini.
The PHP version I'm using is:
PHP 5.5.23 (cli) (built: Apr 9 2015 19:29:27)
As you can see, Phalcon is in the correct directory:
ls /Applications/MAMP/bin/php/php5.5.23/lib/php/extensions/no-debug-non-zts-20121212
apcu.so
imagick.so
phalcon.so
...
And as you can see from phpinfo() it's installed ...
The following commands both give different results too:
Shows Phalcon as installed:
echo "<?php phpinfo(); ?>" | php > phpinfo.txt && cat phpinfo.txt | grep phalcon
Shows Phalcon not installed:
php -m
Any ideas?
After trying a lot, I found out that the code shown in the reference is wrong:
ln -s ~/phalcon-tools/phalcon.sh ~/phalcon-tools/phalcon
chmod +x ~/phalcon-tools/phalcon
The proper way can be found in the github repo:
ln -s ~/phalcon-devtools/phalcon.php /usr/bin/phalcon
chmod ugo+x /usr/bin/phalcon
Basically, the link shouldn't be the script but the php file. Fixing that, I could run the dev tool properly.
I have the same problem with you. and I solved it as long as I add this two line into my ~/.bash_profile file.
export PATH=$PATH:/Users/scott/phalcon-tools
export PTOOLSPATH=/Users/scott/phalcon-tools
At first, I only add the first line into .bash_profile, and I got the same information with you.
May be in command line ini file phalcon is not installed so it is throwing error.
just type this command in terminal .
php --ini
then check the output the interesting line is
Loaded Configuration File: /etc/php5/cli/php.ini
now try to check that
phalcon.so
is loaded there as well or not.
as for web and cli there are different phalcon.so file is provided so we need to inlcude "phalcon.so" in both files.
so phalcon.so is included in your web php.ini so its running smooth there and not it command line i guess.

Run php file via command line on an ubuntu linux server

I am trying to run a php file on a ubuntu linux server but get a 'command not found' error when i run "php file_name.php"
Searching online, i found an article that suggested I run "sudo aptitude install php5-cli" which I did and restarted apache afterwards but I still get this error.
How do I fix this?
Try this once,
Go to terminal.
whereis php
It will show where is php installed.
Export that path to environment variable using following command
export PATH=$PATH;/path/to/php's/bin directory
Then execute required file..
As follows,
php file_to_execute.php
first make sure that you've installed following packs:
php5
php5-cli
php-pear
like this:
sudo apt-get install php5 php5-cli php-pear
then make sure to configure php safely befor using it.
also make your php file executable ( chmod 700 )
Try the following step :
Open your cmd/console or press ctr+alt+t.
php5 /your/path/to/php_file_name.

Categories