0

I'm trying to make screen start in background every time I boot. I need to have at least 4 windows, each running different command automatically. How can I do that?

2 Answers 2

1

First, start a detached screen session. Then send commands to that session using its name. Note that there's no good way to figure out whether a session is already running and/or what's going on inside it; it's up to you to make sure that you don't try to relaunch this script when it's already running (there are many ways to do that, but it's out of scope for this question).

I recommend putting your command lines into scripts, otherwise you're likely to have a hell of a time with quoting levels. Assuming you've done that, your launcher script will be something like this:

#!/bin/bash

# An arbitrary name to uniquely identify this screen session:
SESSION="my_session_name_here"

# Create the detached session, running the first command in its first window:
screen -S "${SESSION}" -d -m script1.sh

# Now add the other windows. You create new windows within screen with
# "C-a:screen" (usually mapped to some other shortcut), so that's the command: 

screen -S "${SESSION}" -X screen script2.sh
screen -S "${SESSION}" -X screen script3.sh

# You could have given the windows different titles by adding "-t title1" etc.

You can at any time attach the session to see what it's doing, as if you'd launched it interactively. However, because the command in each window is not a shell from which you launched the respective script, as soon as you Control-C out of the script, the window will close. If you want to avoid that, remember that the screen session is behaving as if you were interacting with it by hand, so you'd do something like:

screen -S my_session -d -m
# "active" window is now 0 (the only one)
screen -S my_session -X exec script1.sh
screen -S my_session -X screen
# "active" window is now 1, running the shell
screen -S my_session -X exec script2.sh
# ... etc.

However, that is somewhat brittle. If you do use this, you definitely want to have a really trivial wrapper script that only lists the screen remote commands, and have all the actual work in other scripts, even if they're trivially short.

0

A bash script can launch any program, whether command-line or GUI. All you need to do is create a line per application. If you use nohup you can exit the shell once all the applications are initiated:-

#!/bin/bash
nohup Program1Path Program1Parameters&
nohup Program2Path Program2Parameters&
nohup Program3Path Program3Parameters&
nohup Program4Path Program4Parameters&
exit

Once you have created this script and made it executable, simply add it to Startup Applications and it will run whenever you log on.

9
  • The problem is that each command outputs to a different file by using ">>". Will it still work? Also is not a program per se, more like series of commands that run until stopped.
    – MarioPL98
    Jan 30, 2019 at 22:02
  • Redirection will not affect it. I'm not clear about how you want the programs to be scheduled: do you mean something like { Prog1; Prog2; Prog3; Prog4; }&?
    – AFH
    Jan 30, 2019 at 22:42
  • A simple example of what I'm trying to achieve lets say I have: ping google.com | while read input; do echo "$(date): $input"; done Other command might be: while (true); do sensors | while read input; do echo "$(date): $input"; done; sleep 1;done;
    – MarioPL98
    Jan 31, 2019 at 6:58
  • I need few of commands like these but more complicated start at boot in different windows in one screen session.
    – MarioPL98
    Jan 31, 2019 at 7:00
  • Any of your command strings can be bracketed as above, eg { ping google.com | while read input; do echo "$(date): $input"; done; }&. The last command string can be run in foreground, without the { ... }&. Your examples do not include any GUI programs, so I don't see where the four windows come in, unless you want each command string in a separate (foreground) script, with all four scripts added as separate entries in Startup Applications.
    – AFH
    Jan 31, 2019 at 11:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .