Claude Code "Not logged in · Please run /login" on a server
Why Claude Code says Not logged in, Login expired or another process is refreshing your login on a server or scheduled job, and which credential fixes it.
Not logged in · Please run /login means Claude Code found no credential for that run. On a server, that usually means the job runs as a different user, or with a different HOME or CLAUDE_CONFIG_DIR, from the one you signed in as. Login expired and OAuth token revoked mean there was a sign-in and it no longer works. A message about another process refreshing your login means a second Claude Code on the machine holds the refresh lock. Run claude auth status as the job’s user to see what it finds. Then give the job a credential that doesn’t depend on you being there: a one-year token from claude setup-token on your plan, or an API key billed to a Console account.
Which message, which cause
Anthropic’s errors reference lists each of these messages. In a claude -p run they read differently from the terminal versions:
Not logged in · Please run /login“No valid credential is available for this session.” This run sees no sign-in, token or key at all. On Claude Code 2.1.323 a signed-outclaude -pprints exactly this and exits 1.Login expired · Please run /login, in-pFailed to authenticate: OAuth session expired and could not be refreshed. Claude Code tried to renew your saved login, was refused, and cleared the saved credentials. Retrying does nothing: “Retrying without signing in shows the same message on every request.”OAuth token revoked · Please run /loginor401 OAuth token has expired, in-pstartingFailed to authenticate. The API refused the credential Claude Code sent. “A revoked token means you signed out everywhere or an admin removed access.” A token fromclaude setup-tokengives the same messages when it lapses.Could not refresh your login because another Claude Code process is refreshing it, in-p“Failed to refresh OAuth token: another Claude Code process is refreshing it or exited mid-refresh”. Your login was fine but due for renewal, and another process held the refresh lock. Anthropic’s first advice is to retry in a minute.- “Failed to refresh OAuth token: the disk that holds the Claude Code config directory (~/.claude by default), or your disk quota on it, is full”. Free space and rerun. Signing in won’t help, because the new login couldn’t be saved. Running out of inodes gives the same message.
/plugin isn't available in this environment.Not a sign-in problem. A-prun can’t open an interactive command (below).
The headless docs say a failure such as missing authentication is printed “as the result on stdout”, so a cron job that saves only stderr never records it. Capture both, and check the exit status.
“Not logged in” when you are logged in
On Linux the sign-in lives in your account’s ~/.claude/.credentials.json, or under CLAUDE_CONFIG_DIR when that is set. A job only finds it if it runs as the same user, with the same home folder and the same CLAUDE_CONFIG_DIR. The usual ways to break that are a system service with no User=, which runs as root, a job started with sudo, a container that doesn’t mount the folder, and a job that sets its own HOME. To see what the job’s account finds, run:
sudo -u <job-user> -i claude auth statusCopiedOn 2.1.323, a signed-out account prints JSON with "loggedIn": false and "authMethod": "none" and exits 1. If the job is meant to use an API key, check that ANTHROPIC_API_KEY is exported in the environment the job actually starts in, not only in your interactive shell’s ~/.bashrc.
The errors page names one more cause. When someone signs in again with the same claude.ai account on the machine while claude -p jobs are running, jobs could fail with this message, because “Before v2.1.318, Claude Code removed the stored login during that sign-in, before saving the new one.” Update Claude Code with claude update.
A login that lapses overnight
Login expired is the one that catches unattended work. Anthropic doesn’t publish how long a /login sign-in lasts. Claude Code warns three days before it expires, but only at startup, which a scheduled run or a session left open for days never shows you. The same page says a background session in agent view “that outlives the login stops making progress once the credential expires and can’t recover until you sign in again.”
A report on Claude Code’s tracker shows what a forced sign-in can cost, issue #108585, opened on November 9, 2026: a forced sign-in during a multi-agent session left running overnight killed every background subagent, threw away their unfinished work, and was only noticed about six hours later. The issue is open, and no maintainer has replied. Until it changes, renew before you leave a long session running: /login renews the sign-in, and /status shows a Login row reading Expired — log in again once it has lapsed.
For a -p job, the errors page’s fix is to run claude “in the same environment, complete /login, then rerun your command”: same user and same config folder, or the new sign-in goes somewhere the job doesn’t look. Installing Claude Code on Linux covers signing in on a machine with no browser, where you paste a code back from your phone or laptop.
“Another Claude Code process is refreshing it”
Claude Code processes on one machine take turns renewing a login through a shared lock. The errors page says this message appears when another process “held the shared refresh lock, or exited and left it behind”. Retry in a minute. If it keeps coming back, close other Claude Code processes, and if none are running, sign in again, which “doesn’t wait on the refresh lock.”
A server can leave that lock behind itself. Issue #105729, opened on 2.1.280 on Linux, traces it to a health check. An agent gateway was running claude auth status --json as an availability probe. The probe started a token refresh, created ~/.claude/.oauth_refresh.lock, and was killed by its caller’s timeout. With SIGTERM or SIGKILL straight after the lock appeared, the lock stayed behind in 8 of 8 runs, and every other Claude Code process on the account failed with this message until it went stale, about 60 seconds later. On a normal exit the lock is released. The issue is open, with no maintainer reply.
So if a monitor, a gateway or the top of a script runs claude auth status, let it finish rather than wrapping it in a short timeout, and don’t run it more often than you need to. To see whether a lock is there right now:
ls -l ~/.claude/.oauth_refresh.lockCopied“/<command> isn’t available in this environment.”
A -p run can’t open an interactive command. The headless docs say that skills and custom commands you invoke yourself do work in a prompt, but “built-in commands that only run in the terminal interface, such as /login, aren’t available.” Plugins are the common case: in a non-interactive run, Anthropic’s plugin docs say “/plugin doesn’t run, and Claude replies /plugin isn't available in this environment. Plugins you already installed do load.” Install them from the shell instead:
claude plugin marketplace add anthropics/claude-plugins-official
claude plugin install <plugin>@claude-plugins-officialCopiedThe first line matters on a new server. The official marketplace is added the first time someone opens an interactive session there, so a setup script adds it before the install. For /login, there is no non-interactive form: sign in once in a terminal on the machine, or use one of the credentials below. Claude Code plugins covers the rest of the plugin setup.
A plan’s sign-in or an API key for the job
The two are different credentials with different purposes, and Anthropic says so on its legal and compliance page. Signing in with a Claude plan “is designed to support ordinary use of Claude Code and other native Anthropic applications”, and developers building products or services “should use API key authentication through Claude Console or a supported cloud provider.”
- Your plan, as a token.
claude setup-tokenprints a one-year token forCLAUDE_CODE_OAUTH_TOKEN, which the authentication docs say to “use for CI pipelines and scripts where browser login isn’t available.” It “authenticates with your Claude subscription”, so the job spends your plan, and when it lapses you get the revoked-or-expired messages above. The setup-token guide covers storing it and what it can’t do. - An API key in
ANTHROPIC_API_KEY, “for direct Anthropic API access with a key from the Claude Console”, its usage “billed to the key owner” rather than drawn from a plan. The docs call it “a static credential”, so there is no sign-in to expire or renew. In-p“the key is always used when present”, even over a working sign-in, and a key with no credit behind it stops the run with “Credit balance is too low”. Where the key comes from a vault, anapiKeyHelperscript can fetch it when the run starts.
Running Claude Code on a schedule covers the rest of an unattended job: the timer, permissions, limits, and hearing about a run that failed.