Categories


Archives


Recent Posts


Categories


N98-magerun: System Commands

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

This article is part of a longer series covering the n98-magerun power tool

Today we’ll be covering the n98-magerun system (or sys) commands.

$ n98-magerun.phar list sys

sys:check                        Checks Magento System
sys:cron:history                 Lists last executed jobs
sys:cron:list                    Lists all cronjobs
sys:cron:run                     Runs a cronjob by job code
sys:info                         Prints infos about the current magento system.
sys:maintenance                  Toggles maintenance mode.
sys:modules:list                 List all installed modules
sys:setup:compare-versions       Compare module version with core_resource table.
sys:setup:run                    Runs all new setup scripts.
sys:store:config:base-url:list   Lists all base urls
sys:store:list                   Lists all installed store-views
sys:url:list                     Get all urls.
sys:website:list                 Lists all websites

These commands provide you with diagnostic information and allow you to perform common systems tasks from the comfort of n98-magerun’s command line interface. Let’s get started.

Maintenance Commands

The sys:info command will print out some basic Magento system information.

$ n98-magerun.phar sys:info

  Magento System Information  

Version                  : 1.7.0.1
Edition                  : Community
Cache Backend            : Zend_Cache_Backend_File
Cache Directory          : /path/to/magento/var/cache
Session                  : files
Crypt Key                : 18cea96f92f35a540d83a2fe7f33c005
Install Date             : Sun, 01 Jul 2012 17:06:09 +0000

This is one of those quick sanity check commands to help get your bearings in a new system, as well as ensure additional XML files aren’t interfering with the settings in app/etc/local.xml.

The sys:check command will run through a number of basic tests that ensure your Magento and PHP systems still contain the features, folders, and files needed to run Magento.

$ n98-magerun.phar sys:check

  Check: Filesystem                           

Folder media found.
Folder var found.
Folder var/cache found.
Folder var/session found.
File app/etc/local.xml found.
File index.php.sample found.

  Check: PHP                    

Required PHP Module simplexml found.
Required PHP Module mcrypt found.
Required PHP Module hash found.
Required PHP Module gd found.
Required PHP Module dom found.
Required PHP Module iconv found.
Required PHP Module curl found.
Required PHP Module soap found.
Required PHP Module pdo found.
Required PHP Module pdo_mysql found.
Bytecode Cache apc found.

  Check: Security                         

app/etc/local.xml cannot be accessed from outside.

  Check: MySQL  

MySQL Version 5.5.29 found.
Required MySQL Storage Engine InnoDB found.

This is another command that’s useful to run when you inherit an existing Magento system.

If you end up finding something that needs to be fixed and you want to take the store down while you deploy a solution, the sys:maintenance command can be used to flip Magento in and out of maintenance mode.

$ n98-magerun.phar sys:maintenance
Maintenance mode on

$ n98-magerun.phar sys:maintenance
Maintenance mode off

When Magento’s in maintenance mode, a “this site down for maintenance” placeholder page is rendered for all URLs. This is normally controlled by the presence of a file named maintenance.flag in Magento’s root folder, and all sys:maintenance does is create/delete this flag file.

The sys:setup:compare-versions command will compare the configured module versions with what’s in the core_resource table.

$ n98-magerun.phar  sys:setup:compare-versions
┌────────────────────────────────────────┬──────────┬──────────┬──────────┬──────┐
│Setup                                   │Module    │DB        │Data      │Status│
├────────────────────────────────────────┼──────────┼──────────┼──────────┼──────┤
│default_setup                           │          │          │          │OK    │
├────────────────────────────────────────┼──────────┼──────────┼──────────┼──────┤
│default_write                           │          │          │          │OK    │
├────────────────────────────────────────┼──────────┼──────────┼──────────┼──────┤
│default_read                            │          │          │          │OK    │
├────────────────────────────────────────┼──────────┼──────────┼──────────┼──────┤
│core_setup                              │1.6.0.2   │1.6.0.2   │1.6.0.2   │OK    │
...
├────────────────────────────────────────┼──────────┼──────────┼──────────┼──────┤
│ajaxify_read                            │          │          │          │OK    │
└────────────────────────────────────────┴──────────┴──────────┴──────────┴──────┘

  No setup problems was found. 

Magento’s setup resources are similar to database migrations in other software systems. They’re used to update the database when an extension is installed or upgraded. This command makes sure that the modules in the system have installed their data, which is helpful when diagnosing a setup script that’s not running.

Normally, setup resource scripts run automatically when Magento’s cache state is refreshed. When a new page is loaded, an uncached Magento will check for new scripts to run, and then run them. Because browsers themselves often cache pages, this can lead to situations where it’s not clear if a script has run or not. That’s why we have the sys:setup:run command. This command will automatically run any new setup resource scripts.

$ n98-magerun.phar sys:setup:run
done

The command works by clearing Magento’s cache and then calling the Mage_Core_Model_Resource_Setup::applyAllUpdates() and Mage_Core_Model_Resource_Setup::applyAllDataUpdates methods. Use it with care if your system is susceptible to cache warming problems.

Cron Commands

The sys:cron category provides insight into Magento’s normally opaque cron system. These commands are related to Magento’s internal cron system, and not the external *nix cron.

The sys:cron:list command will examine the merged Magento config.xml tree and list out all properly configured cron jobs.

$ n98-magerun.phar sys:cron:list

  Cronjob List  

    +-----------------------------------------+------+----+----+----+----+
    | Job                                     | m    | h  | D  | M  | WD |
    +-----------------------------------------+------+----+----+----+----+
    | aggregate_sales_report_bestsellers_data | 0    | 0  | *  | *  | *  |
    | aggregate_sales_report_coupons_data     | 0    | 0  | *  | *  | *  |
    | aggregate_sales_report_invoiced_data    | 0    | 0  | *  | *  | *  |
    | aggregate_sales_report_order_data       | 0    | 0  | *  | *  | *  |
    | aggregate_sales_report_refunded_data    | 0    | 0  | *  | *  | *  |
    | aggregate_sales_report_shipment_data    | 0    | 0  | *  | *  | *  |
    | aggregate_sales_report_tax_data         | 0    | 0  | *  | *  | *  |
    | captcha_delete_expired_images           | */10 | *  | *  | *  | *  |
    | captcha_delete_old_attempts             | */30 | *  | *  | *  | *  |
    | catalog_product_alert                   |      |    |    |    |    |
    | catalog_product_index_price_reindex_all | 0    | 2  | *  | *  | *  |
    | catalogrule_apply_all                   | 0    | 1  | *  | *  | *  |
    | core_clean_cache                        | 30   | 2  | *  | *  | *  |
    | currency_rates_update                   |      |    |    |    |    |
    | log_clean                               |      |    |    |    |    |
    | newsletter_send_all                     | */5  | *  | *  | *  | *  |
    | paypal_fetch_settlement_reports         |      |    |    |    |    |
    | persistent_clear_expired                | 0    | 0  | *  | *  | *  |
    | sales_clean_quotes                      | 0    | 0  | *  | *  | *  |
    | sitemap_generate                        |      |    |    |    |    |
    | system_backup                           |      |    |    |    |    |
    +-----------------------------------------+------+----+----+----+----+

If you’re trying to setup a custom cron job, this command is offers invaluable help in making sure your nodes are in the right spot and that your minute, hour, day, month, day-of-the-week entries are correct.

There’s also a command (sys:cron:run) for manually running a cron job.

$ n98-magerun.phar sys:cron:run log_clean
Run Mage_Log_Model_Cron::logClean done

This can be invaluable when you’re developing a new cron script, or debugging features that rely heavily on cron interaction.

The last cron command is sys:cron:history.

$ n98-magerun.phar sys:cron:history

  Last executed jobs                        

+-----------+---------+---------------------+
| Job       | Status  | Finished            |
+-----------+---------+---------------------+
| log_clean | success | 2013-04-22 21:35:01 |
+-----------+---------+---------------------+

This will list out the last time each configured cron job has run (assuming they’ve run at all). This is another one of those commands that’s useful when inheriting a Magento system. In the example above, only the log_clean cron job has run. This probably means the cron.php script (which runs Magento’s cron) was never setup correctly.

Information Commands

The sys:modules:list command is an alias for the already covered dev:module:list command, and will list all the installed modules in the system.

$ n98-magerun.phar.phar dev:module:list

  Magento Modules

+-----------+--------------------------------------+------------+----------+
| codePool  | Name                                 | Version    | Status   |
+-----------+--------------------------------------+------------+----------+
| core      | Mage_Core                            | 1.6.0.2    | active   |
| core      | Mage_Eav                             | 1.6.0.0    | active   |
...

According to the source code, sys:modules:list is depreciated, so you’re better off using dev:module:list in any shell scripts you write.

The sys:store:list command provides a list of all store view IDs and codes.

$ n98-magerun.phar sys:store:list

  Magento Stores  

+-----+-------------------+
| id  | code              |
+-----+-------------------+
|   1 | default           |
|   2 | german            |
|   3 | french            |
|   4 | second_store_view |
+-----+-------------------+

The sys:website:list command does the same, except for Magento’s “website” objects.

$ n98-magerun.phar sys:website:list

  Magento Websites  

+-----+----------------+
| id  | code           |
+-----+----------------+
|   1 | base           |
|   2 | second_website |
+-----+----------------+

The sys:store:config:base-url:list command will list the “base” url for each configured Magento store.

$ n98-magerun.phar sys:store:config:base-url:list

  Magento Stores - Base URLs  

+-----+-------------------+-----------------------------+-----------------------------+
| id  | code              | unsecure_baseurl            | secure_baseurl              |
+-----+-------------------+-----------------------------+-----------------------------+
|   1 | default           | http://magento.example.com/ | http://magento.example.com/ |
|   2 | german            | http://magento.example.com/ | http://magento.example.com/ |
|   3 | french            | http://magento.example.com/ | http://magento.example.com/ |
|   4 | second_store_view | http://magento.example.com/ | http://magento.example.com/ |
+-----+-------------------+-------------------------------------------+---------------+

Magento allows users to set the base URL for a store in the System -> Configuration -> Web section of the system configuration. This base URL will be used to generate all links and URLs in a particular Magento store. The sys:store:config:base-url:list command is a nice sanity check if you’re seeing weird URL behavior, or working with systems that have myriad apache URL configurations pointing at a system. It’s also a quick way to make sure a site’s using https urls for secure links (in the above example, you can see secure_baseurl is NOT doing this – no PCI for you).

Finally, the sys:url:list command will gather together and list Magento’s more “programatic” URLs for a particular store ID or code

$ n98-magerun.phar sys:url:list --add-cmspages default
http://magento.example.com/
http://magento.example.com/n98-magerun.phar/about-magento-demo-store
http://magento.example.com/n98-magerun.phar/customer-service
http://magento.example.com/n98-magerun.phar/enable-cookies
http://magento.example.com/n98-magerun.phar/privacy-policy-cookie-restriction-mode
http://magento.example.com/n98-magerun.phar/celebrity_home
http://magento.example.com/n98-magerun.phar/celebrity_no_route
http://magento.example.com/n98-magerun.phar/home

In the above example we’ve asked sys:url:list to give us all the CMS page URLs (--add-cmspages) for the default store view. If you take a look at the command options, you can see it allows you to fetch CMS, products, and category URLs

$ n98-magerun.phar help sys:url:list
...    
Options:
 --add-categories      Adds categories
 --add-products        Adds products
 --add-cmspages        Adds cms pages
 --add-all             Adds categories, products and cms pages

You could also list all three URL types with the --add-all option

$ n98-magerun.phar sys:url:list --add-all default

Behind the scenes this command is using the resource models from the sitemap modules to gather together these URLs. Keep in mind there may be more URLs browsable in your system via manual MVC routing, or rewrites added to the database/configuration.

Copyright © Alan Storm 1975 – 2017 All Rights Reserved

Originally Posted: 24th April 2013