|

Why a systemd Serviceo Starts Manually but Fails After Reboot

After a reboot, the server is accessible over SSH, but the application does not respond.systemctl status myapp.serviceshowsinactiveorfailed. The administrator runssystemctl start myapp.service, and the application starts normally. The same problem returns after the next reboot.

This does not prove that the application itself is faulty. The service may not be enabled for boot, may start before the network or filesystem is ready, or may stop retrying after repeated failures. The useful evidence is in the failed boot, not in the later successful manual start.

Check What Actually Works When Started Manually

Ifsystemctl start myapp.servicesucceeds after boot, the unit can at least start the application under systemd. The first checks should cover automatic startup, unit ordering and the state of dependencies during boot.

A different problem arises when/opt/myapp/bin/myappruns successfully from SSH butsystemctl start myapp.servicefails. An interactive shell may have a differentPATH, environment variables and working directory. Systemd runs the process under the user and permissions specified in the unit. A working command in a terminal does not mean the unit is configured correctly.

On a VPS running systemd-managed applications, starting a service manually several minutes after boot may succeed simply because the network, mounts and other services are ready by then.

Check Whether the Service Is Enabled at Boot

Start with a read-only command:

systemctl is-enabled myapp.service

enabledindicates that the unit has the installation links used for automatic activation.disabledmeans those links have not been created, although installation instructions exist. Astaticunit has no ordinary enablement instructions but may legitimately be started as a dependency, through socket activation or another mechanism.

Amaskedunit cannot be started. Ifsystemctl startsucceeds for what appears to be the same unit, check whether its name or the manager’s state has changed. Read that status in the context of how the unit is meant to be activated.

systemctl startstarts the unit now.systemctl enablecreates the symbolic links specified by its[Install]section for future activation. They are separate operations. A typical service contains:

[Install]
WantedBy=multi-user.target

The presence ofWantedBy=does not mean the service is enabled. Runningsystemctl enable myapp.servicecreates the corresponding link undermulti-user.target.wants/.systemctl enable --now myapp.servicealso starts the service immediately, but should not be used as a first response before the failure has been investigated. Managing system units requires administrative privileges.

Check that the relevant target participates in normal boot, usingsystemctl get-defaultand the target’s dependencies. Even anenabledservice can enter the boot transaction and fail to start. The distinction is covered in the systemctl documentation.

Inspect the Effective Unit and the Failed Boot Journal

Rather than opening only the unit file you expect to be in use, inspect the configuration and its drop-ins:

systemctl cat myapp.service
systemctl show myapp.service -p FragmentPath -p DropInPaths

The main unit may come from a software package, while local changes live under/etc/systemd/system/myapp.service.d/.systemctl catdisplays files on disk that the manager may not yet have reloaded. After editing a unit,systemctl daemon-reloadmakes systemd reread its configuration; it does not restart the application.

The most useful evidence comes from the failed startup attempt, not the service state after a successful manual start:

journalctl -b -u myapp.service --no-pager

Find the first attempt and compare its timestamp with other boot events. Look forNo such file or directory,Permission denied,Connection refused, DNS failures, missing environment files, failedExecStartPre=commands, timeouts,start-limit-hitor a unit skipped because ofCondition...orAssert....

The exact messages depend on the environment. If the machine has since rebooted again, usejournalctl -b -1 -u myapp.service, provided the previous boot’s journal was retained. A current status ofactive (running)does not erase the earlier failure.

Separate Unit Ordering from Service Dependencies

After=postgresql.servicemeans that, if both units are part of the same startup transaction,myapp.serviceis ordered after the startup phase ofpostgresql.service. Systemd determines when that phase is complete according to the PostgreSQL unit’sType=and startup commands. This does not guarantee that PostgreSQL is ready to accept application queries.

After=alone does not add PostgreSQL to the transaction or cause it to be started.Before=specifies the opposite ordering but does not pull in a dependency either.

If the application needs a local service to be started alongside it, use a combination such as:

[Unit]
Wants=postgresql.service
After=postgresql.service

Wants=pulls in the dependency, but a failure in that service will generally not prevent the application from starting.

If the application genuinely cannot start successfully without the local service, a stronger dependency may be appropriate:

[Unit]
Requires=postgresql.service
After=postgresql.service

WithRequires=andAfter=together, failure to start the required unit prevents the dependent unit from starting.Requires=does not guarantee that the dependency will remain available throughout the application’s lifetime.

The opposite case matters too:Wants=postgresql.servicewithoutAfter=pulls in PostgreSQL but still allows both units to start in parallel. The systemd.unit documentation distinguishes dependency relationships from startup ordering. Do not introduceRequires=without a specific need, as it makes the application more sensitive to failures in another service.

network.target Does Not Mean the Network Is Ready

After=network.targetdoes not guarantee an assigned IP address, working DNS resolution or a route to a remote database. This target primarily provides an ordering point for networking services.

An application may start afternetwork.targetbut before interface configuration, routing and DNS setup have finished. An immediate connection to a remote database, message broker or external API can then fail.

A minute later, the samesystemctl startcommand works because the remote resource is now reachable. Confirm the sequence through timestamps and connection errors rather than assuming thatnetwork.targetindicates usable connectivity.

When network-online.target Is Appropriate

If an application genuinely cannot start before the network has been configured, its[Unit]section may need:

Wants=network-online.target
After=network-online.target

Wants=pulls the target into the startup transaction.After=orders the application after that target.

Using onlyAfter=network-online.targetdoes not guarantee a delay if nothing pulls the target into the transaction. Even whennetwork-online.targetis reached, it only means the network manager has satisfied its own criteria for being online. It does not establish thatdb.example.net:5432, an external API or a particular DNS response is available.

First identify which service manages the network:

systemctl status NetworkManager.service
systemctl status systemd-networkd.service

Then examine the correspondingNetworkManager-wait-online.serviceorsystemd-networkd-wait-online.serviceand the dependencies ofnetwork-online.target.

Do not enable an arbitrary wait-online service. Readiness criteria depend on the network manager, waiting may delay boot, andnetwork-online.targetdoes not monitor subsequent connectivity failures. The systemd network-online guidance explains these limitations.

Remote dependencies still need connection retries with backoff, timeouts and proper handling of temporary failures.ExecStartPre=/bin/sleep 30does not test whether a resource is ready. Five seconds might be enough on one boot, while a minute may not be enough on another.

A local PostgreSQL instance can be linked through systemd dependencies.Requires=postgresql.servicecannot make an external database available, and even a started local service may not yet have passed an application-level readiness check.

Check Mounts and Files Needed During Early Boot

On a server with administrative access to systemd, you can compare the application’s paths, mount dependencies and boot journal.

The failure may be unrelated to networking. If/mnt/data,/srv/storageor another directory has not yet been mounted, the application may be unable to find its configuration, database or working files. Later in the boot process, the filesystem becomes available and manual startup succeeds.

For a path that must be accessible before the service starts, consider:

[Unit]
RequiresMountsFor=/mnt/data

Systemd adds dependencies on the mounts needed to access that path. Network storage still requires appropriate network handling and must account for the availability of the remote server. Simply addingAfter=local-fs.targetis not a substitute for a dependency on the required mount.

Check whether the directory exists during boot, rather than only afterwards.

ForWorkingDirectory=, systemd automatically addsRequires=andAfter=dependencies on the mounts needed to access the specified path. Before adding a separateRequiresMountsFor=, inspect the dependencies already generated and the boot journal.

Compare the Service User, Environment and Preparation Commands

CheckUser=,Group=,WorkingDirectory=,EnvironmentFile=andExecStart=in the unit. For example:

[Service]
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
EnvironmentFile=/etc/myapp/myapp.env
ExecStart=/opt/myapp/venv/bin/python -m myapp

An SSH session may load a shell profile, a differentPATH, a virtual environment or credentials automatically. Systemd does not have to reproduce that interactive environment.

Use the actual executable path. Check that the service user can access the working directory, configuration, sockets, keys and log directories.

A missing requiredEnvironmentFile=or an incorrect interpreter path will prevent startup. Check whether another job creates those files later in the boot process.

InspectExecStartPre=as well. If a database-check script fails during boot, the mainExecStart=command is never executed. The same script may succeed later when the database is reachable.

The journal should include the preparation commands, which should be short-lived and return meaningful exit codes. Do not turnExecStartPreinto a long-running process that waits indefinitely for an external API.

Restart Policies and Start Rate Limiting

For a service that exits with a non-zero status after a temporary failure, a restart policy such as this may help:

[Service]
Restart=on-failure
RestartSec=5s

This is an example, not a universal setting. A persistent configuration error will cause repeated failures. Systemd also limits startup frequency. After enough failed attempts, the service may reachstart-limit-hit, at which point automatic restarts stop.

Inspect the effectiveStartLimitIntervalSec=andStartLimitBurst=values rather than increasing them without finding the cause.

Once the underlying problem has been fixed, a new attempt may require clearing the failed state and rate-limit counters:

systemctl reset-failed myapp.service
systemctl start myapp.service

These commands change the service state.reset-faileddoes not repair the original fault.

Aninactivestate is not always a failure either: a short-livedoneshotservice may exit normally. For an application expected to remain running, examine its service type, exit status, restart count and expected state after startup.

Compare Dependencies with the Actual Boot Sequence

To inspect the effective dependencies rather than relying only on the unit file, run:

systemctl show myapp.service -p After -p Before -p Wants -p Requires
systemctl list-dependencies myapp.service
systemctl list-dependencies --after myapp.service

list-dependencieshelps identify linked services, while--aftershows ordering relationships.

Do not treat every unit named underAfter=as something systemd will automatically start for the application. Compare those entries withWants=andRequires=, and inspect the target’s dependencies where needed.

After boot, two more commands are useful:

systemd-analyze critical-chain myapp.service
systemd-analyze blame

critical-chainshows the time-critical activation chain, including waits involvingnetwork-online.targetor another unit. However, systemd starts jobs in parallel, socket activation can affect the result, and jobs that timed out may not always be visible. The chain can also be incomplete for a failed service. Use it alongsidejournalctl, not instead of the journal.

blamesorts units by the time they spent activating. It is not a list of reasons why the application failed, nor does it include all the time a job spent waiting in the queue.

WithType=simple, the application’s startup work may not appear as a lengthy activation phase. Its absence from the list of slow units proves little. Both commands have limitations described in the systemd-analyze documentation.

Before applying changes, check the unit’s syntax and known dependencies:

systemd-analyze verify /etc/systemd/system/myapp.service

Replace the path with the real unit file.verifycan find invalid directives and dependency problems, but cannot test remote database availability or the environment during early boot.

After modifying the unit or its drop-ins, runsystemctl daemon-reload. A successfulsystemctl restart myapp.serviceon a fully booted machine still does not confirm that automatic startup will work.

Worked Example: An Application Starts Before Its Remote Database Is Ready

Suppose a service is enabled at boot and the application attempts to connect to a remote database once during startup. Its unit contains:

[Unit]
After=network.target

[Service]
ExecStart=/opt/myapp/bin/myapp

[Install]
WantedBy=multi-user.target

After a reboot,network.targethas been reached, but the interface may still be receiving a route or DNS may not yet be ready. The application encounters a connection error and exits.

A minute later, the administrator runssystemctl start myapp.service, and the database connection succeeds.

journalctl -b -u myapp.serviceshows the first failed startup attempt and its timestamp. Examining the startup order confirms thatAfter=network.targetdid not provide the required network readiness.

For an application that genuinely needs the network configured before starting, the unit is updated withWants=network-online.targetandAfter=network-online.target, after checking the network manager and its wait-online service.

The application also gains bounded retries with backoff for database connections, since a configured network does not guarantee that the remote database is reachable.

After a controlled reboot, the application begins serving requests without manual intervention, and the journal no longer shows the earlier startup failure.

This is an illustrative diagnostic sequence, not an actual Era.Host incident.

Verify the Fix After a Reboot, Not Just a Restart

Before rebooting, checksystemctl is-enabled myapp.service, the effective unit and the results ofsystemd-analyze verify.

Schedule a production reboot during an agreed maintenance window. Where possible, reproduce the scenario on a staging server first. Retain the original logs for comparison.

After the machine boots, without manually starting the service, run:

systemctl status myapp.service
journalctl -b -u myapp.service --no-pager

Confirm that the service started as part of boot, did not pass through an earlyfailedorstart-limit-hitstate, and is running with the correct user and environment. Check that the required mounts are available.

Verify application readiness rather than relying solely on the systemd status: test connections to the database or Redis, the application’s health endpoint and its main operations. Repeat the checks after several minutes to rule out restart loops and delayed failures.

A successful manualrestartdoes not prove the problem is fixed. The fix is confirmed when the application starts automatically after boot, passes its readiness checks and remains stable without administrator intervention. Correct the dependency, environment or failure-handling logic rather than hiding the problem behind a fixedsleep.

Similar Posts