Getting the exit code of a failed systemd service

11,530

systemctl status does have what you're looking for:

$ systemctl status openproject-web-1.service
● openproject-web-1.service
   Loaded: loaded (/etc/systemd/system/openproject-web-1.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2020-05-25 08:17:17 CEST; 1 day 4h ago
  Process: 969 ExecStart=/usr/bin/openproject run web (code=exited, status=203/EXEC)
 Main PID: 969 (code=exited, status=203/EXEC)

You see here under Main PID: you have (code=exited, status=203/EXEC). That 203 is the exit code.

The exit codes are up to the specific application, but there are conventions. 0 is successful exit, 1-255 is abnormal exit, 256+ is out of range. The posix standard has a few special cases. But you'll want to look at the documentation of the application for anything more than that.

For example, grep(1) says:

...the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred

As JdePB describes below, systemd may set some exit codes itself in the range of 200 to 242. In the example above we have exit code 203 which means the actual execution failed (maybe the file was not found or was not marked as executable).

Share:
11,530
Parsa Mousavi
Author by

Parsa Mousavi

Currently in 2020 I'm studying computer science and I have about 4 years of computer programming experience.I'm interested in linux , security , network , databases , machine learning and also doing research in different computer and programming fields .No job experience but I'm actively looking for a remote job to earn money and to develop my skills.

Updated on September 18, 2022

Comments

  • Parsa Mousavi
    Parsa Mousavi almost 2 years

    How to get the exit-code of a failed systemd service ?

    service some_service status prints the following :

     Active: failed (Result: exit-code)
    

    But what is that exit-code ?

    And are those exit-codes standard or do they have different meanings for different services?

  • JdeBP
    JdeBP about 4 years
    Read unix.stackexchange.com/a/458655/5132 first, especially in this case.