Recent

Author Topic: Set environment variable  (Read 749 times)

Trenatos

  • Hero Member
  • *****
  • Posts: 508
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Set environment variable
« on: July 05, 2019, 12:32:09 am »
I'm working on translating a Haskell example for creating an AWS custom lambda runtime, it requires setting an environment variable but as I search I can only find references for reading environment variables and setting them for Windows.

The AWS runtimes are based on Linux.

Does anyone know how to set environment variables on Linux?

lucamar

  • Hero Member
  • *****
  • Posts: 2012
Re: Set environment variable
« Reply #1 on: July 05, 2019, 01:14:57 am »
Depends: for the current process or for running a secondary process? If the first, I don't know of any way to do it. For the second, just build an environment and pass it to the exec/fork call you're using.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Trenatos

  • Hero Member
  • *****
  • Posts: 508
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Set environment variable
« Reply #2 on: July 05, 2019, 01:35:45 am »
This would be so that another process (Not controlled by me) can read the environment variable after it's been set.

I've never had to do this from code before, has always been part of various build processes, but since AWS lambda just has to be special, I'll need to.

Kays

  • Full Member
  • ***
  • Posts: 179
  • Whasup!?
    • KaiBurghardt.de
Re: Set environment variable
« Reply #3 on: July 05, 2019, 01:44:00 am »
[…] If the first, I don't know of any way to do it. […]
Well, setenv(3), but while there’s a baseUnix.fpGetEnv() there’s no fpSetEnv. Maybe it’s on purpose, though?
Yours Sincerely
Kai Burghardt

lucamar

  • Hero Member
  • *****
  • Posts: 2012
Re: Set environment variable
« Reply #4 on: July 05, 2019, 02:27:10 am »
[…] If the first, I don't know of any way to do it. […]
Well, setenv(3), but while there’s a baseUnix.fpGetEnv() there’s no fpSetEnv. Maybe it’s on purpose, though?

That's what I meant, yes, that there is no function to do it(*). One could always add it by hand, of course, but it's not there.


(*) Well, there is one setenv(); but it's in libc, which I have always thought was there strictly for Kylix compatibility.

ETA: If you're interested in adding it yourself, something like this should suffice:
Code: Pascal  [Select]
  1. function fpsetenv (name, value: pchar; overwrite: Integer): Integer; cdecl; external clib name 'setenv';
« Last Edit: July 05, 2019, 02:38:40 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Trenatos

  • Hero Member
  • *****
  • Posts: 508
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Set environment variable
« Reply #5 on: July 05, 2019, 04:47:07 am »
After tinkering with this a bit more, it doesn't look like I need to set the environment variable, but it would give me more details.

It's on my to-do list now, thanks guys!

No frameworks or fancy setup, I'm just cross compiling from my Mac set to a Linux x64 target, then zipping and uploading the executable, the whole process takes seconds.

And here's the result of running a pure FPC custom runtime on AWS lambda.

Init Duration: 13.68 ms
Duration: 1.58 ms
Billed Duration: 100 ms
Memory Size: 128 MB
Max Memory Used: 31 MB

Max memory is for the entire container, this little runtime doesn't do anything other than echo back whatever input it got.

For anyone else using AWS lambdas, that duration is with the lambda being warm, though cold starts are crazy fast with FPC on lambda anyway.

Next up, figuring out how to talk to AWS databases.

This will be an article series once I document the process, it really is smooth and without any of the hackery.

No side-loading, no launching through Node.js, etc. Just a compiled FPC program doing its thing.

lucamar

  • Hero Member
  • *****
  • Posts: 2012
Re: Set environment variable
« Reply #6 on: July 05, 2019, 05:30:29 am »
This will be an article series once I document the process, it really is smooth and without any of the hackery.

No side-loading, no launching through Node.js, etc. Just a compiled FPC program doing its thing.

Good work! Sounds quite interesting; I've added it to my list of "things to keep a watch on". :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Trenatos

  • Hero Member
  • *****
  • Posts: 508
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Set environment variable
« Reply #7 on: July 05, 2019, 06:14:56 am »
I did take a stab at your example but immediately run into "Cannot find clib" when adding it to the uses clause.

Would it matter that I'm on a Mac and on a *nix? (Cross compiling)

lucamar

  • Hero Member
  • *****
  • Posts: 2012
Re: Set environment variable
« Reply #8 on: July 05, 2019, 10:12:08 am »
It may matter but I don't really know. I've never worked on MacOS X
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7430
Re: Set environment variable
« Reply #9 on: July 05, 2019, 10:19:46 am »
(*) Well, there is one setenv(); but it's in libc, which I have always thought was there strictly for Kylix compatibility.

Afaik setenv does not set the parents environment, only the copy that is passed to child processes.

toby

  • Jr. Member
  • **
  • Posts: 65
Re: Set environment variable
« Reply #10 on: July 25, 2019, 09:29:23 pm »
what about setting it directly in the bash shell and export it ?   using aprocess.execute ?

it is just 'c'  not clib if you are wanting libc ?

lucamar

  • Hero Member
  • *****
  • Posts: 2012
Re: Set environment variable
« Reply #11 on: July 25, 2019, 09:49:24 pm »
what about setting it directly in the bash shell and export it ?   using aprocess.execute ?

it is just 'c'  not clib if you are wanting libc ?

That sets it only for the child bash you execute with the TProcess. It dissapears at once after the export is executed and the process ends.

That is, unless there is some option or command to make it affect the parent or master environment which I don't know about. Which it might well be: I'm very far from being a bash-guru :)
« Last Edit: July 25, 2019, 09:53:15 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7430
Re: Set environment variable
« Reply #12 on: July 26, 2019, 10:24:23 am »
what about setting it directly in the bash shell and export it ?   using aprocess.execute ?

it is just 'c'  not clib if you are wanting libc ?

clib is an per platform alias. In the past there have been systems where the libc library was called differently (root, system or "glibc")