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?

4
  • you can use cron , it you wan to run after some interval
    – Hackaholic
    Nov 6, 2014 at 21:12
  • I assume you mean cron. How would that be implemented to solve this problem? Nov 6, 2014 at 21:13
  • I've explained once here how to use screen for the job: serverfault.com/questions/466129/restore-ssh-session/… Nov 6, 2014 at 21:15
  • If you don't care about the stdout and stderr you can just redirect them to /dev/null like ./cmd.sh &> /dev/null &
    – 111---
    Nov 6, 2014 at 21:16

5 Answers 5

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.

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.

4
  • Don't forget nohup
    – Sukima
    Nov 7, 2014 at 4:51
  • When using tmux that is not necessary
    – Anthon
    Nov 7, 2014 at 5:27
  • You answered my question with "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" Nov 7, 2014 at 12:17
  • 1
    @MattAlexander your comment seems incomplete
    – Anthon
    Nov 7, 2014 at 12:18
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.

4
  • 1
    This works in Zsh, unlike the stty solution.
    – Melebius
    Sep 3, 2019 at 7:05
  • @Melebius Can you explain why stty doesn't work in zsh?
    – Barmar
    Sep 3, 2019 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 4, 2019 at 8:25
  • @Melebius Maybe related: unix.stackexchange.com/questions/343088/…
    – Barmar
    Sep 4, 2019 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

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.
2
  • 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 6, 2021 at 22:44
  • But you are right saying that this has to do with sudo and cached creds.
    – DimiDak
    May 6, 2021 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.