How do I run one-off dynos with --exit-code on a Container app in a Private Space?

Issue

I tried to run a one-off dyno and fetch the exit code. However, the Heroku CLI command heroku run --exit-code failed with a message like

FATA exec: "command;": executable file not found in $PATH 
 ▸    No exit code returned

Resolution

Create a script file like below and specify it as the ENTRYPOINT in the Dockerfile:

#!/usr/bin/env ruby

HEROKU_EXIT_STATUS_ARG = "\uFFFF heroku-command-exit-status: $?"
HEROKU_EXIT_STATUS_CMD = "; echo \"#{HEROKU_EXIT_STATUS_ARG}\""

argv = ARGV.dup

heroku_exit_status = case
when argv.size > 2 && argv[-3].end_with?(';') && argv[-2] == 'echo' && argv[-1] == HEROKU_EXIT_STATUS_ARG
  argv.pop # HEROKU_EXIT_STATUS_ARG
  argv.pop # "echo"
  argv[-1] = argv[-1].slice(0...-1) # trailing semicolon
  true
when argv.any? && argv[-1].end_with?(HEROKU_EXIT_STATUS_CMD)
  argv[-1] = argv[-1].slice(0...-HEROKU_EXIT_STATUS_CMD.size)
  true
else
  false
end

if heroku_exit_status
  system(*argv)
  puts HEROKU_EXIT_STATUS_ARG.sub('$?', $?.exitstatus.to_s)
  exit! $?.exitstatus
else
  exec(*argv)
end

The Heroku CLI command fails because of the mismatch in how the Heroku CLI command constructs a sequence of commands to be executed and sends the exit status back and in how the platform interprets the command to be executed on the one-off dyno. The script works around the mismatch.

Ask on Stack Overflow

Engage with a community of passionate experts to get the answers you need

Ask on Stack Overflow

Heroku Support

Create a support ticket and our support experts will get back to you

Contact Heroku Support