Jump to content
shams

php code problem from command line?

Recommended Posts

I need to run getmail with two php codes in a loop, i tried to run all these from  the web browser but the php code cannot start the getmail i use the exec, shell_exec and system, now i decided to run these code from the gnome terminal, but i get the problem with the one of php code not running  from the command line , this one is not running:

<?php
$val = apc_fetch('id');
$id=$val + 1;

apc_store('id', $id, 0);
$var = apc_fetch('id');
echo $var
?> 

the command i am running the php code is

#php -f /home/user/wwww/example.net/apc.php

any help please?

Share this post


Link to post
Share on other sites

"Not running" doesn't say much. I'm confident the code really is running, and the problem is that it's not doing what you want it to do.

We'll start easy: do you have apcu installed and enabled?

Share this post


Link to post
Share on other sites

i am running ubuntu 18.04 and the packages php-apcu php-apcu-bc are installed, these php codes working fine from the web browsers.

should i install the apcu from github or it's sufficient?

Edited by shams

Share this post


Link to post
Share on other sites

Does php -m on the command line show that apcu is active?

And no, if you installed it then you installed it. You don't need to go grab the sources.

Share this post


Link to post
Share on other sites

Then by all accounts it should run. Not necessarily do what you think it will, but it should run and output something.

Unless...

#php -f /home/user/wwww/example.net/apc.php

The # isn't actually part of the command, right?

Share this post


Link to post
Share on other sites

no # is not of the command i put just for the bash prompt, this is the output when i run the apc.php  from the firefox web browser:
 

http://example.net/apc.php

1

prints the right ouput but from command line i didn't see any output.

Edited by shams

Share this post


Link to post
Share on other sites

Ah. Knew I was forgetting something important.

Did you enable the apc.enable_cli setting?

Share this post


Link to post
Share on other sites

from where i should enable apc.enable_cli i searched in /etc/php/7.2 and in /etc/apache2 but didn't find that one?

Share this post


Link to post
Share on other sites

I  enabled the  apc.enable_cli from the /etc/php/7.2/apache2/conf.d/20-apcu.ini now when i run the apc.php from the command line  it just prints the 1, in every load it should increase the id by plus one,  which is doing from  the web browser prints the 5?

Share this post


Link to post
Share on other sites

Excellent: we've reached the "not necessarily do what you think it will" stage.

The APC cache only lasts as long as the PHP process is running. From the command line, it executes your script and exits. The cache is destroyed.

It's not practical, nor for the most part even reasonable, to use a memory-level cache (like APC) for command-line scripts. If you need something cached, and especially if you want that value shared between the command-line and web scripts, then you need another type of cache. A network-level cache, like Redis or Memcache.

Share this post


Link to post
Share on other sites
13 hours ago, requinix said:

Excellent: we've reached the "not necessarily do what you think it will" stage.

The APC cache only lasts as long as the PHP process is running. From the command line, it executes your script and exits. The cache is destroyed.

It's not practical, nor for the most part even reasonable, to use a memory-level cache (like APC) for command-line scripts. If you need something cached, and especially if you want that value shared between the command-line and web scripts, then you need another type of cache. A network-level cache, like Redis or Memcache.

Yes exactly.  APC cache has no benefit for CLI because the cache is tied to each php process.  For CLI scripts the php process gets created and destroyed on each excecution.

If you want an in memory cache you need to use one of the old standby methods: memcached or Redis.  Those also have the advantage of being independent of the application server, so they are inherently capable of working in a cluster.  The other option is to use the shared memory functions

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.