# Stop with non-zero exit status

**URL:** <https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062>\
**Category:** Uncategorized\
**Created:** [October 13, 2021, 9:47am UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062 "2021-10-13T09:47:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)\
**Post date:** [October 13, 2021, 9:47am UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/1 "2021-10-13T09:47:29Z")

</div>

Searching for this question I found this SO thread, and the suggestion is to use `stop N`. Is that a standard way to do it? Seems from the discussion that sometimes the number gets printed, or not, depending on the OS or the compiler.

What I want is a flag to allow easier handling of errors from the output of a Fortran program, using an upper level script.

> <https://stackoverflow.com/questions/17392119/stop-fortran-program-with-non-zero-exit-status>

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 13, 2021, 9:58am UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/2 "2021-10-13T09:58:58Z")

</div>

The Fortran 2018 standard says:

> If the stop-code is an integer, it is recommended that the value be used as the process exit status, if the processor supports that concept.

> If QUIET= is omitted or the scalar-logical-expr has the value false:  
> • …  
> • if a stop code is specified, it is **recommended** that it be made available by formatted output to the same unit.

(ERROR\_UNIT, I think)

Note there is also the `ERROR STOP` statement:

> Execution of a STOP statement initiates normal termination of execution. Execution of an ERROR STOP statement initiates error termination of execution.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 13, 2021, 11:41am UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/3 "2021-10-13T11:41:45Z")

</div>

> **[How does Fortran stderr stop return code work?](https://www.scivision.dev/fortran-stderr-stop-return-code/)**
>
> From 1956 Fortran I through Fortran 2018 standard, an integer stop value is generally returned. In Fortran 2018, variable error stop is finally allowed.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 13, 2021, 12:07pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/4 "2021-10-13T12:07:29Z")

</div>

A process exit code has not always been a thing, and thus the Fortran standard cannot mandate anything about it. I will note that I have had fairly good luck with `stop n` passing `n` as the exit code on all platforms and compilers that I care about. I will note that detecting it on Windows is occasionally a bit finicky, I suspect more due to the idiosyncrasies of the `cmd` and/or `batch` shells than problems in the Fortran compilers or run-time systems.

---

<div class="post-metadata">

**Author:** ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)\
**Post date:** [October 13, 2021, 12:34pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/5 "2021-10-13T12:34:34Z")

</div>

When using `stop 1` (for example), this becomes output on the program termination, like:

```auto
ERROR: Invalid input parameter.
STOP 1

```

Is it possible to suppress that printing?

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [October 13, 2021, 1:08pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/6 "2021-10-13T13:08:06Z")

</div>

> STOP 1, QUIET=.TRUE.

is the Standard’s way

---

<div class="post-metadata">

**Author:** ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)\
**Post date:** [October 13, 2021, 1:45pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/7 "2021-10-13T13:45:42Z")

</div>

That is perfect, thank you!

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 13, 2021, 2:43pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/8 "2021-10-13T14:43:07Z")

</div>

By the way, that blog has many interesting short and recent articles in the Fortran category:

> **[fortran](https://www.scivision.dev/categories/fortran/)**
>
> fortran categories taxonomy

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 13, 2021, 3:40pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/9 "2021-10-13T15:40:12Z")

</div>

I have also been using `stop 1`, that seemed to work everywhere. Recently I switched to `error stop`.

Regarding the `quiet` part, with GFortran 11.0.1 I get:

```auto
$ gfortran a.f90
a.f90:1:7:

    1 | STOP 1, QUIET=.TRUE.
      | 1
Error: Syntax error in STOP statement at (1)

```

So I don’t know if I am doing anything wrong.

---

<div class="post-metadata">

**Author:** ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)\
**Post date:** [October 13, 2021, 4:48pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/10 "2021-10-13T16:48:22Z")

</div>

It seems that it is feature not implemented yet?

[https://gcc.gnu.org/bugzilla/show\_bug.cgi?id=84519](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84519)

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 13, 2021, 4:56pm UTC](https://fortran-lang.discourse.group/t/stop-with-non-zero-exit-status/2062/11 "2021-10-13T16:56:51Z")

</div>

> [@certik](#):
>
> `STOP 1, QUIET=.TRUE.`

Looks like GFortran’s parser cannot parse it yet. In LFortran we can parse it:

```console
$ cat b.f90 
program test
STOP 1, QUIET=.TRUE.
end
$ lfortran --show-ast b.f90 
(TranslationUnit [(Program test () [] [] [] [(Stop 0 1 (Logical .true.) ())] [])])

```

But the `quiet=.true.` gets lost by the time it gets to ASR:

```console
$ lfortran --show-asr b.f90
(TranslationUnit (SymbolTable 1 {test: (Program (SymbolTable 2 {}) test [] [(Stop (ConstantInteger 1 (Integer 4 [])))])}) [])

```

And by the time it gets to LLVM, also the non-zero exit code gets lost:

```auto
$ lfortran --show-llvm b.f90
; ModuleID = 'LFortran'
source_filename = "LFortran"

@0 = private unnamed_addr constant [6 x i8] c"STOP\0A\00", align 1

define i32 @main() {
.entry:
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @0, i32 0, i32 0))
  call void @exit(i32 0)
  ret i32 0
}

declare void @_lfortran_printf(i8*, ...)

declare void @exit(i32)

```

We need to fix it: [Implement nonzero exit code and `quiet` in STOP (#597) · Issues · lfortran / lfortran · GitLab](https://gitlab.com/lfortran/lfortran/-/issues/597)
