Your systemd service is reporting active (exited) because the service’s main process successfully started, did its job, and then terminated cleanly. systemd interprets a clean exit as the service completing its intended work and then exiting, rather than running indefinitely like a daemon.
Common Causes and Fixes for active (exited) State:
-
One-Shot Service Configuration:
- Diagnosis: The service is intended to run once and exit. This is a valid
systemdservice type. Check theType=directive in your service’s.servicefile. If it’s set tooneshot, this is expected behavior. - Fix: No fix is needed if this is the intended behavior. If it’s not intended, change
Type=oneshottoType=simple(orforkingif appropriate) in the.servicefile and reloadsystemd:sudo systemctl daemon-reload sudo systemctl restart your_service_name.service - Why it works:
Type=simpletellssystemdthat the main process is the service itself and it should be considered running as long as that process is alive.Type=forkingindicates the parent process will exit after forking a child daemon process.
- Diagnosis: The service is intended to run once and exit. This is a valid
-
Service Script Exits Immediately:
- Diagnosis: The script executed by
ExecStart=in the.servicefile is running, but it’s exiting very quickly, perhaps due to an immediate error or a condition that causes it to finish. Check the service’s logs for any output from the script.sudo journalctl -u your_service_name.service -f - Fix: Debug the script itself. Add
set -xat the beginning of your shell script to trace execution. Identify the line causing the premature exit. For example, a missing configuration file or an incorrect command argument might cause this.# Example fix: Ensure a required file exists before proceeding if [ ! -f /etc/your_app/config.yaml ]; then echo "Error: Configuration file missing." >&2 exit 1 fi - Why it works: By identifying and correcting the condition that causes the script to exit early, you allow the script to continue running or perform its intended long-running task.
- Diagnosis: The script executed by
-
Incorrect
Type=Setting for a Daemon:- Diagnosis: You’ve configured a service that should be a long-running daemon (like a web server or database) but have
Type=oneshotorType=simplewhere the process forks and the parent exits. Thesystemdlogs will show theExecStartprocess exiting.sudo journalctl -u your_service_name.service - Fix: If your application forks a child process and the parent exits, change
Type=toforking. You may also need to specifyPIDFile=in the.serviceunit sosystemdcan track the daemon’s main process.
Then reload and restart:[Service] Type=forking PIDFile=/run/your_app.pid ExecStart=/usr/bin/your_daemon_start_commandsudo systemctl daemon-reload sudo systemctl restart your_service_name.service - Why it works:
Type=forkingtellssystemdto expect theExecStartprocess to fork and the parent to exit.systemdthen monitors the child process using thePIDFileto determine the service’s status.
- Diagnosis: You’ve configured a service that should be a long-running daemon (like a web server or database) but have
-
RemainAfterExit=yesNot Set for One-Shot Success:- Diagnosis: You have a
Type=oneshotservice that successfully completes its task, but you wantsystemdto consider it "active" even after it exits, perhaps because it sets up some system state. The service runs and exits as expected, butsystemddoesn’t keep track of it as "active." - Fix: Add
RemainAfterExit=yesto the[Service]section of your.servicefile.
Reload[Service] Type=oneshot ExecStart=/usr/bin/your_setup_script.sh RemainAfterExit=yessystemdand restart the service.sudo systemctl daemon-reload sudo systemctl restart your_service_name.service - Why it works:
RemainAfterExit=yesinstructssystemdto consider the service active as long as any of its instantiated units are active, even if the mainExecStartprocess has exited. This is useful for services that perform a setup action and then are expected to remain "enabled" without a running process.
- Diagnosis: You have a
-
Incorrect
ExecStop=orExecStopPost=Logic:- Diagnosis: If your service is supposed to be a long-running daemon, but it’s exiting unexpectedly, it might be that
systemdis trying to stop it, and theExecStop=command is causing it to exit prematurely or incorrectly. Check the logs for stop-related messages.sudo journalctl -u your_service_name.service -b - Fix: Review the
ExecStop=command in your.servicefile. Ensure it correctly signals the daemon to stop and exits cleanly itself. If the daemon doesn’t have a specific stop command, you might not needExecStop=, assystemdwill sendSIGTERMby default. If it’s aforkingtype, ensureExecStop=correctly terminates the daemon and exits.[Service] Type=forking ExecStart=/usr/sbin/your_daemon -d ExecStop=/usr/bin/kill -s TERM $MAINPID - Why it works: A correctly defined
ExecStop=ensures thatsystemdcan gracefully shut down the daemon process when requested, preventing it from exiting in a way thatsystemdinterprets as an error.
- Diagnosis: If your service is supposed to be a long-running daemon, but it’s exiting unexpectedly, it might be that
-
Dependencies Causing Premature Exit:
- Diagnosis: The service might be exiting because a dependency it relies on (e.g., a network mount, another service) is not available or fails during the service’s execution. Check
Requires=andWants=directives in the.servicefile and their status.systemctl list-dependencies your_service_name.service sudo journalctl -u your_service_name.service -f - Fix: Ensure all dependencies are correctly configured and running. If a
Requires=dependency fails,systemdwill often stop the dependent service. For example, if your service needs a network connection, ensureAfter=network-online.targetandWants=network-online.targetare present.
Reload and restart.[Unit] Description=My Service After=network-online.target Wants=network-online.targetsudo systemctl daemon-reload sudo systemctl restart your_service_name.service - Why it works:
systemdmanages service startup order and dependencies. By correctly declaring dependencies, you ensure that required resources are available before your service starts, preventing it from failing due to missing prerequisites.
- Diagnosis: The service might be exiting because a dependency it relies on (e.g., a network mount, another service) is not available or fails during the service’s execution. Check
After resolving these, the next error you might encounter is a failed state if the service now crashes instead of exiting cleanly, or a timeout if it takes too long to start.