Run Bash script in background and exit terminal

20

9

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.

user98645

Posted 2012-07-13T12:03:01.327

Reputation:

Duplicate: How do I detach a process from Terminal, entirely?

– slhck – 2012-07-13T12:19:10.097

Answers

46

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

nohup cmd &

Thor

Posted 2012-07-13T12:03:01.327

Reputation: 5 178

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 – 2017-05-09T01:12:17.970

1This 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 – 2012-07-13T13:04:07.290

3cmd & 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 – 2012-07-14T20:34:35.263

2zsh has a shorthand for this: cmd &|. – Thor – 2012-10-26T09:24:11.920

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

Posted 2012-07-13T12:03:01.327

Reputation: 161

4

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.

jamietelin

Posted 2012-07-13T12:03:01.327

Reputation: 149

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 – 2015-08-06T21:01:29.547

1My answer don't make things worse. Do see the point of down vote. Neural would be sufficient. – jamietelin – 2015-08-06T21:41:30.777

3

Put a "&" character after your command.

e.g:

/home/your/script.sh &

Flinth

Posted 2012-07-13T12:03:01.327

Reputation: 342

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 – 2018-12-07T19:17:21.090

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 – 2018-12-08T23:27:54.257

7in 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 – 2012-07-13T13:05:51.050

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 – 2012-07-13T14:03:31.847

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 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 – 2012-07-13T14:10:49.683

Okay, well thank you for your explanations ^^ – Flinth – 2012-07-13T14:18:27.573