0

Is it possible to get the exit code of a previous run of systemd's process on re-run?

That is, suppose I have the following C++ code:

int main(int argc, char **argv)
{
  if (argc > 2)
  {
    std::string opt = argv[1];
    std::string val = argv[2];
    if (arg == "--exit-code" && val == "42") 
    {
       std::cout << "Previous exit code is: " << val << std::endl;
       return 0;
    }
  }

  std::cout << "This is the first run" << std::endl;
  return 42;
}

And systemd's configuration:

[Unit]
Description=Fails first then succeeds
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=./a.out
Restart=on-failure # --exit-code <-- how to pass the previous exit code here?

[Install]
WantedBy=multi-user.target

How does one get the exit code of the previous run and pass it to subsequent attempts of the same process?

codingedward
  • 456
  • 5
  • 17

1 Answers1

0

I would suggest calling systemctl show <service name> and looking for ExecMainCode=<exit code>. Alternatively, you could read $EXIT_CODE in the ExecStopPost and store it somewhere for later use. I don't know any way to pass it as a command line argument, other than writing a wrapper script or changing ExecStart (note that $EXIT_CODE won't be available in ExecStart).

Hack5
  • 3,244
  • 17
  • 37