12

This is what I'm running:

alexandma@ALEXANDMA-1-MBP ./command.sh &
[2] 30374
alexandma@ALEXANDMA-1-MBP
[2]  + suspended (tty output)  ./command.sh

I don't want it to start suspended, I want it to keep running in the background. I'm going to be running a bunch of these in a loop, so I need something that will work that way.

How can I keep it running?

Melebius
  • 768
  • 1
  • 6
  • 18
Matt Alexander
  • 725
  • 1
  • 7
  • 20

5 Answers5

15

Adding a detail to Anthon's explanation:

It is not always the case that a writing background process is stopped. This depends on the terminal setting tostop.

stty tostop
stty -tostop

can be used to toggle this setting. So if you want a background process to write to "another process's" terminal then you can keep it running but don't need tmux, screen or similar.

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
8

It stops because of the reason given: it tries to output to tty. You can try to redirect the output if ./command.sh supports that, or run the command in a tmux or screen window of it's own. E.g.

 tmux new-window -n "window name" ./command.sh

and then view the list of windows created with tmux list-windows and attach to tmux with tmux attach.

That way the program will still wait for input/output to happen, but you can easily provide input once you go to the appropriate window and the output will just be captured without any activity.

Anthon
  • 78,313
  • 42
  • 165
  • 222
6

The easiest way to do this would be to execute the command as follows:

 nohup ./command.sh </dev/null &

I include the nohup in the event you are using a SH varient (not CSH varient) and would be terminating your session.

mdpc
  • 6,736
  • 3
  • 32
  • 46
  • 1
    This works in Zsh, unlike the [`stty` solution](https://unix.stackexchange.com/a/166486/54675). – Melebius Sep 03 '19 at 07:05
  • @Melebius Can you explain why `stty` doesn't work in `zsh`? – Barmar Sep 03 '19 at 13:37
  • @Barmar Unfortunately not. I don’t know anything more about the relations between `stty` and the shell. I just tried these two solutions and `nohup` worked for me. – Melebius Sep 04 '19 at 08:25
  • @Melebius Maybe related: https://unix.stackexchange.com/questions/343088/what-is-the-equivalent-of-stty-echo-for-zsh – Barmar Sep 04 '19 at 14:21
2

Install screen.

Once you've installed screen. Run it : # screen (you'll have a welcome screen when you first start it) Run your command : command.sh

The press Ctrl+a and then d.

It's gonna detach the screen launched on the server and keep your command running.

In order to go back to this screen, type :

screen -r

HalosGhost
  • 4,732
  • 10
  • 33
  • 41
Maxime D
  • 21
  • 1
1

You might get this error if your shell is zsh and the script is calling sudo and you don't have valid cached credentials (you did not run sudo before, or the previous credentials expired) or su and you have to input a password.

If you don't need to keep the output like other answers have mentioned (like running your script without & in tmux or screen), here are some possible solutions for this:

  • disable the tty_tickets or set the timestamp_type to global in your sudoers file (so that you don't have to enter your password in all the terminals) and/or increase the value of timestamp_timeout (this has some serious security implications).
  • run a simple command with sudo before running the script so that sudo will have cached credentials when encountered in your script.
  • change your script so it does not call sudo or su.
Victor Dodon
  • 111
  • 3
  • What works fine in bash inside an alias doesn't work in zsh. I just want openvpn to keep running and in bash I did it with alias ovp='sudo openvpn myconfig &'. Can't find a way to make the same work in zsh. – DimiDak May 06 '21 at 22:44
  • But you are right saying that this has to do with sudo and cached creds. – DimiDak May 06 '21 at 22:45