CMSCLI can only be invoked from command line

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

CMSCLI can only be invoked from command line

Post by rotezecke »

I want to use CMSCLI as a cronjob. My line of code works fine from command line, but when set as cronjob (via cpanel) i get
The application may only be invoked from a command line, got "cgi-fcgi"
What am I doing wrong.

Code: Select all

/path/to/cmscli.phar -d /path/to/cmsms cache-clear 
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: CMSCLI can only be invoked from command line

Post by rotezecke »

Full disclosure: i forgot about my modified config.php file where I have an is_cli function, testing whether a webserver or CLI is accessing CMSMS. cmscli should fail with the first test and go to the else statement (it does so when executing from command line, but not from crontab.

Code: Select all

function is_cli()
{
    if ( defined('STDIN') )
    {
        return true;
    }
    if ( php_sapi_name() === 'cli' )
    {
        return true;
    }
    if ( array_key_exists('SHELL', $_ENV) ) {
        return true;
    }
    if ( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0) 
    {
        return true;
    } 
    if ( !array_key_exists('REQUEST_METHOD', $_SERVER) )
    {
        return true;
    }
    return false;
}
if(!is_cli() && (strpos($_SERVER['SERVER_ADDR'],'10.10.10.',0) === 0 || $_SERVER['SERVER_NAME'] =='xxxxx')) {
//wrong config
} else {
//correct config
}
deactivated010521

Re: CMSCLI can only be invoked from command line

Post by deactivated010521 »

I don't see the reason why you have modified your config.php file.

When running cmscli as crontab be sure to specify where cmscli is installed (full_cmscli_path) Also point to to the cmsms root directory. (~ cmscli -d full_cmsms_root_path).

php-cli is the executer based on your system the path can be `/usr/bin/php` || `/bin/php` (~ whereis php).

Code: Select all

# clear cmsms cache every 5 minutes 
*/5 * * * * /usr/bin/php /full_cmscli_path/cmscli -d /full_cmsms_root_path/public cache-clear  2>&1
double check that you have set excution permissions on cmscli (eg. ~ chmod 755 cmscli).
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: CMSCLI can only be invoked from command line

Post by calguy1000 »

Your function is incorrect.

by default cron is not run from a console/terminal/tty. Therefore there would be no open STDIN resource in PHP. Unless perhaps you are piping data into a command.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: CMSCLI can only be invoked from command line

Post by rotezecke »

I just replace my config file with the original one, no difference. Cron Daemon sends me this email:
Content-type: text/html; charset=UTF-8

The application may only be invoked from a command line, got "cgi-fcgi"
I tried all sorts of ways to call cmscli: absolute path, relative, ~, cd to directory, as one-liner, or inside a script.sh (with execute permissions). When my call to cmscli was incorrect, cron sent a different error message.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3484
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CMSCLI can only be invoked from command line

Post by velden »

Are you using the method like Arnoud described? Calling the php executable from cron explicitly.
*/5 * * * * /usr/bin/php /full_cmscli_path/cmscli -d /full_cmsms_root_path/public cache-clear 2>&1
Further I note that you're using cmscli.phar while reading the install instructions (viewtopic.php?f=1&t=77184) it should have been renamed to cmscli (not sure that's relevant).
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: CMSCLI can only be invoked from command line

Post by rotezecke »

I have about 10 other cronjobs, all using /usr/bin/php -q, working fine.
/usr/bin/php /home/xxx/bin/cmscli -d /home/xxx/public_html cache-clear 2>&1
Renaming cmscli.phar to cmscli, and even with 777 permissions made no difference.
This is on a shared host with PHP 7.2.16, Linux 2.6.32-896.16.1.lve1.4.54.el6.x86_64, CMSMS 2.2.10.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3484
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CMSCLI can only be invoked from command line

Post by velden »

Out of curiosity I created a cronjob and seems to work well. That means: I receive an email containing the cronjob result:
Cache will be cleared on the next request

Code: Select all

/usr/local/bin/php /home/xxx/yyy.nl/cmscli -d /home/xxx/yyy.nl/ cache-clear
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: CMSCLI can only be invoked from command line

Post by rotezecke »

i figured it out: i need to call php-cli explicitly with cmscli.

Code: Select all

/usr/bin/php-cli /home/xxx/bin/cmscli -d /home/xxx/public_html cache-clear
Thanks everyone.
Post Reply

Return to “Modules/Add-Ons”