33

Is it possible to launch a command or Bash script exit terminal and NOT interrupt command?

My solution was to run cron at a specific time of day, but I'm sure there is something easier.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
  • Duplicate: [How do I detach a process from Terminal, entirely?](http://superuser.com/questions/178587/how-do-i-detach-a-process-from-terminal-entirely/178592#178592) – slhck Jul 13 '12 at 12:19

4 Answers4

64

To avoid exit signals propagating to child processes of the terminal and shell, run the command with nohup, i.e.:

nohup cmd &

To ignore all program output and avoid the nohup.out file, you can redirect stdout and stderr to /dev/null like this (with bash):

nohup cmd &> /dev/null &
Thor
  • 6,419
  • 1
  • 36
  • 42
  • 3
    This is the correct answer. Without nohup, the started process is still considered a "child" of the terminal process and thus terminated if the terminal is closed. – Izzy Jul 13 '12 at 13:04
  • 4
    `cmd & disown` works too, since the `&` is treated like a `;` command separator. The disown command removes the connection between the bash shell session and the backgrounded command. – lornix Jul 14 '12 at 20:34
  • 2
    zsh has a shorthand for this: `cmd &|`. – Thor Oct 26 '12 at 09:24
  • For this to work, I had to change a preference in Terminal.app. Under Profiles, select your active profile, then Shell>When the shell exits>Close if the shell exited cleanly – antoine May 09 '17 at 01:12
  • 1
    `nohup` creates a file when it runs, for me, so I added `> /dev/null` to ignore the output. – Andrew May 10 '23 at 21:42
  • For some reason, neither `nohup` nor `& disown` are working for me unless I put `read` on the next line, to require me to press Enter to close the terminal. If I just have a `.sh` file without `read`, the terminal opens and closes real fast, without launching the application. (Of course, I can still close the terminal and the application continues running.) Why is that, and how do you fix it? Thanks – Andrew May 10 '23 at 21:46
  • @Andrew: hard to say without more information, shell, terminal, window manager, operating system etc. You should probably post a new question including said information – Thor May 11 '23 at 07:51
  • @Thor Eh, it's pretty strongly affiliated with this problem + answer, and likely to be something many others encounter as well. It's the default Terminal application in Linux Mint Cinnamon (Ubuntu-based). I would be surprised if it's related to any of that though; are you suggesting that maybe the Terminal application is somehow closing a `nohup`/`disown`-ed application? – Andrew May 11 '23 at 17:36
  • Also your guys' reasons for rejecting the edit are BS, but go ahead and keep your answer incomplete. – Andrew May 11 '23 at 17:37
  • 1
    @Andrew: did you see antoines comment above? It is sometimes related to the terminal, yes. With regards to your edit, ignoring program output might be fine in some cases, I've added it as an alternative – Thor May 12 '23 at 09:36
6

Using screen:

screen -S <session name> -d -m <your command>

after that you can quit the terminal, also you can reattach to it by:

screen -r <session name>

More info: reference

Bruce
  • 161
  • 1
  • 2
6

If you want to run a specific command or file every second or so in the background after exiting the terminal you could try this easy little thing;

nohup watch -n5 'bash script.sh' &

That would run scipt.sh every 5 seconds.

lejahmie
  • 169
  • 1
  • 3
  • 1
    "nohup" is already covered by the accepted answer. And I'm pretty sure the asker mentioned cron because they were using it to say "Run this command a few seconds in the future", not to run it periodically. – David Richerby Aug 06 '15 at 21:01
  • 1
    My answer don't make things worse. Do see the point of down vote. Neural would be sufficient. – lejahmie Aug 06 '15 at 21:41
3

Put a "&" character after your command.

e.g:

/home/your/script.sh &
Flinth
  • 360
  • 1
  • 6
  • 9
    in this case, when the terminal is closed, so is the process started by `/home/your/script.sh` -- as it was not detached from its "parent", but just "backgrounded". Use `nohup` to detach it for real. – Izzy Jul 13 '12 at 13:05
  • My bad, I didn't knew about that, but when I tested on my Debian, the command kept executing after closing the shell which launched it :/ – Flinth Jul 13 '12 at 14:03
  • 1
    I'm not sure where exactly the backgrounded process is attached to and when. But if you e.g. log in to a remote machine, it is definitely stopped as soon as you log out (except if it [daemonized](http://en.wikipedia.org/wiki/Daemon_%28Unix%29) itself). So to be 100% sure, you rather use `nohup` -- which also logs all (now invisible) output into a file called `nohup.out` located in the directory you started the command from. – Izzy Jul 13 '12 at 14:10
  • Okay, well thank you for your explanations ^^ – Flinth Jul 13 '12 at 14:18
  • This answer might work ok when combined with the `jobs` command. Quite often i want to be able to get back to the shell process after backgrounding it. – djangofan Dec 07 '18 at 19:17
  • Yeah, this answer was pretty limited. Actually, now I often use `screen` command for this. Type `screen` to open a new "session" run your things and close the terminal, the command will still be running in background. If you want to come back to this session just type `screen -r` – Flinth Dec 08 '18 at 23:27
  • Background processes are not directly killed when the shell exits. But they get the HUP (hangup) signal and may terminate or not. The nohup command intercepts the hangup signal so the program itself doesn't notice that the shell process ended. – allo Feb 08 '23 at 12:53