amber-lang / amber

💎 Amber the programming language compiled to Bash
https://amber-lang.com
GNU General Public License v3.0
3.93k stars 89 forks source link

[Feature] Spawning and terminating processes #172

Open FedericoCeratto opened 5 months ago

FedericoCeratto commented 5 months ago

One of the features that keeps me in bash land is the quick (and dirty) process management for development, e.g.:

./my_service &
sleep 1
run_local_tests_against_my_service
killall my_service

Having simple unobtrusive process management in Amber would be very nice!

Ph0enixKM commented 5 months ago

@FedericoCeratto that’d be totally on the list to do. Great feature request!

Sod-Almighty commented 4 months ago

Probably a good idea to have a way to refer to the spawned process by ID rather than the brute-force killall <name>. Something like:

let pid = spawn $ some command $
...do some stuff...
kill pid
Mte90 commented 4 months ago

Probably a good idea to have a way to refer to the spawned process by ID rather than the brute-force killall <name>. Something like:

let pid = spawn $ some command $
...do some stuff...
kill pid

maybe as builtin so every command executed in Amber in the script you have the PID saved in a array (internally) so from Amber side you can kill any process you executed.

Sod-Almighty commented 4 months ago

But most commands run synchronously.

I suppose it might make sense for commands that fork themselves and run asynchronously as a daemon. docker for example, or sshd.

Perhaps this syntax?

$ sshd $ as pid1
....some code....
kill pid1

$ docker run -d somecontainer $ as pid2 failed {
  echo "oh no"
  exit 1
}
....some code....
kill pid2
Mte90 commented 4 months ago

But most commands run synchronously.

That's true, save the pid it will be very nice for the cases you mentioned.

Ph0enixKM commented 4 months ago

The as keyword is already used to cast values. How about with?

$ sshd $ with pid1
kill pid1

$ docker run -d somecontainer $ with pid2 failed {
  echo "oh no"
  exit 1
}
kill pid2
Sod-Almighty commented 4 months ago

Mm......"with" doesn't sound right. You're not creating it with a variable, you're storing a handle. I know as is used for casting, but it's not used there for casting. Why can't it be used in both contexts?

Ph0enixKM commented 4 months ago

Mm......"with" doesn't sound right. You're not creating it with a variable, you're storing a handle. I know as is used for casting, but it's not used there for casting. Why can't it be used in both contexts?

It could be used there for casting. In fact it's used in the standard library. How about just proc, process, pid keyword instead?

Sod-Almighty commented 4 months ago

Same syntax, just pid instead of as?

$ sshd $ pid x failed {
  exit 1
}
....some code....
kill x
Ph0enixKM commented 4 months ago

@b1ek @Mte90 @FedericoCeratto @CymDeveloppement do you have better ideas for this syntax?

Mte90 commented 4 months ago

I like to use just pid it is more simpler and compact.

Ph0enixKM commented 4 months ago

Also what do you all think about this syntax that would work like the Python's with:

// Runs the following command in the background:
with $ sshd $ {
  // Do some code
} failed {
  exit 1
}
// The command `sshd` is killed here once the code inside of the `with` block is completed

Alternatively:

with $ sshd $? {
  // Do some code
}
Sod-Almighty commented 4 months ago

I like it. Perhaps with an async option for programs that don't fork:

with async $ sleep 1000 $ {     // run in background
   ...do something...
}   // command is terminated