Get exit code of a background process

前端 未结 12 1927
别跟我提以往
别跟我提以往 2020-11-22 17:14

I have a command CMD called from my main bourne shell script that takes forever.

I want to modify the script as follows:

  1. Run the command CMD in parallel
12条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 17:47

    With this method, your script doesnt have to wait for the background process, you will only have to monitor a temporary file for the exit status.

    FUNCmyCmd() { sleep 3;return 6; };
    
    export retFile=$(mktemp); 
    FUNCexecAndWait() { FUNCmyCmd;echo $? >$retFile; }; 
    FUNCexecAndWait&
    

    now, your script can do anything else while you just have to keep monitoring the contents of retFile (it can also contain any other information you want like the exit time).

    PS.: btw, I coded thinking in bash

提交回复
热议问题