diff --git a/lib/SimpleSAML/Session.php b/lib/SimpleSAML/Session.php
index 592414190d99249445fef6f634dfcd663fb6e2b2..6ab6834b78dbf7f1b9dc1ef5c09302f6b24542d0 100644
--- a/lib/SimpleSAML/Session.php
+++ b/lib/SimpleSAML/Session.php
@@ -139,6 +139,13 @@ class SimpleSAML_Session
     {
         $this->authData = array();
 
+        if (php_sapi_name() === 'cli' || defined('STDIN')) {
+            $this->trackid = 'CL'.bin2hex(openssl_random_pseudo_bytes(4));
+            SimpleSAML\Logger::setTrackId($this->trackid);
+            $this->transient = $transient;
+            return;
+        }
+
         if ($transient) { // transient session
             $sh = SimpleSAML_SessionHandler::getSessionHandler();
             $this->trackid = 'TR'.bin2hex(openssl_random_pseudo_bytes(4));