You can wrap your command in an extra bash
(or your actual shell) call and make redirection there:
$ screen -dm bash -c 'echo hello > ./out'
$ cat ./out
hello
From the man screen
:
-d -m Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
Naming a session
It may be suitable to name your background jobs to disambiguate them in the screen -ls
and to attach if neccessary:
$ screen -S mysession -dm sleep 20
$ screen -ls
8431.mysession (10/04/2021 12:47:58 PM) (Detached)
attach
$ screen -r mysession
How to pass arguments
Parameterization may be a hassle though. The arguments being passed are fed into bash
starting with $0
-- note the ignore_me_arg
:
# note the outer-most quotes to be single -- we don't want them to
# be expanded on the caller's side, but on the callee's one
$ screen -dm bash -c 'echo $1 > ./out' ignore_me_arg hello
$ cat ./out
hello
As a final example, let's say you have a program super_script
that does some stuff and prints all its arguments to stdout
(as echo
would do). You want to call it inside screen
, make a redirection, and do all that jazz with different sets of arguments. Then this should do:
$ screen -dm bash -c 'super_script "$@" > ./out.1' arg0 arg1
$ screen -dm bash -c 'super_script "$@" > ./out.2' arg0 arg1 arg2
... wait for screen's to finish
$ cat out.1
arg1
$ cat out.2
arg1
arg2
screen /dev/ttyUSB0
? – deostroll Jul 28 '19 at 02:36stuff
in the command? I tried to execute the command exactly and nothing happened. No log file was created and when I activated workspace there was no any output there. I tried it in macOS's Terminal. – dmitry1100 Apr 21 '20 at 07:37exec > >(tee -i logfile.log)
andexec 2>&1
into it. – dmitry1100 Apr 22 '20 at 23:55