0
  • Note: ~/opt/bin/MemoryDiagnostics is a bash script and is executable.
$ cat MemoryDiagnostics.service

[Unit] Description=MemoryDiagnostics Service

[Service] ExecStart="%h/opt/bin/MemoryDiagnostics" SyslogIdentifier=MemoryDiagnosticsService

[Install] WantedBy=default.target

$ systemctl --user enable MemoryDiagnostics.service 
Created symlink /home/nikhil/.config/systemd/user/default.target.wants/MemoryDiagnostics.service → /home/nikhil/.config/systemd/user/MemoryDiagnostics.service. 

Then Reboot. Observation: Service fails after reboot

$ systemctl --user status MemoryDiagnostics.service 
● MemoryDiagnostics.service - MemoryDiagnostics Service
     Loaded: loaded (/home/nikhil/.config/systemd/user/MemoryDiagnostics.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2021-09-02 02:34:32 CEST; 11min ago
    Process: 1549 ExecStart=/home/nikhil/opt/bin/MemoryDiagnostics (code=exited, status=1/FAILURE)
   Main PID: 1549 (code=exited, status=1/FAILURE)

Sep 02 02:34:29 X550JX systemd[1147]: Started MemoryDiagnostics Service. Sep 02 02:34:32 X550JX systemd[1147]: MemoryDiagnostics.service: Main process exited, code=exited, status=1/FAILURE Sep 02 02:34:32 X550JX systemd[1147]: MemoryDiagnostics.service: Failed with result 'exit-code'.

  • Service starts when done manually:
$ systemctl --user start MemoryDiagnostics.service 
$ systemctl --user status MemoryDiagnostics.service 
● MemoryDiagnostics.service - MemoryDiagnostics Service
     Loaded: loaded (/home/nikhil/.config/systemd/user/MemoryDiagnostics.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-09-02 02:48:13 CEST; 4s ago
   Main PID: 9941 (bash)
     CGroup: /user.slice/user-1000.slice/user@1000.service/MemoryDiagnostics.service
             ├─9941 bash /home/nikhil/opt/bin/MemoryDiagnostics
             └─9970 sleep 121

Sep 02 02:48:13 X550JX systemd[1147]: Started MemoryDiagnostics Service.


Why does the shell script fails after reboot, but works perfectly fine when started manually?

Porcupine
  • 1,892

1 Answers1

0

~/opt/bin/MemoryDiagnostics is exiting with exit code 1. That means the failure is internal to the application. It's disappointing that MemoryDiagnostics doesn't log more info to stderr so that we can help give it what it needs. But we can make some guesses:

  • If MemoryDiagnostics launches a GUI, then change WantedBy=default.target to WantedBy=graphical-session.target.
  • If MemoryDiagnistics depends on other resources, we can add After= to our [Unit] section to be sure the dependency is ready. Some dependencies which may be relevant could be:
    • After=bluetooth.target if you are also checking (or failing to check) bluetooth.
    • After=default.target if you need other services that are raised by default.target
    • Any other custom units (perhaps mounts or services) that you need ready before running this.

If you are unable to figure out which dependency needs to be ready, (or perhaps that dependency takes time to start, but doesn't use inotify) then you can always add ExecStartPre=/bin/sleep 5 to add an artificial 5s delay to starting.

This method is easier to configure, but will either add unnecessary delay or will risk starting too early.


If you do like the ExecStartPre=sleep method, and you don't necessarily need this to run immediately, just often. Then you can also consider using a *.timer to start your unit. In that case, I'd delete the [Install] section from yor service, and instead create the following timer:

# ~/.config/systemd/user/MemoryDiagnostics.timer
[Unit]
Description=MemoryDiagnostics Timer

[Timer] OnActiveSec=5s

[Install] WantedBy=default.target

Then systemctl --user enable MemoryDiagnostics.timer and let it handle executing the service.

This is effectively equivalent to the ExecStart=sleep solution above, but also gives you extra power to add/change things like:

  • OnCalendar= which will run your diagnostics on a schedule
  • RandomizedDelaySec= which will apply a random offset to the firing of the service to avoid lots of services firing at the same time.
Stewart
  • 13,677