0

I am trying to write a short script that opens a new terminal window, executes a variable that starts a game server, and logs the output of that new window.

The server sends a huge amount of spam in the console that is entirely useless, so I also wanted to append -q so the window is hidden.

My end goal is to look through the log for "Connected to Steam servers" using grep and report it in the main console. Currently I have the following:

$server="$game_path/server_executable"
gnome-terminal -- $server

Anything I add to gnome-terminal breaks the code. I have tried using gnome-terminal -- "$server 2>&1 | tee server.log" to the gnome-terminal line but it breaks. I've also tried gnome-terminal -- "@server >> server.log" but that fails too. I can't figure out the answer to get the new shell running the server command and exporting the output to a log file.

Running on Debian 12.

Stew
  • 1

1 Answers1

0

The better approach is to use something like screen or tmux so that the service continues to run if you close the terminal window. However, that's not what you've asked...

gnome-terminal -- "$server 2>&1 | tee server.log"

This is nearly right. Let's check the documentation (man gnome-terminal)

gnome-terminal [OPTION…] [-- PROGRAM [ARG…]]

And, somewhat confusingly,

--command, -e=COMMAND Split the argument to this option into a program and arguments in the same way a shell would, and execute the resulting command-line inside the terminal. This option is deprecated. Instead, use -- to terminate the options, and put the program and arguments to execute after it: for example, instead of gnome-terminal -e "python3 -q", prefer to use gnome-terminal -- python3 -q.

All well so far. But,

Note that the COMMAND is not run via a shell: it is split into words and executed as a program. If shell syntax is required, use the form gnome-terminal -- sh -c '...'

This last paragraph is the key: you cannot use | and > directly with gnome-terminal; you have to invoke a shell to interpret it:

$server="$game_path/server_executable"
gnome-terminal -- sh -c "$server 2>&1 | tee server.log"

Personally I'd prefer to use screen, with or without gnome-terminal:

gnome-terminal -- screen sh -c 'game_path/server_executable 2>&1 | tee server.log'

Or if you don't really want the gnome-terminal part and it was just a means to starting your game server,

screen -md sh -c 'game_path/server_executable 2>&1 | tee server.log'

Lots of help here on U&L for manipulating screen sessions, for example my answer to Pause a process and bring it back to foreground in a separate screen in Ubuntu

Chris Davies
  • 116,213
  • 16
  • 160
  • 287