# IPC Linux Codesys Runtime SL Save Retains

**URL:** <https://www.wago.community/t/ipc-linux-codesys-runtime-sl-save-retains/2300>\
**Category:** CODESYS\
**Tags:** ipc\
**Created:** [September 19, 2025, 8:26pm UTC](https://www.wago.community/t/ipc-linux-codesys-runtime-sl-save-retains/2300 "2025-09-19T20:26:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![WagoKurt](https://sea2.discourse-cdn.com/flex016/user_avatar/www.wago.community/wagokurt/32/11_2.png) [@WagoKurt](https://www.wago.community/u/WagoKurt)\
**Post date:** [September 19, 2025, 8:26pm UTC](https://www.wago.community/t/ipc-linux-codesys-runtime-sl-save-retains/2300/1 "2025-09-19T20:26:26Z")

</div>

It seems that on the Codesys Linux Runtime SL with Version v3.5.21.2 the retain variables will save when you perform a graceful shutdown (from CLI or WMB) but not if you cycle power. One way to remedy this is to save the retains to a file and periodically save these off, and then subsequently load this file on startup. Here is example code to do this if it is needed.

```c
PROGRAM RetainSaveProgram 
VAR 
  pApp : POINTER TO APPLICATION; 
  sRetFile : STRING := ‘MyAppRetains.ret’; // file 
  nameresult : CmpErrors.RTS_IEC_RESULT; 
  res : DINT; 
  TSave : TON; 
  xFirstScan : BOOL := TRUE;
END_VAR

```

```c
IF xFirstScan THEN
  xLoad := TRUE;
END_IF

IF xSave THEN
  pApp := CmpApp.AppGetCurrent(pResult := ADR(result));
  IF pApp <> 0 THEN
    result := CmpApp.AppStoreRetainsInFile(pApp, sRetFile);
    IF result = 0 THEN
      xSave := FALSE;
    END_IF
  END_IF;
END_IF;
TSave(IN := NOT TSave.Q, PT := T#300S);
IF TSave.Q THEN
  xSave := TRUE;
END_IF

```

```c
IF xLoad THEN
  pApp := CmpApp.AppGetCurrent(pResult := ADR(res));
  IF pApp <> 0 THEN
    res := CmpApp.AppRestoreRetainsFromFile(pApp, sRetFile);
    IF res = 0 THEN
       xLoad := FALSE;
    END_IF
  END_IF;
END_IF;
xFirstScan := FALSE;

```

Here are the two libraries you need to add to your project.

![Screenshot 2025-09-19 at 7.31.25 AM](https://us1.discourse-cdn.com/flex016/uploads/wago/optimized/2X/6/6fc57cf4b5e768e708698fe4d6fc443e897fc7fa_2_690x38.png)
