2

I ran a foreground command that's taking too long. I moved it to the background using "Ctrl-Z" then "bg". Now how do I redirect it's standard and error out to another file. I tried the "reredirect" command, but that command itself run in the foreground and "Ctrl-Z" didn't work on it.

Also, once I redirect the output, would I just run "disown JOBID" to prevent the process for exiting once I leave the terminal?

user994165
  • 245
  • 4
  • 11

1 Answers1

0

To run a job in background, you could directly append & to the end of your command, this is the same as pressing ctrl+z and running bg.

As your question deals about a long command, you should probably rather rely on nohup or screen, so that the encapsulated command continues running when you leave your terminal.

Using nohup

Example:

nohup my_long_command
# Leave your terminal (ctrl+D), my_long_command is still running

Remarks:

Using screen

  1. Install scren
sudo apt install screen
  1. Run a new screen session to run your long command:
# Open a "foo" screen
screen -S foo
my_long_command
# Detach this "foo" screen
screen -d foo
# Leave your terminal (ctrl+D), the screen is still running
  1. When you want to resume your screen:
# List running screens, "foo" will appear
screen -ls
# Reattach the "foo" screen
screen -r foo
  1. If you don't need the "foo" screen anymore:
# Close a screen
screen -XS foo quit

See also geeksforgeeks.org screen: screen command with example