shams Posted September 18, 2018 Share Posted September 18, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/ Share on other sites More sharing options...
requinix Posted September 18, 2018 Share Posted September 18, 2018 "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? Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560920 Share on other sites More sharing options...
shams Posted September 19, 2018 Author Share Posted September 19, 2018 (edited) 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 September 19, 2018 by shams Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560922 Share on other sites More sharing options...
requinix Posted September 19, 2018 Share Posted September 19, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560923 Share on other sites More sharing options...
shams Posted September 20, 2018 Author Share Posted September 20, 2018 ~$ php -m | grep apc apc apcu Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560934 Share on other sites More sharing options...
requinix Posted September 20, 2018 Share Posted September 20, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560935 Share on other sites More sharing options...
shams Posted September 20, 2018 Author Share Posted September 20, 2018 (edited) 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 September 20, 2018 by shams Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560936 Share on other sites More sharing options...
requinix Posted September 20, 2018 Share Posted September 20, 2018 Ah. Knew I was forgetting something important. Did you enable the apc.enable_cli setting? Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560937 Share on other sites More sharing options...
shams Posted September 20, 2018 Author Share Posted September 20, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560938 Share on other sites More sharing options...
shams Posted September 20, 2018 Author Share Posted September 20, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560939 Share on other sites More sharing options...
requinix Posted September 20, 2018 Share Posted September 20, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560940 Share on other sites More sharing options...
gizmola Posted September 20, 2018 Share Posted September 20, 2018 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 Quote Link to comment https://forums.phpfreaks.com/topic/307701-php-code-problem-from-command-line/#findComment-1560972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.