www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Execute the Shell command and continue executing the algorithm

reply Alexander Zhirov <azhirov1991 gmail.com> writes:
I want to run a command in the background during the execution of 
the algorithm, and without waiting for its actual execution, 
because it is "infinite", while continuing the execution of the 
algorithm and then, knowing the ID of the previously launched 
command, kill the process. So far I have done so:

```d
// Here a long program is launched, as an example `sleep`
executeShell("(sleep 10000 && echo \"SLEEP\" >> log) &");

while (!interrupted)
{
     // some algorithm is executed here, for example `echo`
     executeShell("(echo \"OK\" >> log) &");
     if (here is my condition termination of the program)
     {
         // Here is the termination of the running program
     }
     Thread.sleep(1.seconds);
}
```

How to organize such an algorithm correctly?
May 30 2022
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/30/22 04:18, Alexander Zhirov wrote:

 I want to run a command in the background
The closest is spawnShell: import std.stdio; import std.process; import core.thread; void main() { auto pid = spawnShell(`(sleep 10000 & echo SLEEP >> log)`); Thread.sleep(5.seconds); kill(pid); writeln("Terminated with ", wait(pid)); } I am not good at shell scripting but I had to change your && to & to see anything in log. As std.process documentation explains, the value returned by wait() (and more) are platform dependent. Ali
May 30 2022
parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 2022-05-30 15:25, Ali Çehreli wrote:
 On 5/30/22 04:18, Alexander Zhirov wrote:
 
  > I want to run a command in the background
 
 The closest is spawnShell:
 
 import std.stdio;
 import std.process;
 import core.thread;
 
 void main() {
    auto pid = spawnShell(`(sleep 10000 & echo SLEEP >> log)`);
    Thread.sleep(5.seconds);
    kill(pid);
    writeln("Terminated with ", wait(pid));
 }
 
 I am not good at shell scripting but I had to change your && to & to see 
 anything in log.
I think this runs sleep 10000 in the background and emits the echo directly.
 As std.process documentation explains, the value returned by wait() (and 
 more) are platform dependent.
 
 Ali
 
May 31 2022
prev sibling next sibling parent Krzysztof =?UTF-8?B?SmFqZcWbbmljYQ==?= <krzysztof.jajesnica gmail.com> writes:
On Monday, 30 May 2022 at 11:18:42 UTC, Alexander Zhirov wrote:
 I want to run a command in the background during the execution 
 of the algorithm, and without waiting for its actual execution, 
 because it is "infinite", while continuing the execution of the 
 algorithm and then, knowing the ID of the previously launched 
 command, kill the process. So far I have done so:

 ```d
 // Here a long program is launched, as an example `sleep`
 executeShell("(sleep 10000 && echo \"SLEEP\" >> log) &");

 while (!interrupted)
 {
     // some algorithm is executed here, for example `echo`
     executeShell("(echo \"OK\" >> log) &");
     if (here is my condition termination of the program)
     {
         // Here is the termination of the running program
     }
     Thread.sleep(1.seconds);
 }
 ```

 How to organize such an algorithm correctly?
You could use [`spawnShell`](https://dlang.org/phobos/std_process.html#spawnShell) instead of `executeShell` to spawn the long-running process. `spawnShell` will return the PID of the spawned process, which you can later use to kill it with the `kill` function. ```d import core.stdc.signal : SIGINT; import std.process; /* note: with spawnShell you don't need & at the end of command, because spawnShell doesn't wait for spawned process to complete */ Pid pid = spawnShell("(sleep 10000 && echo \"SLEEP\" >> log)"); while (!interrupted) { // some algorithm is executed here, for example `echo` executeShell("(echo \"OK\" >> log) &"); if (here is my condition termination of the program) { /* Kill the previously spawned process using SIGINT signal */ kill(pid, SIGINT); /* Wait for the killed process to shutdown */ wait(pid); } Thread.sleep(1.seconds); } ```
May 30 2022
prev sibling parent reply frame <frame86 live.com> writes:
On Monday, 30 May 2022 at 11:18:42 UTC, Alexander Zhirov wrote:

     if (here is my condition termination of the program)
OT: Wouldn't it be great to have ArnoldC support? ;-)
May 31 2022
parent Jack <jckj33 gmail.com> writes:
On Tuesday, 31 May 2022 at 15:29:16 UTC, frame wrote:
 On Monday, 30 May 2022 at 11:18:42 UTC, Alexander Zhirov wrote:

     if (here is my condition termination of the program)
OT: Wouldn't it be great to have ArnoldC support? ;-)
i'm pretty sure the terminattor is more efficient than kill -9 lmaof
May 31 2022