Asynchronous shell commands

Honestly, I cannot believe that I cannot find a solution for this online. I came across several things that seem similar, but nothing that really does what I want ...

I am trying to use a shell script to run a command. I don't care if / when / how / why it ends. I want the process to start and run, but I want to be able to immediately return to my shell ...

Hope this is clear enough, I probably just missed something incredibly stupid

+58
linux bash asynchronous shell ubuntu
03 Mar. '10 at 1:05
source share
4 answers

You can simply run the script in the background:

$ myscript & 

Note that this is different than placing & inside your script, which probably won't do what you want.

+69
Mar 03 2018-10-03T00:
source share
 nohup cmd 

not delayed when closing the terminal. default output is nohup.out

You can combine this with a background,

 nohup cmd & 

and get rid of the conclusion,

 nohup cmd > /dev/null 2>&1 & 

you can also disown execute the command. type cmd , Ctrl-Z , bg , disown

+27
Mar 03 '10 at 1:14
source share

Alternatively, after starting the program, you can press Ctrl-Z, which stops your program, and then type

B.G.

which puts your last stopped program in the background. (Useful if you started something without "&" and still want it in backgroung without restarting it)

+21
Mar 03 '10 at 1:11
source share

Everyone just forgot disown . So here is the summary:

  • & puts the job in the background.

    • Causes the block to block an attempt to read input and
    • Makes a shell without waiting for its completion.
  • disown removes this process from shell job management, but it still leaves it connected to the terminal.

    • One of the results is that the shell will not send it SIGHUP (If the shell receives SIGHUP , it also sends SIGHUP to the process, which usually leads to the termination of the process).
    • And, obviously, it can only be applied to background jobs (because you cannot enter it when starting the foreground).
  • nohup disconnects the process from the terminal, redirects its output to nohup.out and protects it from SIGHUP .

    • The process will not receive the sent SIGHUP .
    • It is completely independent of task management and can, in principle, also be used to work in the foreground (although this is not very useful).
    • Commonly used with & (as a background job).
+18
May 30 '16 at 18:37
source share



All Articles