How to setup screen using a bash script
Try the following, it will open a new screen session with a bash which will change the directory and open a new bash with this directory as current:
screen -S work bash -c 'cd myDir && exec bash'
Adding -d -m
to run it in detached mode. And after reattaching you will be in myDir
:
screen -S work -d -m bash -c 'cd myDir && exec bash'
Better solution
The following code will create a detached screen with 3 screens each running myCommand1/2/3
in directory myDir1/2/3
.
cd myDir1
screen -S work -d -m
screen -S work -X exec myCommand1
screen -S work -X chdir myDir2
screen -S work -X screen
screen -S work -X exec myCommand2
screen -S work -X chdir myDir3
screen -S work -X screen
screen -S work -X exec myCommand3
cd -
Note the last cd -
that will return you back to your original working directory.
Finally just use screen -r work
to attach your running screen session.
How to start multiple screen sessions from bash script
Resolved that problem with launching the app in the screen after initiation first:
screen -dmS launcher
screen -S launcher -X screen "/home/test/Launcher.exe"
screen -dmS server
screen -S server -X screen "/home/test/server/server/Server.exe"
Using screen in bash script
Your script, as in your example, will get executed by your sell, not the one in the screen. You need to tell the running screen to read a file and execute it - that's what the -X option is for.
Try
tempfile=$(mktemp)
cat > $tempfile <<EOF
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
EOF
screen -X readbuf $tempfile
screen -X paste .
rm -f $tempfile
You can leave screen running in a 2nd terminal session to see what happens.
View output of a script using screen
So I finally figured it out. I ended up creating just one screen session and multiple windows within the session.
#!/bin/sh
Lum_Types=("Window1" "Window2" "Window3" )
Windowname="Screen_session"
screen -mdS $Windowname
for Lum in ${Lum_Types[@]}
do
screen -S $Windowname -X screen -t $Lum
screen -S $Windowname -p $Lum -X stuff $"python /Path/to/file arg1 arg2"
screen -S $Windowname -p $Lum -X stuff $'\n'
done
One key part was to add the $
after the -X stuff
in order to pass the string to the screen session and windows.
– Victor
GNU Screen: Open window and send command from loop in Bash script
At the moment (after some help from @Cyrus in the comments) the script looks something like the following:
#!/bin/bash
SERVERLIST=$1
INITIAL_WD="$PWD"
SCREEN_SESSION="mysession"
if ! screen -list | grep -q "$SCREEN_SESSION"; then
echo "Creating new screen session..."
screen -d -m -S "$SCREEN_SESSION"
fi
while IFS='' read line || [[ -n "$line" ]] ; do
echo "Uploading to ${line}"
screen -S "$SCREEN_SESSION" -X chdir "$INITIAL_WD"
screen -S "$SCREEN_SESSION" -X screen bash -c "lftp -e \"set ftp:ssl-allow false; mirror -Rvc\" \"$line\" ; exec bash"
done < <(cat $SERVERLIST)
This seems to work!
Shell Script - SSH using Screen
You fell victim to shell quoting - which is tricky when using ssh. Your command does something alike
cd home/Shell; ./AWS - teste.sh
so I would try
ssh -i keypair.pem -t ubuntu@ec2-address screen -S teste "sh -c \"cd home/Shell; ./AWS - teste.sh\""
Using a bash script to launch screen, then split and resize a screen
I know this is old but I needed this, too, and could not find an answer. I finally solved it out like this:
make a configuration file, say .splitscreenrc:
chdir /etc
screen 0
stuff "ls -1\n"
split
focus down
resize 3
chdir /tmp
screen 1
stuff "ls\n"
focus uprun
screen -c .splitscreenrc
Hope this helps someone ;)
Related Topics
Get Man Pages for Driver Functions
Linux Device Driver File Operations: It Is Possible to Have Race Conditions
How to Upgrade a Package in Linux That Was Built from Source
Get The Name of The Directory Where a Script Is Executed
Bash - Process Backspace Control Character When Redirecting Output to File
How to Generate Files in a Docker Container for Having The Same Owner as The Host's User
PHPmyadmin Mcrypt Extension Is Missing
Where Do Char Device Appear After Cdev_Add() Registers Successfully with Major on 117.
How to Launch a Background Process Through a Wrapper That'Ll Undo The Sigint Ignore
Assembly Divisions and Floating Points
Bash Script - Auto Fill Answer
Git - How to Remove Branch from Checkout Autocomplete
Is There Any General Interfaces on Linux to Simulate Mouse Movements and Click
Why Do Es and Ds Zero Out Eventually on 64 Bit Kernel When Set to Tls Selectors
Use "Git Revert" to Back-Out a Change Adding a Line